Search in sources :

Example 1 with PgMar

use of org.docx4j.wml.SectPr.PgMar 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 PgMar

use of org.docx4j.wml.SectPr.PgMar 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)

Example 3 with PgMar

use of org.docx4j.wml.SectPr.PgMar in project docx4j-template by vindell.

the class Docx4j_工具类_S3_Test method setDocMarginSpace.

/**
 * @Description:设置页边距
 */
public void setDocMarginSpace(WordprocessingMLPackage wordPackage, ObjectFactory factory, String top, String left, String bottom, String right) {
    SectPr sectPr = getDocSectPr(wordPackage);
    PgMar pg = sectPr.getPgMar();
    if (pg == null) {
        pg = factory.createSectPrPgMar();
        sectPr.setPgMar(pg);
    }
    if (StringUtils.isNotBlank(top)) {
        pg.setTop(new BigInteger(top));
    }
    if (StringUtils.isNotBlank(bottom)) {
        pg.setBottom(new BigInteger(bottom));
    }
    if (StringUtils.isNotBlank(left)) {
        pg.setLeft(new BigInteger(left));
    }
    if (StringUtils.isNotBlank(right)) {
        pg.setRight(new BigInteger(right));
    }
}
Also used : PgMar(org.docx4j.wml.SectPr.PgMar) SectPr(org.docx4j.wml.SectPr) BigInteger(java.math.BigInteger)

Aggregations

SectPr (org.docx4j.wml.SectPr)3 PgMar (org.docx4j.wml.SectPr.PgMar)3 PageDimensions (org.docx4j.model.structure.PageDimensions)2 Body (org.docx4j.wml.Body)2 BigInteger (java.math.BigInteger)1 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)1