1 回答

TA貢獻1784條經驗 獲得超2個贊
解決方案:
調整相關代碼的一部分sectPr,并pgMar以不增加新的欄目,但重復使用它們:
CTSectPr getSectPr = document.getDocument().getBody().getSectPr();
getSectPr.unsetPgMar();
CTPageMar addNewPgMar = getSectPr.addNewPgMar();
addNewPgMar.setLeft(BigInteger.valueOf(720L));
addNewPgMar.setTop(BigInteger.valueOf(720L));
addNewPgMar.setRight(BigInteger.valueOf(720L));
addNewPgMar.setBottom(BigInteger.valueOf(720L));
// Also good to handle footer and header for more expectable result
addNewPgMar.setFooter(BigInteger.valueOf(0L));
addNewPgMar.setHeader(BigInteger.valueOf(0L));
解釋:
問題的原因是XDocReport轉換器(它是一個獨立于Apache POI 的項目)只處理sectPr文檔的第一個條目。
您的示例將生成WordprocessingML >>如下:
<w:sectPr w:rsidR="003F19CD" w:rsidRPr="005E1322">
<w:pgSz w:h="16838" w:w="11906"/>
<w:pgMar w:bottom="1134" w:footer="708" w:header="708" w:left="1701" w:right="850" w:top="1134"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
<w:sectPr>
<w:pgMar w:bottom="620" w:left="620" w:right="620" w:top="620"/>
</w:sectPr>
在轉換為 PDF 的過程中,它將以 second pgmar( <w:pgMar w:bottom="620" w:left="620" w:right="620" w:top="620"/>) 被忽略的方式處理,因為它是 second 的一部分sectPr。
同時在將調整后的文檔保存到新的Word文檔pgMar的情況下,s 將被合并,您將看到所需的結果(調整后的邊距),新的WordprocessingML將如下所示:
<w:sectPr w:rsidR="003F19CD" w:rsidRPr="005E1322">
<w:pgSz w:h="16838" w:w="11906"/>
<w:pgMar w:left="620" w:top="620" w:right="620" w:bottom="620" w:footer="0" w:header="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
<w:sectPr>
<w:pgMar w:bottom="620" w:left="620" w:right="620" w:top="620"/>
</w:sectPr>
解決方案部分的代碼示例將生成單個sectPr和單個,pgMar因此PDFConverter將根據需要工作。
附加信息:
還需要提及的是XDocReport提供了配置可能性 >>:
options.setConfiguration(new IPdfWriterConfiguration() {
public void configure(PdfWriter writer) {
writer.setPDFXConformance(PdfWriter.PDFA1A);
}
});
但不幸的是,不可能以這種方式處理邊距(docx無論如何,文檔中的邊距值都會在配置完成后覆蓋它們)。
另外下面是pom.xml使用的依賴項:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
<version>2.0.1</version>
</dependency>
添加回答
舉報