poi设置Word页边距

Maven引入依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
注:poi-ooxml是CTPageMar类的依赖包

一、处理已有文档:

// word标准布局的页边距
private static long LEFT_MARGIN = 1800L;
private static long RIGHT_MARGIN = 1800L;
private static long TOP_MARGIN = 1440L;
private static long BOTTOM_MARGIN = 1440L;

CTSectPr sectPr = document.getDocument().getBody().getSectPr();
CTPageMar pageMar = sectPr.getPgMar();
pageMar.setLeft(BigInteger.valueOf(LEFT_MARGIN));
pageMar.setRight(BigInteger.valueOf(RIGHT_MARGIN));
pageMar.setTop(BigInteger.valueOf(TOP_MARGIN));
pageMar.setBottom(BigInteger.valueOf(BOTTOM_MARGIN));

二、处理新建文档:

CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
CTPageMar pageMar = sectPr.addNewPgMar();
pageMar.setLeft(BigInteger.valueOf(720L));
pageMar.setTop(BigInteger.valueOf(1440L));
pageMar.setRight(BigInteger.valueOf(720L));
pageMar.setBottom(BigInteger.valueOf(1440L));

-----------------------------------

参考:https://stackoverflow.com/questions/17787176/spacing-and-margin-settings-in-ms-word-document-using-apache-poi-docx

posted @ 2019-08-20 14:04  Commissar-Xia  阅读(2493)  评论(0编辑  收藏  举报