添加横向页面的步骤位,填分割文档,再调整宽度
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
代码
final XWPFDocument xwpfComment
= new XWPFDocument(new FileInputStream("文件路径"));
final CTBody body = xwpfComment.getDocument().getBody();
//插入分隔符,实现部分page 横向
final XWPFParagraph paragraph = xwpfComment.createParagraph();
final CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
//用于调整页面宽度
final CTPageSz ctPageSz =
ctSectPr.isSetPgSz() ? ctSectPr.getPgSz() : ctSectPr.addNewPgSz();
ctPageSz.setW(BigInteger.valueOf(11906));
ctPageSz.setH(BigInteger.valueOf(16838));
//分割页面
paragraph.setPageBreak(true);
//插入横向页面
final CTSectPr sectPr = body.getSectPr();
final CTPageSz ctPageSz1 = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addNewPgSz();
ctPageSz1.setH(BigInteger.valueOf(11906));
ctPageSz1.setW(BigInteger.valueOf(16838));
ctPageSz1.setOrient(STPageOrientation.LANDSCAPE);
//
final XWPFParagraph paragraph1 = xwpfComment.createParagraph();
paragraph1.createRun().setText("页面横向了---------");
final FileOutputStream outputStream = new FileOutputStream("新文件路径");
xwpfComment.write(outputStream);
xwpfComment.close();