当前版本 5.2.2 ,版本不同,写法略有差异
- poi-5.2.2 操作word 【段落】
- poi-5.2.2 操作word 【表格】
- poi-5.2.2 操作word【单元格、行、列】
- poi 5.2.2 操作word【页眉页脚】
- poi 5.2.2 操作word【纸张、边距】
- poi-5.2.2 操作word【图片操作相关】
- poi 5.2.2 操作word【目录】
本文所需依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-full</artifactId>
<version>5.2.2</version>
</dependency>
纸张、正文操作相关
- 设置纸张大小
/**
* 设置纸张大小
* 若要强限制纸张大小的参数,可以将常量改为枚举
* @param wordConstants_paperSize {@link WordBaseConstants#PAPER_SIZE_A4}
*/
public static void setPaperSize(XWPFDocument document , String wordConstants_paperSize){
String[] split = wordConstants_paperSize.split("-");
CTBody body = document.getDocument().getBody();
CTSectPr sectPr = body.isSetSectPr() ? body.getSectPr() : body.addNewSectPr();
CTPageSz pageSize = sectPr.addNewPgSz();
int w = Integer.parseInt(split[0]) * 20;
int h = Integer.parseInt(split[1]) * 20;
pageSize.setW(BigInteger.valueOf(w));
pageSize.setH(BigInteger.valueOf(h));
if(w > h){ //横版
pageSize.setOrient(STPageOrientation.LANDSCAPE);
}else{
pageSize.setOrient(STPageOrientation.PORTRAIT);
}
}
- 获取纸张高度
/**
* 获取word纸张高度 单位 磅 【需提前设置过纸张大小,获取不到返回0】
* */
public static int getPaperSizeH(XWPFDocument docxDocument){
try {
return (Integer) docxDocument.getDocument().getBody().getSectPr().getPgSz().getH() / 20;
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
- 获取纸张宽度
/**
* 获取word纸张宽度 单位 磅 【需提前设置过纸张大小,获取不到返回0】
* */
public static int getPaperSizeW(XWPFDocument docxDocument){
try {
return (Integer) docxDocument.getDocument().getBody().getSectPr().getPgSz().getW() / 20;
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
- 设置页边距
/**
* 设置页边距 单位 磅
*/
public static void setPageMargin(XWPFDocument document, int left, int top, int right, int bottom) {
//一磅 = 20缇
CTBody body = document.getDocument().getBody();
CTSectPr sectPr = body.isSetSectPr() ? body.getSectPr() : body.addNewSectPr();
CTPageMar ctpagemar = sectPr.isSetPgMar() ? sectPr.getPgMar() : sectPr.addNewPgMar();
ctpagemar.setLeft(new BigInteger((left*20) + ""));
ctpagemar.setTop(new BigInteger((top * 20) + ""));
ctpagemar.setRight(new BigInteger((right * 20) + ""));
ctpagemar.setBottom(new BigInteger((bottom * 20) + ""));
}
- 设置A4常规页边距(默认页边距)
/** A4纸 常规 页边距 */
public static void setPageMargin_norm(XWPFDocument document) {
setPageMargin(document , 72 , 90 , 72 , 90);
}
- 获取页面左边距
/**
* 获取页面左边距(正文左边距到纸张左边距) 单位 磅
*/
public static int getPageLeftMergin(XWPFDocument document){
CTPageMar ctPageMar = getCTPageMar(document);
return (Integer) ctPageMar.getLeft() / 20;
}
- 获取页面上边距
/**
* 获取页面上边距(正文上边距到纸张上边距的距离) 单位 磅
*/
public static int getPageTopMergin(XWPFDocument document){
CTPageMar ctPageMar = getCTPageMar(document);
return (Integer) ctPageMar.getTop() / 20;
}
- 获取页面右边距
/**
* 获取页面右边距(正文右边距到纸张右边距的距离) 单位 磅
*/
public static int getPageRightMergin(XWPFDocument document){
CTPageMar ctPageMar = getCTPageMar(document);
return (Integer) ctPageMar.getRight() / 20;
}
- 获取页面下边距
/**
* 获取页面下边距(正文下边距到纸张下边距的距离) 单位 磅
*/
public static int getPageBottomMergin(XWPFDocument document){
CTPageMar ctPageMar = getCTPageMar(document);
return (Integer) ctPageMar.getBottom() / 20;
}
- 获取正文宽度
/**
* 获取word正文宽度 单位 磅 【获取不到返回0,须提前设置纸张大小,页边距,若不设置,word处理的时候会使用默认的】
* */
public static int getPagebodyWidth(XWPFDocument document){
try {
//下面这个单位(缇)是英寸 * 1440 得到的, 一英寸 = 914400 EMUs
CTSectPr sectPr = document.getDocument().getBody().getSectPr();
if(sectPr != null){
int pageSize = ((BigInteger) sectPr.getPgSz().getW()).intValue(); //纸张大小
int leftMar = ((BigInteger) sectPr.getPgMar().getLeft()).intValue(); //左缩进
int rightMar = ((BigInteger) sectPr.getPgMar().getRight()).intValue(); //右缩进
return (pageSize - leftMar - rightMar) / 20;
}else{
return WordBaseConstants.PAPER_A4_W - 72 - 72; //不设置纸张大小和边距的情况下默认宽度
}
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
- 获取正文高度
/**
* 获取word正文高度 单位 磅 【需提前设置过纸张大小、页边距,不设置返回默认,获取不到返回0】
* */
public static int getPagebodyHeight(XWPFDocument docxDocument){
try {
//下面这个单位(缇)是英寸 * 1440 得到的, 一英寸 = 914400 EMUs
CTSectPr sectPr = docxDocument.getDocument().getBody().getSectPr();
if(sectPr != null){
int pageSize = (Integer) sectPr.getPgSz().getH(); //纸张大小
int topMar = (Integer) sectPr.getPgMar().getTop(); //上缩进
int bottomMar = (Integer) sectPr.getPgMar().getBottom(); //下缩进
return (pageSize - topMar - bottomMar) / 20;
}else{
return WordBaseConstants.PAPER_A4_H - 90 - 90; //不设置纸张大小和边距的情况下默认高度
}
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
- 涉及到的子方法
private static CTPageMar getCTPageMar(XWPFDocument document){
CTBody body = document.getDocument().getBody();
CTSectPr sectPr = body.isSetSectPr() ? body.getSectPr() : body.addNewSectPr();
CTPageMar ctpagemar = sectPr.isSetPgMar() ? sectPr.getPgMar() : sectPr.addNewPgMar();
return ctpagemar;
}
- 涉及常量
public class WordBaseConstants {
/**纸张大小 - A4*/
public static final String PAPER_SIZE_A4 = "595-842";
/**纸张大小 - A4 磅*/
public static final int PAPER_A4_W = 595;
/**纸张大小 - A4 磅*/
public static final int PAPER_A4_H = 843;
/**纸张大小 - A4-横版*/
public static final String PAPER_SIZE_A4_H = "842-595";
/**纸张大小 - A3*/
public static final String PAPER_SIZE_A3 = "842-1191";
}