poi设置Word页边距

Maven引入依赖:


    org.apache.poi
    poi
    3.17


    org.apache.poi
    poi-scratchpad
    3.17


    org.apache.poi
    poi-ooxml
    3.17
注: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

转载于:https://www.cnblogs.com/commissar-Xia/p/11382538.html

你可能感兴趣的:(poi设置Word页边距)