Search in sources :

Example 1 with PageDimensions

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();
    }
}
Also used : PageDimensions(org.docx4j.model.structure.PageDimensions) PgMar(org.docx4j.wml.SectPr.PgMar) SectPr(org.docx4j.wml.SectPr) Body(org.docx4j.wml.Body) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 2 with PageDimensions

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;
}
Also used : SectionWrapper(org.docx4j.model.structure.SectionWrapper) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) URISyntaxException(java.net.URISyntaxException) TocException(org.docx4j.toc.TocException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) PageDimensions(org.docx4j.model.structure.PageDimensions) org.docx4j.wml(org.docx4j.wml) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 3 with PageDimensions

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();
    }
}
Also used : PageDimensions(org.docx4j.model.structure.PageDimensions) PgMar(org.docx4j.wml.SectPr.PgMar) SectPr(org.docx4j.wml.SectPr) Body(org.docx4j.wml.Body)

Aggregations

PageDimensions (org.docx4j.model.structure.PageDimensions)3 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)2 Body (org.docx4j.wml.Body)2 SectPr (org.docx4j.wml.SectPr)2 PgMar (org.docx4j.wml.SectPr.PgMar)2 URISyntaxException (java.net.URISyntaxException)1 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)1 SectionWrapper (org.docx4j.model.structure.SectionWrapper)1 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)1 TocException (org.docx4j.toc.TocException)1 org.docx4j.wml (org.docx4j.wml)1