poi設置Word頁邊距


Maven引入依賴:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
注:poi-ooxml是CTPageMar類的依賴包

一、處理已有文檔:

// word標准布局的頁邊距
private static long LEFT_MARGIN = 1800L;
private static long RIGHT_MARGIN = 1800L;
private static long TOP_MARGIN = 1440L;
private static long BOTTOM_MARGIN = 1440L;

CTSectPr sectPr = document.getDocument().getBody().getSectPr();
CTPageMar pageMar = sectPr.getPgMar();
pageMar.setLeft(BigInteger.valueOf(LEFT_MARGIN));
pageMar.setRight(BigInteger.valueOf(RIGHT_MARGIN));
pageMar.setTop(BigInteger.valueOf(TOP_MARGIN));
pageMar.setBottom(BigInteger.valueOf(BOTTOM_MARGIN));

二、處理新建文檔:

CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
CTPageMar pageMar = sectPr.addNewPgMar();
pageMar.setLeft(BigInteger.valueOf(720L));
pageMar.setTop(BigInteger.valueOf(1440L));
pageMar.setRight(BigInteger.valueOf(720L));
pageMar.setBottom(BigInteger.valueOf(1440L));

-----------------------------------

參考:https://stackoverflow.com/questions/17787176/spacing-and-margin-settings-in-ms-word-document-using-apache-poi-docx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM