Java 如何设置 Word 文档的页面方向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20188953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:51:09  来源:igfitidea点击:

How to set page orientation for Word document?

javaapache-poilandscape-portraitorientation-changesxwpf

提问by Kapelchik

I use Apache POI XWPFto create and handle MS Worddocuments. But I didn't find in the documentation how to change the page orientation.

我使用Apache POI XWPF创建和处理MS Word文档。但是我没有在文档中找到如何更改页面方向。

Apparently this way should make it:

显然这种方式应该做到:

XWPFDocument doc = new XWPFDocument();

CTDocument1 document = doc.getDocument();
CTBody body = document.getBody();

if (!body.isSetSectPr()) {
     body.addNewSectPr();
}
CTSectPr section = body.getSectPr();

if(!section.isSetPgSz()) {
    section.addNewPgSz();
}
CTPageSz pageSize = section.getPgSz();

pageSize.setOrient(STPageOrientation.LANDSCAPE);

But this method doesn't work properly. I can set the page orientation to landscape, and when I read the page orientation in the code, I get landscape. All right. But if I open the saved document I've portrait format. This setting doesn't work in fact. What could be the problem?

但是这种方法不能正常工作。我可以将页面方向设置为横向,当我在代码中读取页面方向时,我得到横向。好的。但是,如果我打开保存的文档,我会使用纵向格式。这个设置实际上不起作用。可能是什么问题呢?

As a workaround, I'm forced to start work with a blank document created manually in landscape or portrait format. But I want to create documents programmatically from scratch in needed orientation.

作为一种解决方法,我不得不开始使用以横向或纵向格式手动创建的空白文档。但我想以所需的方向从头开始以编程方式创建文档。

For instance POI HSSF and XSSFhave functionality to toggle between landscape and portrait mode. It's setLandscape()method of org.apache.poi.ss.usermodel.PrintSetupinterface.

例如,POI HSSF 和 XSSF具有在横向和纵向模式之间切换的功能。它是接口的setLandscape()方法org.apache.poi.ss.usermodel.PrintSetup

But what about XWPFor HWPF?

但是XWPFHWPF呢?

采纳答案by Zach

You were very much on the right path. Setting the orientation to landscape describes the general orientation of the paper, but will still need the sizeof the paper. Your CTPageSz object doesn't have that yet.

你走在正确的道路上。将方向设置为横向描述了纸张的一般方向,但仍需要纸张的尺寸。您的 CTPageSz 对象还没有。

This means in addition to your setOrient call, you'll need to both setW and setH. These calls take BigIntegers that are representative of 1/20 Point. For a landscaped LETTER type paper therefore, you'll just:

这意味着除了 setOrient 调用之外,您还需要 setW 和 setH。这些调用采用代表 1/20 点的 BigInteger。因此,对于景观信纸类型的纸张,您只需:

pageSize.setW(BigInteger.valueOf(15840));
pageSize.setH(BigInteger.valueOf(12240));

For Word to recognize it as Landscaped, the width must be greater than the height. You still want to keep the setOrient call as well if you want it to behave properly when going to print.

为了让 Word 将其识别为 Landscaped,宽度必须大于高度。如果您希望它在打印时正常运行,您仍然希望保留 setOrient 调用。

Here's some common paper sizes in points from https://www.gnu.org/software/gv/manual/html_node/Paper-Keywords-and-paper-size-in-points.htmlyou should take these and multiply them by twenty to use in the above method calls

以下是https://www.gnu.org/software/gv/manual/html_node/Paper-Keywords-and-paper-size-in-points.html 中的一些常见纸张尺寸, 您应该将这些尺寸乘以 20在上述方法调用中使用

Letter       612x792
LetterSmall  612x792
Tabloid      792x1224
Ledger       1224x792
Legal        612x1008
Statement    396x612
Executive    540x720
A0           2384x3371
A1           1685x2384
A2           1190x1684
A3           842x1190
A4           595x842
A4Small      595x842
A5           420x595
B4           729x1032
B5           516x729
Folio        612x936
Quarto       610x780
10x14        720x1008

回答by nrodriguez

Answer is right.

答案是对的。

I just had to add extra dependencies to be able to access CTPageSz class.

我只需要添加额外的依赖项就可以访问 CTPageSz 类。

// SBT config
"org.apache.poi" % "poi-ooxml" % "4.1.0",     // Base library
"org.apache.poi" % "ooxml-schemas" % "1.4",   // required to access CTPageSz