【问题标题】:How to change margins when converting with apache poi使用 apache poi 转换时如何更改边距
【发布时间】:2018-09-23 13:05:17
【问题描述】:

当我从 Microsoft Word 文档转换时,我需要更改 PDF 文件的边距。

public class TestCon {
    public static final String DEST = "./test.pdf";
    public static final String SRC = "./test.docx";

    public static void main(String[] args) {
        try {
            InputStream doc = new FileInputStream(new File(SRC));

            XWPFDocument document = new XWPFDocument(doc );
            CTSectPr addNewSectPr = document.getDocument().getBody().addNewSectPr();
            CTPageMar addNewPgMar = addNewSectPr.addNewPgMar();
            addNewPgMar.setLeft(BigInteger.valueOf(720L));
            addNewPgMar.setTop(BigInteger.valueOf(720L));
            addNewPgMar.setRight(BigInteger.valueOf(720L));
            addNewPgMar.setBottom(BigInteger.valueOf(720L));

            OutputStream out = new FileOutputStream(new File(DEST));
            PdfOptions options = PdfOptions.create();
            PdfConverter.getInstance().convert(document, out, options);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

这不起作用。 pdf中的边距没有变化


但是当我这样做时:

        FileOutputStream out = new FileOutputStream(new File(SRC1));
        InputStream doc = new FileInputStream(new File(SRC));

        XWPFDocument document = new XWPFDocument(doc );
        CTSectPr addNewSectPr = document.getDocument().getBody().addNewSectPr();
        CTPageMar addNewPgMar = addNewSectPr.addNewPgMar();
        addNewPgMar.setLeft(BigInteger.valueOf(720L));
        addNewPgMar.setTop(BigInteger.valueOf(720L));
        addNewPgMar.setRight(BigInteger.valueOf(720L));
        addNewPgMar.setBottom(BigInteger.valueOf(720L));
        document.write(out);
        out.close();

无需转换为 PDF 即可。

【问题讨论】:

标签: java pdf apache-poi docx


【解决方案1】:

解决办法:

调整与sectPrpgMar 相关的代码部分,不添加新部分,而是重复使用它们:

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 的过程中,第二个 pgmar (&lt;w:pgMar w:bottom="620" w:left="620" w:right="620" w:top="620"/&gt;) 将被忽略,因为它是第二个 sectPr 的一部分。

在将调整后的文档保存到新的 Word 文档的同时,pgMars 将被合并,您将看到所需的结果(调整后的边距),新的 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提供configuration possibility >>:

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>

【讨论】:

  • 非常感谢!我还有一个问题,是否可以通过 WordprocessingML 进行自动断字?
  • @crazy_deviL,它支持,因为它是由 MicrosoftExcelWord 开发的基于 XML 的文档格式文件。请阅读此question >>,了解存储自动连字配置的更多详细信息。但这并不意味着 XDocReport 的转换器将支持自动连字符(您可以简单地对其进行测试并发现它不支持)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2014-06-05
  • 1970-01-01
  • 2022-07-25
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多