public static CxCy scale(ImageInfo imageInfo, PageDimensions page) { double writableWidthTwips = page.getWritableWidthTwips(); log.debug("writableWidthTwips: " + writableWidthTwips); ImageSize size = imageInfo.getSize(); Dimension2D dPt = size.getDimensionPt(); double imageWidthTwips = dPt.getWidth() * 20; log.debug("imageWidthTwips: " + imageWidthTwips); long cx; long cy; boolean scaled = false; if (imageWidthTwips > writableWidthTwips) { log.debug("Scaling image to fit page width"); scaled = true; cx = UnitsOfMeasurement.twipToEMU(writableWidthTwips); cy = UnitsOfMeasurement.twipToEMU( dPt.getHeight() * 20 * writableWidthTwips / imageWidthTwips); } else { log.debug("Scaling image - not necessary"); cx = UnitsOfMeasurement.twipToEMU(imageWidthTwips); cy = UnitsOfMeasurement.twipToEMU(dPt.getHeight() * 20); } log.debug("cx=" + cx + "; cy=" + cy); return new CxCy(cx, cy, scaled); }
public static WordprocessingMLPackage createPackage(PageSizePaper sz, boolean landscape) throws InvalidFormatException { // Create a package WordprocessingMLPackage wmlPack = new WordprocessingMLPackage(); // Create main document part MainDocumentPart wordDocumentPart = new MainDocumentPart(); // Create main document part content org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory(); org.docx4j.wml.Body body = factory.createBody(); org.docx4j.wml.Document wmlDocumentEl = factory.createDocument(); wmlDocumentEl.setBody(body); // Create a basic sectPr using our Page model PageDimensions page = new PageDimensions(); page.setPgSize(sz, landscape); SectPr sectPr = factory.createSectPr(); body.setSectPr(sectPr); sectPr.setPgSz(page.getPgSz()); sectPr.setPgMar(page.getPgMar()); // Put the content in the part wordDocumentPart.setJaxbElement(wmlDocumentEl); // Add the main document part to the package relationships // (creating it if necessary) wmlPack.addTargetPart(wordDocumentPart); // Create a styles part Part stylesPart = new org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart(); try { ((org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart) stylesPart) .unmarshalDefaultStyles(); // Add the styles part to the main document part relationships // (creating it if necessary) wordDocumentPart.addTargetPart(stylesPart); // NB - add it to main doc part, not package! } catch (Exception e) { // TODO: handle exception // e.printStackTrace(); log.error(e.getMessage(), e); } // Metadata: docx4j 2.7.1 can populate some of this from docx4j.properties // See SaveToZipFile DocPropsCorePart core = new DocPropsCorePart(); org.docx4j.docProps.core.ObjectFactory coreFactory = new org.docx4j.docProps.core.ObjectFactory(); core.setJaxbElement(coreFactory.createCoreProperties()); wmlPack.addTargetPart(core); DocPropsExtendedPart app = new DocPropsExtendedPart(); org.docx4j.docProps.extended.ObjectFactory extFactory = new org.docx4j.docProps.extended.ObjectFactory(); app.setJaxbElement(extFactory.createProperties()); wmlPack.addTargetPart(app); // Return the new package return wmlPack; }
private void setPageMargins() { try { Body body = wordMLPackage.getMainDocumentPart().getContents().getBody(); Padding padding = bean.getReportLayout().getPagePadding(); PageDimensions page = new PageDimensions(); PgMar pgMar = page.getPgMar(); pgMar.setBottom(BigInteger.valueOf(pixelsToDxa(padding.getBottom()))); pgMar.setTop(BigInteger.valueOf(pixelsToDxa(padding.getTop()))); pgMar.setLeft(BigInteger.valueOf(pixelsToDxa(padding.getLeft()))); pgMar.setRight(BigInteger.valueOf(pixelsToDxa(padding.getRight()))); SectPr sectPr = factory.createSectPr(); body.setSectPr(sectPr); sectPr.setPgMar(pgMar); } catch (Docx4JException e) { e.printStackTrace(); } }