Poi3.17生成word设置页边距
原创
©著作权归作者所有:来自51CTO博客作者某科学的南条的原创作品,请联系作者获取转载授权,否则将追究法律责任
依赖:
<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));
一般生成word的时候都是属于处理新建文档,所以使用第二种方法就行