【发布时间】:2018-10-05 07:51:09
【问题描述】:
我正在使用 docx4j 和 docx stamper 创建报告
我有一个主文档,其中包含我的目录和徽标以及为其设置的页码。然后我开始使用 docx stamper 填充其他 docx 布局,然后将它们添加到主要部分的末尾。问题是,如果我将一个 docx 布局中的页面方向设置为横向,然后将其添加到主要部分,它就会失去方向。结果包含两个部分和正确的页码。保留所有样式,但具有横向方向的第一和第三部分显示为纵向,与主文档中一样。
documentPartBase = wordMLPackageBase.getMainDocumentPart();
WordprocessingMLPackage wordMLPackageOne = fillFirstTemplate.fillTempLe();
for (Object obj : wordMLPackageOne.getMainDocumentPart().getContent()) {
documentPartBase.addObject(obj);
}
documentPartBase.addObject(pageBreak);
WordprocessingMLPackage wordMLPackageTwo = fillSecTemplate.fillTempLe();
for (Object obj : wordMLPackageTwo.getMainDocumentPart().getContent()) {
documentPartBase.addObject(obj);
}
documentPartBase.addObject(pageBreak);
WordprocessingMLPackage wordMLPackageThree = fillThirdTemplate.fillTempLe();
for (Object obj : wordMLPackageThree.getMainDocumentPart().getContent()) {
documentPartBase.addObject(obj);
}
documentPartBase.addObject(pageBreak);
wordMLPackageBase.save(new File(Paths.get(".").getFileSystem().getPath("D:").toString(), resultFileName))
我尝试在将部分添加到主要部分之前添加一个横向包,但没有帮助:
WordprocessingMLPackage aPackage;
try {
aPackage = createPackage(PageSizePaper.A4, true);
} catch (InvalidFormatException e) {
throw new RuntimeException("Unhandled exception occurred.", e);
}
for (Object obj :aPackage.getMainDocumentPart().getContent()){
documentPartBase.addObject(obj);
}
WordprocessingMLPackage wordMLPackageThree = fillThirdTemplate.fillTempLe();
for (Object obj : wordMLPackageThree.getMainDocumentPart().getContent()) {
documentPartBase.addObject(obj);
}
并且还尝试添加:
try {
sectPr = (SectPr) XmlUtils.unmarshalString("<w:sectPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
+ "<w:pgSz w:w=\"16839\" w:h=\"11907\" w:orient=\"landscape\"/>"
+ "</w:sectPr>");
} catch (JAXBException e) {
throw new RuntimeException("Unhandled exception occurred.", e);
}
wordMLPackageThree.getMainDocumentPart().getContent().add(sectPr);
根据
添加如下方法就像下面的代码使整个文档显示为横向!
makeLandscape(wordMLPackageThree.getMainDocumentPart(), STPageOrientation.LANDSCAPE);
for (Object obj : wordMLPackageThree.getMainDocumentPart().getContent())
{
//Properties properties = Docx4jProperties.getProperties();
//properties.setProperty("docx4j.PageOrientationLandscape", "true");
documentPartBase.addObject(obj);
}
private void makeLandscape(MainDocumentPart mdp, STPageOrientation stPageOrientation)
{
ObjectFactory objectFactory = new ObjectFactory();
SectPr sectionLandscape = objectFactory.createSectPr();
SectPr.PgSz landscape = new SectPr.PgSz();
landscape.setOrient(stPageOrientation);
landscape.setH(BigInteger.valueOf(11906));
landscape.setW(BigInteger.valueOf(16383));
sectionLandscape.setPgSz(landscape);
org.docx4j.wml.P p = objectFactory.createP();
PPr createPPr = objectFactory.createPPr();
createPPr.setSectPr(sectionLandscape);
p.setPPr(createPPr);
mdp.addObject(p);
}
我按照第一条评论中的说法进行了尝试,但整个文档是横向的,并且在末尾添加了一个空的纵向页面:
makeLandscape(documentPartBase,STPageOrientation.LANDSCAPE);
WordprocessingMLPackage wordMLPackageThree= fillFunktionenLe.fillFunktionenLe();
for (Object obj : wordMLPackageThree.getMainDocumentPart().getContent()) {
documentPartBase.addObject(obj);
}
makeLandscape(documentPartBase,STPageOrientation.PORTRAIT);
这也没有帮助。
如何在将内容附加在一起后使用 docx4j 保持页面方向?
【问题讨论】:
-
在你想要的横向内容之后添加一个 sectPr 元素是你需要做的。这意味着除非第 1 页是横向的,否则您还需要在横向内容之前添加一个 sectPr(即指定您想要纵向内容)。
-
我尝试在第三部分前后改变方向,但它只是让我的页脚消失了!
-
好吧,您需要了解页脚在 sectPr 中是如何工作的。如果太难了,Plutext 的商业 Docx4j Enterprise 中的 MergeDocx 组件可能会有所帮助:plutext.com/m/index.php/products
标签: docx4j