use of org.docx4j.model.structure.PageDimensions in project Java-Tutorial by gpcodervn.
the class Docx4jUtils method setPageMargins.
/**
* Set document page margins to 50 pixels
*/
private void setPageMargins() {
try {
Body body = wordMLPackage.getMainDocumentPart().getContents().getBody();
PageDimensions page = new PageDimensions();
PgMar pgMar = page.getPgMar();
pgMar.setBottom(BigInteger.valueOf(pixelsToDxa(50)));
pgMar.setTop(BigInteger.valueOf(pixelsToDxa(50)));
pgMar.setLeft(BigInteger.valueOf(pixelsToDxa(50)));
pgMar.setRight(BigInteger.valueOf(pixelsToDxa(50)));
SectPr sectPr = factory.createSectPr();
body.setSectPr(sectPr);
sectPr.setPgMar(pgMar);
} catch (Docx4JException e) {
e.printStackTrace();
}
}
use of org.docx4j.model.structure.PageDimensions in project flexmark-java by vsch.
the class CoreNodeDocxRenderer method newImage.
public R newImage(final DocxRendererContext docx, byte[] bytes, String filenameHint, Attributes attributes, int id1, int id2) {
try {
BinaryPartAbstractImage imagePart = null;
imagePart = BinaryPartAbstractImage.createImagePart(docx.getPackage(), docx.getContainerPart(), bytes);
Inline inline = null;
String altText = attributes.contains("alt") ? attributes.getValue("alt") : "";
List<SectionWrapper> sections = docx.getPackage().getDocumentModel().getSections();
PageDimensions page = sections.get(sections.size() - 1).getPageDimensions();
double writableWidthTwips = page.getWritableWidthTwips();
long cx = getSizeInfo(attributes, "width", page.getWritableWidthTwips());
long cy = cx > 0 ? getSizeInfo(attributes, "height", page.getWritableHeightTwips()) : -1;
// kludge: normally there is no max-width attribute but we can fake it
long longMaxWidth = getSizeInfo(attributes, "max-width", page.getWritableWidthTwips());
int maxWidth = longMaxWidth > 0 && longMaxWidth <= Integer.MAX_VALUE ? (int) longMaxWidth : -1;
if (cx > 0 && cy > 0) {
// here we need cx & cy in emu which needs conversion from twips
cx = UnitsOfMeasurement.twipToEMU(cx);
cy = UnitsOfMeasurement.twipToEMU(cy);
inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, cy, false);
} else {
if (cx > 0) {
inline = imagePart.createImageInline(filenameHint, altText, id1, id2, cx, false);
} else {
if (maxWidth > 0) {
inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false, maxWidth);
} else {
inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);
}
}
}
// Now add the inline in w:p/w:r/w:drawing
org.docx4j.wml.R run = docx.createR();
org.docx4j.wml.Drawing drawing = docx.getFactory().createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return run;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.docx4j.model.structure.PageDimensions in project docx4j-template by vindell.
the class Docx4jStyle_S3 method setPageMargins.
public void setPageMargins(WordprocessingMLPackage wordMLPackage, ObjectFactory factory) {
try {
Body body = wordMLPackage.getMainDocumentPart().getContents().getBody();
PageDimensions page = new PageDimensions();
PgMar pgMar = page.getPgMar();
pgMar.setBottom(BigInteger.valueOf(pixelsToDxa(50)));
pgMar.setTop(BigInteger.valueOf(pixelsToDxa(50)));
pgMar.setLeft(BigInteger.valueOf(pixelsToDxa(50)));
pgMar.setRight(BigInteger.valueOf(pixelsToDxa(50)));
SectPr sectPr = factory.createSectPr();
body.setSectPr(sectPr);
sectPr.setPgMar(pgMar);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations