亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 apache poi 轉換時如何更改邊距

使用 apache poi 轉換時如何更改邊距

皈依舞 2021-10-06 12:52:34
當我從 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,它就可以工作。
查看完整描述

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>


查看完整回答
反對 回復 2021-10-06
  • 1 回答
  • 0 關注
  • 342 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號