在Java中使用Apache POI XWPF相同word文档中横向和纵向页面 [英] Landscape and portrait pages in the same word document using Apache POI XWPF in Java
问题描述
我试图用Java和Apache POI库来创建包含了一些横向和纵向的一些页面的Word文档。我可以改变所有网页的方向,但有没有办法改变他们有的只是方向?我一直在使用不同的部分和机构尝试过,但都无济于事。
I am trying to use Java and the Apache POI library to create a word document that contained some landscape and some portrait pages. I can change the orientation of all the pages, but is there a way to change just the orientation of some of them? I have tried using different sections and bodies, but to no avail.
目前,我写了一个函数,接受XWPFDocument作为输入和改变一个新的身体的方向。我曾希望改变的新机构将不会影响previous页面的方向,但它影响整个文档。
At the moment I have written a function that takes a XWPFDocument as input and changes the orientation of a new body. I had hoped changing the orientation for new body would not affect the previous pages, but it affects the entire document.
private void changeOrientation(XWPFDocument document, String orientation){
CTDocument1 doc = document.getDocument();
CTBody body = doc.addNewBody();
body.addNewSectPr();
CTSectPr section = body.getSectPr();
if(!section.isSetPgSz()) {
section.addNewPgSz();
}
CTPageSz pageSize = section.getPgSz();
if(orientation.equals("landscape")){
pageSize.setOrient(STPageOrientation.LANDSCAPE);
pageSize.setW(BigInteger.valueOf(842 * 20));
pageSize.setH(BigInteger.valueOf(595 * 20));
}
else{
pageSize.setOrient(STPageOrientation.PORTRAIT);
pageSize.setH(BigInteger.valueOf(842 * 20));
pageSize.setW(BigInteger.valueOf(595 * 20));
}
}
谢谢!
推荐答案
据OOXML规范<一个href=\"http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-376,%20Fourth%20Edition,%20Part%201%20-%20Fundamentals%20And%20Markup%20Language%20Reference.zip\"相对=nofollow> ECMA-376,第四版,第1部分 - 基础知识和标记语言参考 - 18年6月17日sectPr(节属性),具有多节文档中,截面特性(在sectPr元素)是存储的子元素:
According to OOXML Specification ECMA-376, Fourth Edition, Part 1 - Fundamentals And Markup Language Reference - 17.6.18 sectPr (Section Properties), in a document with multiple sections, section properties (the sectPr element) are stored as the child element of :
- 在节末段,对于除最后所有章节
节, - body元素,在最后一节。
所以,要改变部分的页面方向应该创造或找到相应的sectPr和使用以下code:
So, to change page orientation of a section one should create or locate corresponding sectPr and use following code:
private void changeOrientation(CTSectPr section, String orientation) {
CTPageSz pageSize = section.isSetPgSz? section.getPgSz() : section.addNewPgSz();
if (orientation.equals("landscape")) {
pageSize.setOrient(STPageOrientation.LANDSCAPE);
pageSize.setW(BigInteger.valueOf(842 * 20));
pageSize.setH(BigInteger.valueOf(595 * 20));
} else {
pageSize.setOrient(STPageOrientation.PORTRAIT);
pageSize.setH(BigInteger.valueOf(842 * 20));
pageSize.setW(BigInteger.valueOf(595 * 20));
}
}
这篇关于在Java中使用Apache POI XWPF相同word文档中横向和纵向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!