第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 apache poi 轉(zhuǎn)換時(shí)如何更改邊距

使用 apache poi 轉(zhuǎn)換時(shí)如何更改邊距

皈依舞 2021-10-06 12:52:34
當(dāng)我從 Microsoft Word 文檔轉(zhuǎn)換時(shí),我需要更改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中的邊距不會(huì)改變但是當(dāng)我這樣做時(shí):        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();無(wú)需轉(zhuǎn)換為PDF,它就可以工作。
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

解決方案:

調(diào)整相關(guān)代碼的一部分sectPr,并pgMar以不增加新的欄目,但重復(fù)使用它們:


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));

解釋:

問(wèn)題的原因是XDocReport轉(zhuǎn)換器(它是一個(gè)獨(dú)立于Apache POI 的項(xiàng)目)只處理sectPr文檔的第一個(gè)條目。


您的示例將生成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>

在轉(zhuǎn)換為 PDF 的過(guò)程中,它將以 second pgmar( <w:pgMar w:bottom="620" w:left="620" w:right="620" w:top="620"/>) 被忽略的方式處理,因?yàn)樗?second 的一部分sectPr。


同時(shí)在將調(diào)整后的文檔保存到新的Word文檔pgMar的情況下,s 將被合并,您將看到所需的結(jié)果(調(diào)整后的邊距),新的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>

解決方案部分的代碼示例將生成單個(gè)sectPr和單個(gè),pgMar因此PDFConverter將根據(jù)需要工作。


附加信息:

還需要提及的是XDocReport提供了配置可能性 >>:


options.setConfiguration(new IPdfWriterConfiguration() {

    public void configure(PdfWriter writer) {

        writer.setPDFXConformance(PdfWriter.PDFA1A);

    }

});

但不幸的是,不可能以這種方式處理邊距(docx無(wú)論如何,文檔中的邊距值都會(huì)在配置完成后覆蓋它們)。


另外下面是pom.xml使用的依賴項(xiàng):


<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>


查看完整回答
反對(duì) 回復(fù) 2021-10-06
  • 1 回答
  • 0 關(guān)注
  • 344 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)