当前位置: 首页 > 工具软件 > docx4j > 使用案例 >

java docx4j_[简单]docx4j常用方法小结

董良策
2023-12-01

1 importjava.io.File;2 importjava.io.FileInputStream;3 importjava.io.InputStream;4 importjava.io.StringWriter;5 importjava.math.BigInteger;6 importjava.util.ArrayList;7 importjava.util.List;8

9 importjavax.xml.bind.JAXBElement;10

11 importorg.apache.commons.io.IOUtils;12 importorg.apache.commons.lang3.StringUtils;13 importorg.docx4j.TextUtils;14 importorg.docx4j.XmlUtils;15 importorg.docx4j.dml.wordprocessingDrawing.Inline;16 importorg.docx4j.model.properties.table.tr.TrHeight;17 importorg.docx4j.openpackaging.packages.OpcPackage;18 importorg.docx4j.openpackaging.packages.WordprocessingMLPackage;19 importorg.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;20 importorg.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;21 importorg.docx4j.openpackaging.parts.relationships.Namespaces;22 importorg.docx4j.wml.BooleanDefaultTrue;23 importorg.docx4j.wml.Br;24 importorg.docx4j.wml.CTBackground;25 importorg.docx4j.wml.CTBorder;26 importorg.docx4j.wml.CTEm;27 importorg.docx4j.wml.CTHeight;28 importorg.docx4j.wml.CTLineNumber;29 importorg.docx4j.wml.CTShd;30 importorg.docx4j.wml.CTSignedHpsMeasure;31 importorg.docx4j.wml.CTSignedTwipsMeasure;32 importorg.docx4j.wml.CTTblCellMar;33 importorg.docx4j.wml.CTTextScale;34 importorg.docx4j.wml.CTVerticalAlignRun;35 importorg.docx4j.wml.CTVerticalJc;36 importorg.docx4j.wml.Color;37 importorg.docx4j.wml.ContentAccessor;38 importorg.docx4j.wml.Drawing;39 importorg.docx4j.wml.Highlight;40 importorg.docx4j.wml.HpsMeasure;41 importorg.docx4j.wml.Jc;42 importorg.docx4j.wml.JcEnumeration;43 importorg.docx4j.wml.ObjectFactory;44 importorg.docx4j.wml.P;45 importorg.docx4j.wml.P.Hyperlink;46 importorg.docx4j.wml.PPr;47 importorg.docx4j.wml.PPrBase.Ind;48 importorg.docx4j.wml.PPrBase.PBdr;49 importorg.docx4j.wml.PPrBase.Spacing;50 importorg.docx4j.wml.ParaRPr;51 importorg.docx4j.wml.R;52 importorg.docx4j.wml.RFonts;53 importorg.docx4j.wml.RPr;54 importorg.docx4j.wml.STBorder;55 importorg.docx4j.wml.STBrType;56 importorg.docx4j.wml.STEm;57 importorg.docx4j.wml.STLineNumberRestart;58 importorg.docx4j.wml.STLineSpacingRule;59 importorg.docx4j.wml.STPageOrientation;60 importorg.docx4j.wml.STShd;61 importorg.docx4j.wml.STVerticalAlignRun;62 importorg.docx4j.wml.STVerticalJc;63 importorg.docx4j.wml.SectPr;64 importorg.docx4j.wml.SectPr.PgBorders;65 importorg.docx4j.wml.SectPr.PgMar;66 importorg.docx4j.wml.SectPr.PgSz;67 importorg.docx4j.wml.SectPr.Type;68 importorg.docx4j.wml.Tbl;69 importorg.docx4j.wml.TblBorders;70 importorg.docx4j.wml.TblGrid;71 importorg.docx4j.wml.TblGridCol;72 importorg.docx4j.wml.TblPr;73 importorg.docx4j.wml.TblWidth;74 importorg.docx4j.wml.Tc;75 importorg.docx4j.wml.TcPr;76 importorg.docx4j.wml.TcPrInner.GridSpan;77 importorg.docx4j.wml.TcPrInner.HMerge;78 importorg.docx4j.wml.TcPrInner.VMerge;79 importorg.docx4j.wml.Text;80 importorg.docx4j.wml.TextDirection;81 importorg.docx4j.wml.Tr;82 importorg.docx4j.wml.TrPr;83 importorg.docx4j.wml.U;84 importorg.docx4j.wml.UnderlineEnumeration;85

86 //代码基于docx4j-3.2.0

87 public classDocx4j_工具类_S3_Test {88

89 /*------------------------------------other---------------------------------------------------*/

90 /**

91 * @Description:新增超链接92 */

93 public voidcreateHyperlink(WordprocessingMLPackage wordMLPackage, MainDocumentPart mainPart, ObjectFactory factory,94 P paragraph, String url, String value, String cnFontName, String enFontName, String fontSize)95 throwsException {96 if(StringUtils.isBlank(enFontName)) {97 enFontName = "Times New Roman";98 }99 if(StringUtils.isBlank(cnFontName)) {100 cnFontName = "微软雅黑";101 }102 if(StringUtils.isBlank(fontSize)) {103 fontSize = "22";104 }105 org.docx4j.relationships.ObjectFactory reFactory = neworg.docx4j.relationships.ObjectFactory();106 org.docx4j.relationships.Relationship rel =reFactory.createRelationship();107 rel.setType(Namespaces.HYPERLINK);108 rel.setTarget(url);109 rel.setTargetMode("External");110 mainPart.getRelationshipsPart().addRelationship(rel);111 StringBuffer sb = newStringBuffer();112 //addRelationship sets the rel's @Id

113 sb.append("");117 sb.append("");118 sb.append("");125 sb.append("");130 sb.append(value);131 sb.append("");132

133 Hyperlink link =(Hyperlink) XmlUtils.unmarshalString(sb.toString());134 paragraph.getContent().add(link);135 }136

137 public String getElementContent(Object obj) throwsException {138 StringWriter stringWriter = newStringWriter();139 TextUtils.extractText(obj, stringWriter);140 returnstringWriter.toString();141 }142

143 /**

144 * @Description:得到指定类型的元素145 */

146 public static List getAllElementFromObject(Object obj, Class>toSearch) {147 List result = new ArrayList();148 if (obj instanceofJAXBElement)149 obj = ((JAXBElement>) obj).getValue();150 if(obj.getClass().equals(toSearch))151 result.add(obj);152 else if (obj instanceofContentAccessor) {153 List> children =((ContentAccessor) obj).getContent();154 for(Object child : children) {155 result.addAll(getAllElementFromObject(child, toSearch));156 }157 }158 returnresult;159 }160

161 /**

162 * @Description:保存WordprocessingMLPackage163 */

164 public void saveWordPackage(WordprocessingMLPackage wordPackage, File file) throwsException {165 wordPackage.save(file);166 }167

168 /**

169 * @Description:新建WordprocessingMLPackage170 */

171 public WordprocessingMLPackage createWordprocessingMLPackage() throwsException {172 returnWordprocessingMLPackage.createPackage();173 }174

175 /**

176 * @Description:加载带密码WordprocessingMLPackage177 */

178 publicWordprocessingMLPackage loadWordprocessingMLPackageWithPwd(String filePath, String password)179 throwsException {180 OpcPackage opcPackage = WordprocessingMLPackage.load(newjava.io.File(filePath), password);181 WordprocessingMLPackage wordMLPackage =(WordprocessingMLPackage) opcPackage;182 returnwordMLPackage;183 }184

185 /**

186 * @Description:加载WordprocessingMLPackage187 */

188 public WordprocessingMLPackage loadWordprocessingMLPackage(String filePath) throwsException {189 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(newjava.io.File(filePath));190 returnwordMLPackage;191 }192

193 /*------------------------------------Word 表格相关---------------------------------------------------*/

194 /**

195 * @Description: 跨列合并196 */

197 public void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell, inttoCell) {198 if (row < 0 || fromCell < 0 || toCell < 0) {199 return;200 }201 List

trList =getTblAllTr(tbl);202 if (row >trList.size()) {203 return;204 }205 Tr tr =trList.get(row);206 List tcList =getTrAllCell(tr);207 for (int cellIndex = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) {208 Tc tc =tcList.get(cellIndex);209 TcPr tcPr =getTcPr(tc);210 if (cellIndex ==fromCell) {211 GridSpan gridSpan =tcPr.getGridSpan();212 if (gridSpan == null) {213 gridSpan = newGridSpan();214 tcPr.setGridSpan(gridSpan);215 }216 gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1, toCell) - fromCell + 1));217 } else{218 tr.getContent().remove(cellIndex);219 }220 }221 }222

223 /**

224 * @Description: 跨列合并225 */

226 public void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, inttoCell) {227 if (row < 0 || fromCell < 0 || toCell < 0) {228 return;229 }230 List

trList =getTblAllTr(tbl);231 if (row >trList.size()) {232 return;233 }234 Tr tr =trList.get(row);235 List tcList =getTrAllCell(tr);236 for (int cellIndex = fromCell, len = Math.min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) {237 Tc tc =tcList.get(cellIndex);238 TcPr tcPr =getTcPr(tc);239 HMerge hMerge =tcPr.getHMerge();240 if (hMerge == null) {241 hMerge = newHMerge();242 tcPr.setHMerge(hMerge);243 }244 if (cellIndex ==fromCell) {245 hMerge.setVal("restart");246 } else{247 hMerge.setVal("continue");248 }249 }250 }251

252 /**

253 * @Description: 跨行合并254 */

255 public void mergeCellsVertically(Tbl tbl, int col, int fromRow, inttoRow) {256 if (col < 0 || fromRow < 0 || toRow < 0) {257 return;258 }259 for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {260 Tc tc =getTc(tbl, rowIndex, col);261 if (tc == null) {262 break;263 }264 TcPr tcPr =getTcPr(tc);265 VMerge vMerge =tcPr.getVMerge();266 if (vMerge == null) {267 vMerge = newVMerge();268 tcPr.setVMerge(vMerge);269 }270 if (rowIndex ==fromRow) {271 vMerge.setVal("restart");272 } else{273 vMerge.setVal("continue");274 }275 }276 }277

278 /**

279 * @Description:得到指定位置的单元格280 */

281 public Tc getTc(Tbl tbl, int row, intcell) {282 if (row < 0 || cell < 0) {283 return null;284 }285 List

trList =getTblAllTr(tbl);286 if (row >=trList.size()) {287 return null;288 }289 List tcList =getTrAllCell(trList.get(row));290 if (cell >=tcList.size()) {291 return null;292 }293 returntcList.get(cell);294 }295

296 /**

297 * @Description:得到所有表格298 */

299 public ListgetAllTbl(WordprocessingMLPackage wordMLPackage) {300 MainDocumentPart mainDocPart =wordMLPackage.getMainDocumentPart();301 List objList = getAllElementFromObject(mainDocPart, Tbl.class);302 if (objList == null) {303 return null;304 }305 List tblList = new ArrayList();306 for(Object obj : objList) {307 if (obj instanceofTbl) {308 Tbl tbl =(Tbl) obj;309 tblList.add(tbl);310 }311 }312 returntblList;313 }314

315 /**

316 * @Description:删除指定位置的表格,删除后表格数量减一317 */

318 public boolean removeTableByIndex(WordprocessingMLPackage wordMLPackage, int index) throwsException {319 boolean flag = false;320 if (index < 0) {321 returnflag;322 }323 List objList =wordMLPackage.getMainDocumentPart().getContent();324 if (objList == null) {325 returnflag;326 }327 int k = -1;328 for (int i = 0, len = objList.size(); i < len; i++) {329 Object obj =XmlUtils.unwrap(objList.get(i));330 if (obj instanceofTbl) {331 k++;332 if (k ==index) {333 wordMLPackage.getMainDocumentPart().getContent().remove(i);334 flag = true;335 break;336 }337 }338 }339 returnflag;340 }341

342 /**

343 * @Description: 获取单元格内容,无分割符344 */

345 public String getTblContentStr(Tbl tbl) throwsException {346 returngetElementContent(tbl);347 }348

349 /**

350 * @Description: 获取表格内容351 */

352 public List getTblContentList(Tbl tbl) throwsException {353 List resultList = new ArrayList();354 List trList =getTblAllTr(tbl);355 for(Tr tr : trList) {356 StringBuffer sb = newStringBuffer();357 List tcList =getTrAllCell(tr);358 for(Tc tc : tcList) {359 sb.append(getElementContent(tc) + ",");360 }361 resultList.add(sb.toString());362 }363 returnresultList;364 }365

366 publicTblPr getTblPr(Tbl tbl) {367 TblPr tblPr =tbl.getTblPr();368 if (tblPr == null) {369 tblPr = newTblPr();370 tbl.setTblPr(tblPr);371 }372 returntblPr;373 }374

375 /**

376 * @Description: 设置表格总宽度377 */

378 public voidsetTableWidth(Tbl tbl, String width) {379 if(StringUtils.isNotBlank(width)) {380 TblPr tblPr =getTblPr(tbl);381 TblWidth tblW =tblPr.getTblW();382 if (tblW == null) {383 tblW = newTblWidth();384 tblPr.setTblW(tblW);385 }386 tblW.setW(newBigInteger(width));387 tblW.setType("dxa");388 }389 }390

391 /**

392 * @Description:创建表格(默认水平居中,垂直居中)393 */

394 public Tbl createTable(WordprocessingMLPackage wordPackage, int rowNum, int colsNum) throwsException {395 colsNum = Math.max(1, colsNum);396 rowNum = Math.max(1, rowNum);397 int widthTwips =getWritableWidth(wordPackage);398 int colWidth = widthTwips /colsNum;399 int[] widthArr = new int[colsNum];400 for (int i = 0; i < colsNum; i++) {401 widthArr[i] =colWidth;402 }403 returncreateTable(rowNum, colsNum, widthArr);404 }405

406 /**

407 * @Description:创建表格(默认水平居中,垂直居中)408 */

409 public Tbl createTable(int rowNum, int colsNum, int[] widthArr) throwsException {410 colsNum = Math.max(1, Math.min(colsNum, widthArr.length));411 rowNum = Math.max(1, rowNum);412 Tbl tbl = newTbl();413 StringBuffer tblSb = newStringBuffer();414 tblSb.append("");415 tblSb.append("");416 tblSb.append("");417 //上边框

418 tblSb.append("");419 tblSb.append("");420 //左边框

421 tblSb.append("");422 //下边框

423 tblSb.append("");424 //右边框

425 tblSb.append("");426 tblSb.append("");427 tblSb.append("");428 tblSb.append("");429 tblSb.append("");430 TblPr tblPr = null;431 tblPr =(TblPr) XmlUtils.unmarshalString(tblSb.toString());432 Jc jc = newJc();433 //单元格居中对齐

434 jc.setVal(JcEnumeration.CENTER);435 tblPr.setJc(jc);436

437 tbl.setTblPr(tblPr);438

439 //设定各单元格宽度

440 TblGrid tblGrid = newTblGrid();441 tbl.setTblGrid(tblGrid);442 for (int i = 0; i < colsNum; i++) {443 TblGridCol gridCol = newTblGridCol();444 gridCol.setW(BigInteger.valueOf(widthArr[i]));445 tblGrid.getGridCol().add(gridCol);446 }447 //新增行

448 for (int j = 0; j < rowNum; j++) {449 Tr tr = newTr();450 tbl.getContent().add(tr);451 //列

452 for (int i = 0; i < colsNum; i++) {453 Tc tc = newTc();454 tr.getContent().add(tc);455

456 TcPr tcPr = newTcPr();457 TblWidth cellWidth = newTblWidth();458 cellWidth.setType("dxa");459 cellWidth.setW(BigInteger.valueOf(widthArr[i]));460 tcPr.setTcW(cellWidth);461 tc.setTcPr(tcPr);462

463 //垂直居中

464 setTcVAlign(tc, STVerticalJc.CENTER);465 P p = newP();466 PPr pPr = newPPr();467 pPr.setJc(jc);468 p.setPPr(pPr);469 R run = newR();470 p.getContent().add(run);471 tc.getContent().add(p);472 }473 }474 returntbl;475 }476

477 /**

478 * @Description:表格增加边框 可以设置上下左右四个边框样式以及横竖水平线样式479 */

480 public voidsetTblBorders(TblPr tblPr, CTBorder topBorder, CTBorder rightBorder, CTBorder bottomBorder,481 CTBorder leftBorder, CTBorder hBorder, CTBorder vBorder) {482 TblBorders borders =tblPr.getTblBorders();483 if (borders == null) {484 borders = newTblBorders();485 tblPr.setTblBorders(borders);486 }487 if (topBorder != null) {488 borders.setTop(topBorder);489 }490 if (rightBorder != null) {491 borders.setRight(rightBorder);492 }493 if (bottomBorder != null) {494 borders.setBottom(bottomBorder);495 }496 if (leftBorder != null) {497 borders.setLeft(leftBorder);498 }499 if (hBorder != null) {500 borders.setInsideH(hBorder);501 }502 if (vBorder != null) {503 borders.setInsideV(vBorder);504 }505 }506

507 /**

508 * @Description: 设置表格水平对齐方式(仅对表格起作用,单元格不一定水平对齐)509 */

510 public voidsetTblJcAlign(Tbl tbl, JcEnumeration jcType) {511 if (jcType != null) {512 TblPr tblPr =getTblPr(tbl);513 Jc jc =tblPr.getJc();514 if (jc == null) {515 jc = newJc();516 tblPr.setJc(jc);517 }518 jc.setVal(jcType);519 }520 }521

522 /**

523 * @Description: 设置表格水平对齐方式(包括单元格),只对该方法前面产生的单元格起作用524 */

525 public voidsetTblAllJcAlign(Tbl tbl, JcEnumeration jcType) {526 if (jcType != null) {527 setTblJcAlign(tbl, jcType);528 List

trList =getTblAllTr(tbl);529 for(Tr tr : trList) {530 List tcList =getTrAllCell(tr);531 for(Tc tc : tcList) {532 setTcJcAlign(tc, jcType);533 }534 }535 }536 }537

538 /**

539 * @Description: 设置表格垂直对齐方式(包括单元格),只对该方法前面产生的单元格起作用540 */

541 public voidsetTblAllVAlign(Tbl tbl, STVerticalJc vAlignType) {542 if (vAlignType != null) {543 List

trList =getTblAllTr(tbl);544 for(Tr tr : trList) {545 List tcList =getTrAllCell(tr);546 for(Tc tc : tcList) {547 setTcVAlign(tc, vAlignType);548 }549 }550 }551 }552

553 /**

554 * @Description: 设置单元格Margin555 */

556 public voidsetTableCellMargin(Tbl tbl, String top, String right, String bottom, String left) {557 TblPr tblPr =getTblPr(tbl);558 CTTblCellMar cellMar =tblPr.getTblCellMar();559 if (cellMar == null) {560 cellMar = newCTTblCellMar();561 tblPr.setTblCellMar(cellMar);562 }563 if(StringUtils.isNotBlank(top)) {564 TblWidth topW = newTblWidth();565 topW.setW(newBigInteger(top));566 topW.setType("dxa");567 cellMar.setTop(topW);568 }569 if(StringUtils.isNotBlank(right)) {570 TblWidth rightW = newTblWidth();571 rightW.setW(newBigInteger(right));572 rightW.setType("dxa");573 cellMar.setRight(rightW);574 }575 if(StringUtils.isNotBlank(bottom)) {576 TblWidth btW = newTblWidth();577 btW.setW(newBigInteger(bottom));578 btW.setType("dxa");579 cellMar.setBottom(btW);580 }581 if(StringUtils.isNotBlank(left)) {582 TblWidth leftW = newTblWidth();583 leftW.setW(newBigInteger(left));584 leftW.setType("dxa");585 cellMar.setLeft(leftW);586 }587 }588

589 /**

590 * @Description: 得到表格所有的行591 */

592 public List

getTblAllTr(Tbl tbl) {593 List objList = getAllElementFromObject(tbl, Tr.class);594 List trList = new ArrayList();595 if (objList == null) {596 returntrList;597 }598 for(Object obj : objList) {599 if (obj instanceofTr) {600 Tr tr =(Tr) obj;601 trList.add(tr);602 }603 }604 returntrList;605

606 }607

608 /**

609 * @Description:设置tr高度610 */

611 public voidsetTrHeight(Tr tr, String heigth) {612 TrPr trPr =getTrPr(tr);613 CTHeight ctHeight = newCTHeight();614 ctHeight.setVal(newBigInteger(heigth));615 TrHeight trHeight = newTrHeight(ctHeight);616 trHeight.set(trPr);617 }618

619 /**

620 * @Description: 在表格指定位置新增一行,默认居中621 */

622 public void addTrByIndex(Tbl tbl, intindex) {623 addTrByIndex(tbl, index, STVerticalJc.CENTER, JcEnumeration.CENTER);624 }625

626 /**

627 * @Description: 在表格指定位置新增一行(默认按表格定义的列数添加)628 */

629 public void addTrByIndex(Tbl tbl, intindex, STVerticalJc vAlign, JcEnumeration hAlign) {630 TblGrid tblGrid =tbl.getTblGrid();631 Tr tr = newTr();632 if (tblGrid != null) {633 List gridList =tblGrid.getGridCol();634 for(TblGridCol tblGridCol : gridList) {635 Tc tc = newTc();636 setTcWidth(tc, tblGridCol.getW().toString());637 if (vAlign != null) {638 //垂直居中

639 setTcVAlign(tc, vAlign);640 }641 P p = newP();642 if (hAlign != null) {643 PPr pPr = newPPr();644 Jc jc = newJc();645 //单元格居中对齐

646 jc.setVal(hAlign);647 pPr.setJc(jc);648 p.setPPr(pPr);649 }650 R run = newR();651 p.getContent().add(run);652 tc.getContent().add(p);653 tr.getContent().add(tc);654 }655 } else{656 //大部分情况都不会走到这一步

657 Tr firstTr = getTblAllTr(tbl).get(0);658 int cellSize =getTcCellSizeWithMergeNum(firstTr);659 for (int i = 0; i < cellSize; i++) {660 Tc tc = newTc();661 if (vAlign != null) {662 //垂直居中

663 setTcVAlign(tc, vAlign);664 }665 P p = newP();666 if (hAlign != null) {667 PPr pPr = newPPr();668 Jc jc = newJc();669 //单元格居中对齐

670 jc.setVal(hAlign);671 pPr.setJc(jc);672 p.setPPr(pPr);673 }674 R run = newR();675 p.getContent().add(run);676 tc.getContent().add(p);677 tr.getContent().add(tc);678 }679 }680 if (index >= 0 && index

687 /**

688 * @Description: 得到行的列数689 */

690 public intgetTcCellSizeWithMergeNum(Tr tr) {691 int cellSize = 1;692 List tcList =getTrAllCell(tr);693 if (tcList == null || tcList.size() == 0) {694 returncellSize;695 }696 cellSize =tcList.size();697 for(Tc tc : tcList) {698 TcPr tcPr =getTcPr(tc);699 GridSpan gridSpan =tcPr.getGridSpan();700 if (gridSpan != null) {701 cellSize += gridSpan.getVal().intValue() - 1;702 }703 }704 returncellSize;705 }706

707 /**

708 * @Description: 删除指定行 删除后行数减一709 */

710 public boolean removeTrByIndex(Tbl tbl, intindex) {711 boolean flag = false;712 if (index < 0) {713 returnflag;714 }715 List objList =tbl.getContent();716 if (objList == null) {717 returnflag;718 }719 int k = -1;720 for (int i = 0, len = objList.size(); i < len; i++) {721 Object obj =XmlUtils.unwrap(objList.get(i));722 if (obj instanceofTr) {723 k++;724 if (k ==index) {725 tbl.getContent().remove(i);726 flag = true;727 break;728 }729 }730 }731 returnflag;732 }733

734 publicTrPr getTrPr(Tr tr) {735 TrPr trPr =tr.getTrPr();736 if (trPr == null) {737 trPr = newTrPr();738 tr.setTrPr(trPr);739 }740 returntrPr;741 }742

743 /**

744 * @Description:隐藏行(只对表格中间的部分起作用,不包括首尾行)745 */

746 public void setTrHidden(Tr tr, booleanhidden) {747 List tcList =getTrAllCell(tr);748 for(Tc tc : tcList) {749 setTcHidden(tc, hidden);750 }751 }752

753 /**

754 * @Description: 设置单元格宽度755 */

756 public voidsetTcWidth(Tc tc, String width) {757 if(StringUtils.isNotBlank(width)) {758 TcPr tcPr =getTcPr(tc);759 TblWidth tcW =tcPr.getTcW();760 if (tcW == null) {761 tcW = newTblWidth();762 tcPr.setTcW(tcW);763 }764 tcW.setW(newBigInteger(width));765 tcW.setType("dxa");766 }767 }768

769 /**

770 * @Description: 隐藏单元格内容771 */

772 public void setTcHidden(Tc tc, booleanhidden) {773 List

pList =getTcAllP(tc);774 for(P p : pList) {775 PPr ppr =getPPr(p);776 List objRList = getAllElementFromObject(p, R.class);777 if (objRList == null) {778 continue;779 }780 for(Object objR : objRList) {781 if (objR instanceofR) {782 R r =(R) objR;783 RPr rpr =getRPr(r);784 setRPrVanishStyle(rpr, hidden);785 }786 }787 setParaVanish(ppr, hidden);788 }789 }790

791 public List

getTcAllP(Tc tc) {792 List objList = getAllElementFromObject(tc, P.class);793 List

pList = new ArrayList

();794 if (objList == null) {795 returnpList;796 }797 for(Object obj : objList) {798 if (obj instanceofP) {799 P p =(P) obj;800 pList.add(p);801 }802 }803 returnpList;804 }805

806 publicTcPr getTcPr(Tc tc) {807 TcPr tcPr =tc.getTcPr();808 if (tcPr == null) {809 tcPr = newTcPr();810 tc.setTcPr(tcPr);811 }812 returntcPr;813 }814

815 /**

816 * @Description: 设置单元格垂直对齐方式817 */

818 public voidsetTcVAlign(Tc tc, STVerticalJc vAlignType) {819 if (vAlignType != null) {820 TcPr tcPr =getTcPr(tc);821 CTVerticalJc vAlign = newCTVerticalJc();822 vAlign.setVal(vAlignType);823 tcPr.setVAlign(vAlign);824 }825 }826

827 /**

828 * @Description: 设置单元格水平对齐方式829 */

830 public voidsetTcJcAlign(Tc tc, JcEnumeration jcType) {831 if (jcType != null) {832 List

pList =getTcAllP(tc);833 for(P p : pList) {834 setParaJcAlign(p, jcType);835 }836 }837 }838

839 publicRPr getRPr(R r) {840 RPr rpr =r.getRPr();841 if (rpr == null) {842 rpr = newRPr();843 r.setRPr(rpr);844 }845 returnrpr;846 }847

848 /**

849 * @Description: 获取所有的单元格850 */

851 public ListgetTrAllCell(Tr tr) {852 List objList = getAllElementFromObject(tr, Tc.class);853 List tcList = new ArrayList();854 if (objList == null) {855 returntcList;856 }857 for(Object tcObj : objList) {858 if (tcObj instanceofTc) {859 Tc objTc =(Tc) tcObj;860 tcList.add(objTc);861 }862 }863 returntcList;864 }865

866 /**

867 * @Description: 获取单元格内容868 */

869 public String getTcContent(Tc tc) throwsException {870 returngetElementContent(tc);871 }872

873 /**

874 * @Description:设置单元格内容,content为null则清除单元格内容875 */

876 public voidsetTcContent(Tc tc, RPr rpr, String content) {877 List pList =tc.getContent();878 P p = null;879 if (pList != null && pList.size() > 0) {880 if (pList.get(0) instanceofP) {881 p = (P) pList.get(0);882 }883 } else{884 p = newP();885 tc.getContent().add(p);886 }887 R run = null;888 List rList =p.getContent();889 if (rList != null && rList.size() > 0) {890 for (int i = 0, len = rList.size(); i < len; i++) {891 //清除内容(所有的r

892 p.getContent().remove(0);893 }894 }895 run = newR();896 p.getContent().add(run);897 if (content != null) {898 String[] contentArr = content.split("\n");899 Text text = newText();900 text.setSpace("preserve");901 text.setValue(contentArr[0]);902 run.setRPr(rpr);903 run.getContent().add(text);904

905 for (int i = 1, len = contentArr.length; i < len; i++) {906 Br br = newBr();907 run.getContent().add(br);//换行

908 text = newText();909 text.setSpace("preserve");910 text.setValue(contentArr[i]);911 run.setRPr(rpr);912 run.getContent().add(text);913 }914 }915 }916

917 /**

918 * @Description:设置单元格内容,content为null则清除单元格内容919 */

920 public voidremoveTcContent(Tc tc) {921 List pList =tc.getContent();922 P p = null;923 if (pList != null && pList.size() > 0) {924 if (pList.get(0) instanceofP) {925 p = (P) pList.get(0);926 }927 } else{928 return;929 }930 List rList =p.getContent();931 if (rList != null && rList.size() > 0) {932 for (int i = 0, len = rList.size(); i < len; i++) {933 //清除内容(所有的r

934 p.getContent().remove(0);935 }936 }937 }938

939 /**

940 * @Description:删除指定位置的表格941 *@deprecated

942 */

943 public void deleteTableByIndex2(WordprocessingMLPackage wordMLPackage, int index) throwsException {944 if (index < 0) {945 return;946 }947 final String xpath = "(//w:tbl)[" + index + "]";948 final List jaxbNodes = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(xpath, true);949 if (jaxbNodes != null && jaxbNodes.size() > 0) {950 wordMLPackage.getMainDocumentPart().getContent().remove(jaxbNodes.get(0));951 }952 }953

954 /**

955 * @Description:获取NodeList956 *@deprecated

957 */

958 public List getObjectByXpath(WordprocessingMLPackage wordMLPackage, String xpath) throwsException {959 final List jaxbNodes = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(xpath, true);960 returnjaxbNodes;961 }962

963 /*------------------------------------Word 段落相关---------------------------------------------------*/

964 /**

965 * @Description: 只删除单独的段落,不包括表格内或其他内的段落966 */

967 public boolean removeParaByIndex(WordprocessingMLPackage wordMLPackage, intindex) {968 boolean flag = false;969 if (index < 0) {970 returnflag;971 }972 List objList =wordMLPackage.getMainDocumentPart().getContent();973 if (objList == null) {974 returnflag;975 }976 int k = -1;977 for (int i = 0, len = objList.size(); i < len; i++) {978 if (objList.get(i) instanceofP) {979 k++;980 if (k ==index) {981 wordMLPackage.getMainDocumentPart().getContent().remove(i);982 flag = true;983 break;984 }985 }986 }987 returnflag;988 }989

990 /**

991 * @Description: 设置段落水平对齐方式992 */

993 public voidsetParaJcAlign(P paragraph, JcEnumeration hAlign) {994 if (hAlign != null) {995 PPr pprop =paragraph.getPPr();996 if (pprop == null) {997 pprop = newPPr();998 paragraph.setPPr(pprop);999 }1000 Jc align = newJc();1001 align.setVal(hAlign);1002 pprop.setJc(align);1003 }1004 }1005

1006 /**

1007 * @Description: 设置段落内容1008 */

1009 public voidsetParaRContent(P p, RPr runProperties, String content) {1010 R run = null;1011 List rList =p.getContent();1012 if (rList != null && rList.size() > 0) {1013 for (int i = 0, len = rList.size(); i < len; i++) {1014 //清除内容(所有的r

1015 p.getContent().remove(0);1016 }1017 }1018 run = newR();1019 p.getContent().add(run);1020 if (content != null) {1021 String[] contentArr = content.split("\n");1022 Text text = newText();1023 text.setSpace("preserve");1024 text.setValue(contentArr[0]);1025 run.setRPr(runProperties);1026 run.getContent().add(text);1027

1028 for (int i = 1, len = contentArr.length; i < len; i++) {1029 Br br = newBr();1030 run.getContent().add(br);//换行

1031 text = newText();1032 text.setSpace("preserve");1033 text.setValue(contentArr[i]);1034 run.setRPr(runProperties);1035 run.getContent().add(text);1036 }1037 }1038 }1039

1040 /**

1041 * @Description: 添加段落内容1042 */

1043 public voidappendParaRContent(P p, RPr runProperties, String content) {1044 if (content != null) {1045 R run = newR();1046 p.getContent().add(run);1047 String[] contentArr = content.split("\n");1048 Text text = newText();1049 text.setSpace("preserve");1050 text.setValue(contentArr[0]);1051 run.setRPr(runProperties);1052 run.getContent().add(text);1053

1054 for (int i = 1, len = contentArr.length; i < len; i++) {1055 Br br = newBr();1056 run.getContent().add(br);//换行

1057 text = newText();1058 text.setSpace("preserve");1059 text.setValue(contentArr[i]);1060 run.setRPr(runProperties);1061 run.getContent().add(text);1062 }1063 }1064 }1065

1066 /**

1067 * @Description: 添加图片到段落1068 */

1069 public voidaddImageToPara(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, P paragraph,1070 String filePath, String content, RPr rpr, String altText, int id1, int id2) throwsException {1071 R run =factory.createR();1072 if (content != null) {1073 Text text =factory.createText();1074 text.setValue(content);1075 text.setSpace("preserve");1076 run.setRPr(rpr);1077 run.getContent().add(text);1078 }1079

1080 InputStream is = newFileInputStream(filePath);1081 byte[] bytes =IOUtils.toByteArray(is);1082 BinaryPartAbstractImage imagePart =BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);1083 Inline inline = imagePart.createImageInline(filePath, altText, id1, id2, false);1084 Drawing drawing =factory.createDrawing();1085 drawing.getAnchorOrInline().add(inline);1086 run.getContent().add(drawing);1087 paragraph.getContent().add(run);1088 }1089

1090 /**

1091 * @Description: 段落添加Br 页面Break(分页符)1092 */

1093 public voidaddPageBreak(P para, STBrType sTBrType) {1094 Br breakObj = newBr();1095 breakObj.setType(sTBrType);1096 para.getContent().add(breakObj);1097 }1098

1099 /**

1100 * @Description: 设置段落是否禁止行号(禁止用于当前行号)1101 */

1102 public voidsetParagraphSuppressLineNum(P p) {1103 PPr ppr =getPPr(p);1104 BooleanDefaultTrue line =ppr.getSuppressLineNumbers();1105 if (line == null) {1106 line = newBooleanDefaultTrue();1107 }1108 line.setVal(true);1109 ppr.setSuppressLineNumbers(line);1110 }1111

1112 /**

1113 * @Description: 设置段落底纹(对整段文字起作用)1114 */

1115 public voidsetParagraphShdStyle(P p, STShd shdType, String shdColor) {1116 PPr ppr =getPPr(p);1117 CTShd ctShd =ppr.getShd();1118 if (ctShd == null) {1119 ctShd = newCTShd();1120 }1121 if(StringUtils.isNotBlank(shdColor)) {1122 ctShd.setColor(shdColor);1123 }1124 if (shdType != null) {1125 ctShd.setVal(shdType);1126 }1127 ppr.setShd(ctShd);1128 }1129

1130 /**

1131 *@paramisSpace1132 * 是否设置段前段后值1133 *@parambefore1134 * 段前磅数1135 *@paramafter1136 * 段后磅数1137 *@parambeforeLines1138 * 段前行数1139 *@paramafterLines1140 * 段后行数1141 *@paramisLine1142 * 是否设置行距1143 *@paramlineValue1144 * 行距值1145 *@paramsTLineSpacingRule1146 * 自动auto 固定exact 最小 atLeast 1磅=20 1行=100 单倍行距=2401147 */

1148 public void setParagraphSpacing(P p, booleanisSpace, String before, String after, String beforeLines,1149 String afterLines, booleanisLine, String lineValue, STLineSpacingRule sTLineSpacingRule) {1150 PPr pPr =getPPr(p);1151 Spacing spacing =pPr.getSpacing();1152 if (spacing == null) {1153 spacing = newSpacing();1154 pPr.setSpacing(spacing);1155 }1156 if(isSpace) {1157 if(StringUtils.isNotBlank(before)) {1158 //段前磅数

1159 spacing.setBefore(newBigInteger(before));1160 }1161 if(StringUtils.isNotBlank(after)) {1162 //段后磅数

1163 spacing.setAfter(newBigInteger(after));1164 }1165 if(StringUtils.isNotBlank(beforeLines)) {1166 //段前行数

1167 spacing.setBeforeLines(newBigInteger(beforeLines));1168 }1169 if(StringUtils.isNotBlank(afterLines)) {1170 //段后行数

1171 spacing.setAfterLines(newBigInteger(afterLines));1172 }1173 }1174 if(isLine) {1175 if(StringUtils.isNotBlank(lineValue)) {1176 spacing.setLine(newBigInteger(lineValue));1177 }1178 if (sTLineSpacingRule != null) {1179 spacing.setLineRule(sTLineSpacingRule);1180 }1181 }1182 }1183

1184 /**

1185 * @Description: 设置段落缩进信息 1厘米≈5671186 */

1187 public voidsetParagraphIndInfo(P p, String firstLine, String firstLineChar, String hanging, String hangingChar,1188 String right, String rigthChar, String left, String leftChar) {1189 PPr ppr =getPPr(p);1190 Ind ind =ppr.getInd();1191 if (ind == null) {1192 ind = newInd();1193 ppr.setInd(ind);1194 }1195 if(StringUtils.isNotBlank(firstLine)) {1196 ind.setFirstLine(newBigInteger(firstLine));1197 }1198 if(StringUtils.isNotBlank(firstLineChar)) {1199 ind.setFirstLineChars(newBigInteger(firstLineChar));1200 }1201 if(StringUtils.isNotBlank(hanging)) {1202 ind.setHanging(newBigInteger(hanging));1203 }1204 if(StringUtils.isNotBlank(hangingChar)) {1205 ind.setHangingChars(newBigInteger(hangingChar));1206 }1207 if(StringUtils.isNotBlank(left)) {1208 ind.setLeft(newBigInteger(left));1209 }1210 if(StringUtils.isNotBlank(leftChar)) {1211 ind.setLeftChars(newBigInteger(leftChar));1212 }1213 if(StringUtils.isNotBlank(right)) {1214 ind.setRight(newBigInteger(right));1215 }1216 if(StringUtils.isNotBlank(rigthChar)) {1217 ind.setRightChars(newBigInteger(rigthChar));1218 }1219 }1220

1221 publicPPr getPPr(P p) {1222 PPr ppr =p.getPPr();1223 if (ppr == null) {1224 ppr = newPPr();1225 p.setPPr(ppr);1226 }1227 returnppr;1228 }1229

1230 publicParaRPr getParaRPr(PPr ppr) {1231 ParaRPr parRpr =ppr.getRPr();1232 if (parRpr == null) {1233 parRpr = newParaRPr();1234 ppr.setRPr(parRpr);1235 }1236 returnparRpr;1237

1238 }1239

1240 public void setParaVanish(PPr ppr, booleanisVanish) {1241 ParaRPr parRpr =getParaRPr(ppr);1242 BooleanDefaultTrue vanish =parRpr.getVanish();1243 if (vanish != null) {1244 vanish.setVal(isVanish);1245 } else{1246 vanish = newBooleanDefaultTrue();1247 parRpr.setVanish(vanish);1248 vanish.setVal(isVanish);1249 }1250 }1251

1252 /**

1253 * @Description: 设置段落边框样式1254 */

1255 public voidsetParagraghBorders(P p, CTBorder topBorder, CTBorder bottomBorder, CTBorder leftBorder,1256 CTBorder rightBorder) {1257 PPr ppr =getPPr(p);1258 PBdr pBdr = newPBdr();1259 if (topBorder != null) {1260 pBdr.setTop(topBorder);1261 }1262 if (bottomBorder != null) {1263 pBdr.setBottom(bottomBorder);1264 }1265 if (leftBorder != null) {1266 pBdr.setLeft(leftBorder);1267 }1268 if (rightBorder != null) {1269 pBdr.setRight(rightBorder);1270 }1271 ppr.setPBdr(pBdr);1272 }1273

1274 /**

1275 * @Description: 设置字体信息1276 */

1277 public voidsetFontStyle(RPr runProperties, String cnFontFamily, String enFontFamily, String fontSize,1278 String color) {1279 setFontFamily(runProperties, cnFontFamily, enFontFamily);1280 setFontSize(runProperties, fontSize);1281 setFontColor(runProperties, color);1282 }1283

1284 /**

1285 * @Description: 设置字体大小1286 */

1287 public voidsetFontSize(RPr runProperties, String fontSize) {1288 if(StringUtils.isNotBlank(fontSize)) {1289 HpsMeasure size = newHpsMeasure();1290 size.setVal(newBigInteger(fontSize));1291 runProperties.setSz(size);1292 runProperties.setSzCs(size);1293 }1294 }1295

1296 /**

1297 * @Description: 设置字体1298 */

1299 public voidsetFontFamily(RPr runProperties, String cnFontFamily, String enFontFamily) {1300 if (StringUtils.isNotBlank(cnFontFamily) ||StringUtils.isNotBlank(enFontFamily)) {1301 RFonts rf =runProperties.getRFonts();1302 if (rf == null) {1303 rf = newRFonts();1304 runProperties.setRFonts(rf);1305 }1306 if (cnFontFamily != null) {1307 rf.setEastAsia(cnFontFamily);1308 }1309 if (enFontFamily != null) {1310 rf.setAscii(enFontFamily);1311 }1312 }1313 }1314

1315 /**

1316 * @Description: 设置字体颜色1317 */

1318 public voidsetFontColor(RPr runProperties, String color) {1319 if (color != null) {1320 Color c = newColor();1321 c.setVal(color);1322 runProperties.setColor(c);1323 }1324 }1325

1326 /**

1327 * @Description: 设置字符边框1328 */

1329 public voidaddRPrBorderStyle(RPr runProperties, String size, STBorder bordType, String space, String color) {1330 CTBorder value = newCTBorder();1331 if(StringUtils.isNotBlank(color)) {1332 value.setColor(color);1333 }1334 if(StringUtils.isNotBlank(size)) {1335 value.setSz(newBigInteger(size));1336 }1337 if(StringUtils.isNotBlank(space)) {1338 value.setSpace(newBigInteger(space));1339 }1340 if (bordType != null) {1341 value.setVal(bordType);1342 }1343 runProperties.setBdr(value);1344 }1345

1346 /**

1347 * @Description:着重号1348 */

1349 public voidaddRPrEmStyle(RPr runProperties, STEm emType) {1350 if (emType != null) {1351 CTEm em = newCTEm();1352 em.setVal(emType);1353 runProperties.setEm(em);1354 }1355 }1356

1357 /**

1358 * @Description: 空心1359 */

1360 public voidaddRPrOutlineStyle(RPr runProperties) {1361 BooleanDefaultTrue outline = newBooleanDefaultTrue();1362 outline.setVal(true);1363 runProperties.setOutline(outline);1364 }1365

1366 /**

1367 * @Description: 设置上标下标1368 */

1369 public voidaddRPrcaleStyle(RPr runProperties, STVerticalAlignRun vAlign) {1370 if (vAlign != null) {1371 CTVerticalAlignRun value = newCTVerticalAlignRun();1372 value.setVal(vAlign);1373 runProperties.setVertAlign(value);1374 }1375 }1376

1377 /**

1378 * @Description: 设置字符间距缩进1379 */

1380 public void addRPrScaleStyle(RPr runProperties, intindent) {1381 CTTextScale value = newCTTextScale();1382 value.setVal(indent);1383 runProperties.setW(value);1384 }1385

1386 /**

1387 * @Description: 设置字符间距信息1388 */

1389 public void addRPrtSpacingStyle(RPr runProperties, intspacing) {1390 CTSignedTwipsMeasure value = newCTSignedTwipsMeasure();1391 value.setVal(BigInteger.valueOf(spacing));1392 runProperties.setSpacing(value);1393 }1394

1395 /**

1396 * @Description: 设置文本位置1397 */

1398 public void addRPrtPositionStyle(RPr runProperties, intposition) {1399 CTSignedHpsMeasure ctPosition = newCTSignedHpsMeasure();1400 ctPosition.setVal(BigInteger.valueOf(position));1401 runProperties.setPosition(ctPosition);1402 }1403

1404 /**

1405 * @Description: 阴文1406 */

1407 public voidaddRPrImprintStyle(RPr runProperties) {1408 BooleanDefaultTrue imprint = newBooleanDefaultTrue();1409 imprint.setVal(true);1410 runProperties.setImprint(imprint);1411 }1412

1413 /**

1414 * @Description: 阳文1415 */

1416 public voidaddRPrEmbossStyle(RPr runProperties) {1417 BooleanDefaultTrue emboss = newBooleanDefaultTrue();1418 emboss.setVal(true);1419 runProperties.setEmboss(emboss);1420 }1421

1422 /**

1423 * @Description: 设置隐藏1424 */

1425 public void setRPrVanishStyle(RPr runProperties, booleanisVanish) {1426 BooleanDefaultTrue vanish =runProperties.getVanish();1427 if (vanish != null) {1428 vanish.setVal(isVanish);1429 } else{1430 vanish = newBooleanDefaultTrue();1431 vanish.setVal(isVanish);1432 runProperties.setVanish(vanish);1433 }1434 }1435

1436 /**

1437 * @Description: 设置阴影1438 */

1439 public voidaddRPrShadowStyle(RPr runProperties) {1440 BooleanDefaultTrue shadow = newBooleanDefaultTrue();1441 shadow.setVal(true);1442 runProperties.setShadow(shadow);1443 }1444

1445 /**

1446 * @Description: 设置底纹1447 */

1448 public voidaddRPrShdStyle(RPr runProperties, STShd shdtype) {1449 if (shdtype != null) {1450 CTShd shd = newCTShd();1451 shd.setVal(shdtype);1452 runProperties.setShd(shd);1453 }1454 }1455

1456 /**

1457 * @Description: 设置突出显示文本1458 */

1459 public voidaddRPrHightLightStyle(RPr runProperties, String hightlight) {1460 if(StringUtils.isNotBlank(hightlight)) {1461 Highlight highlight = newHighlight();1462 highlight.setVal(hightlight);1463 runProperties.setHighlight(highlight);1464 }1465 }1466

1467 /**

1468 * @Description: 设置删除线样式1469 */

1470 public void addRPrStrikeStyle(RPr runProperties, boolean isStrike, booleanisDStrike) {1471 //删除线

1472 if(isStrike) {1473 BooleanDefaultTrue strike = newBooleanDefaultTrue();1474 strike.setVal(true);1475 runProperties.setStrike(strike);1476 }1477 //双删除线

1478 if(isDStrike) {1479 BooleanDefaultTrue dStrike = newBooleanDefaultTrue();1480 dStrike.setVal(true);1481 runProperties.setDstrike(dStrike);1482 }1483 }1484

1485 /**

1486 * @Description: 加粗1487 */

1488 public voidaddRPrBoldStyle(RPr runProperties) {1489 BooleanDefaultTrue b = newBooleanDefaultTrue();1490 b.setVal(true);1491 runProperties.setB(b);1492 }1493

1494 /**

1495 * @Description: 倾斜1496 */

1497 public voidaddRPrItalicStyle(RPr runProperties) {1498 BooleanDefaultTrue b = newBooleanDefaultTrue();1499 b.setVal(true);1500 runProperties.setI(b);1501 }1502

1503 /**

1504 * @Description: 添加下划线1505 */

1506 public voidaddRPrUnderlineStyle(RPr runProperties, UnderlineEnumeration enumType) {1507 U val = newU();1508 val.setVal(enumType);1509 runProperties.setU(val);1510 }1511

1512 /*------------------------------------Word 相关---------------------------------------------------*/

1513 /**

1514 * @Description: 设置分节符 nextPage:下一页 continuous:连续 evenPage:偶数页 oddPage:奇数页1515 */

1516 public voidsetDocSectionBreak(WordprocessingMLPackage wordPackage, String sectValType) {1517 if(StringUtils.isNotBlank(sectValType)) {1518 SectPr sectPr =getDocSectPr(wordPackage);1519 Type sectType =sectPr.getType();1520 if (sectType == null) {1521 sectType = newType();1522 sectPr.setType(sectType);1523 }1524 sectType.setVal(sectValType);1525 }1526 }1527

1528 /**

1529 * @Description: 设置页面背景色1530 */

1531 public voidsetDocumentBackGround(WordprocessingMLPackage wordPackage, ObjectFactory factory, String color)1532 throwsException {1533 MainDocumentPart mdp =wordPackage.getMainDocumentPart();1534 CTBackground bkground =mdp.getContents().getBackground();1535 if(StringUtils.isNotBlank(color)) {1536 if (bkground == null) {1537 bkground =factory.createCTBackground();1538 bkground.setColor(color);1539 }1540 mdp.getContents().setBackground(bkground);1541 }1542 }1543

1544 /**

1545 * @Description: 设置页面边框1546 */

1547 public voidsetDocumentBorders(WordprocessingMLPackage wordPackage, ObjectFactory factory, CTBorder top,1548 CTBorder right, CTBorder bottom, CTBorder left) {1549 SectPr sectPr =getDocSectPr(wordPackage);1550 PgBorders pgBorders =sectPr.getPgBorders();1551 if (pgBorders == null) {1552 pgBorders =factory.createSectPrPgBorders();1553 sectPr.setPgBorders(pgBorders);1554 }1555 if (top != null) {1556 pgBorders.setTop(top);1557 }1558 if (right != null) {1559 pgBorders.setRight(right);1560 }1561 if (bottom != null) {1562 pgBorders.setBottom(bottom);1563 }1564 if (left != null) {1565 pgBorders.setLeft(left);1566 }1567 }1568

1569 /**

1570 * @Description: 设置页面大小及纸张方向 landscape横向1571 */

1572 public voidsetDocumentSize(WordprocessingMLPackage wordPackage, ObjectFactory factory, String width, String height,1573 STPageOrientation stValue) {1574 SectPr sectPr =getDocSectPr(wordPackage);1575 PgSz pgSz =sectPr.getPgSz();1576 if (pgSz == null) {1577 pgSz =factory.createSectPrPgSz();1578 sectPr.setPgSz(pgSz);1579 }1580 if(StringUtils.isNotBlank(width)) {1581 pgSz.setW(newBigInteger(width));1582 }1583 if(StringUtils.isNotBlank(height)) {1584 pgSz.setH(newBigInteger(height));1585 }1586 if (stValue != null) {1587 pgSz.setOrient(stValue);1588 }1589 }1590

1591 publicSectPr getDocSectPr(WordprocessingMLPackage wordPackage) {1592 SectPr sectPr = wordPackage.getDocumentModel().getSections().get(0).getSectPr();1593 returnsectPr;1594 }1595

1596 /**

1597 * @Description:设置页边距1598 */

1599 public voidsetDocMarginSpace(WordprocessingMLPackage wordPackage, ObjectFactory factory, String top, String left,1600 String bottom, String right) {1601 SectPr sectPr =getDocSectPr(wordPackage);1602 PgMar pg =sectPr.getPgMar();1603 if (pg == null) {1604 pg =factory.createSectPrPgMar();1605 sectPr.setPgMar(pg);1606 }1607 if(StringUtils.isNotBlank(top)) {1608 pg.setTop(newBigInteger(top));1609 }1610 if(StringUtils.isNotBlank(bottom)) {1611 pg.setBottom(newBigInteger(bottom));1612 }1613 if(StringUtils.isNotBlank(left)) {1614 pg.setLeft(newBigInteger(left));1615 }1616 if(StringUtils.isNotBlank(right)) {1617 pg.setRight(newBigInteger(right));1618 }1619 }1620

1621 /**

1622 * @Description: 设置行号1623 *@paramdistance1624 * :距正文距离 1厘米=5671625 *@paramstart1626 * :起始编号(0开始)1627 *@paramcountBy1628 * :行号间隔1629 *@paramrestartType1630 * :STLineNumberRestart.CONTINUOUS(continuous连续编号)
1631 * STLineNumberRestart.NEW_PAGE(每页重新编号)
1632 * STLineNumberRestart.NEW_SECTION(每节重新编号)1633 */

1634 public voidsetDocInNumType(WordprocessingMLPackage wordPackage, String countBy, String distance, String start,1635 STLineNumberRestart restartType) {1636 SectPr sectPr =getDocSectPr(wordPackage);1637 CTLineNumber lnNumType =sectPr.getLnNumType();1638 if (lnNumType == null) {1639 lnNumType = newCTLineNumber();1640 sectPr.setLnNumType(lnNumType);1641 }1642 if(StringUtils.isNotBlank(countBy)) {1643 lnNumType.setCountBy(newBigInteger(countBy));1644 }1645 if(StringUtils.isNotBlank(distance)) {1646 lnNumType.setDistance(newBigInteger(distance));1647 }1648 if(StringUtils.isNotBlank(start)) {1649 lnNumType.setStart(newBigInteger(start));1650 }1651 if (restartType != null) {1652 lnNumType.setRestart(restartType);1653 }1654 }1655

1656 /**

1657 * @Description:设置文字方向 tbRl 垂直1658 */

1659 public voidsetDocTextDirection(WordprocessingMLPackage wordPackage, String textDirection) {1660 if(StringUtils.isNotBlank(textDirection)) {1661 SectPr sectPr =getDocSectPr(wordPackage);1662 TextDirection textDir =sectPr.getTextDirection();1663 if (textDir == null) {1664 textDir = newTextDirection();1665 sectPr.setTextDirection(textDir);1666 }1667 textDir.setVal(textDirection);1668 }1669 }1670

1671 /**

1672 * @Description:设置word 垂直对齐方式(Word默认方式都是"顶端对齐")1673 */

1674 public voidsetDocVAlign(WordprocessingMLPackage wordPackage, STVerticalJc valignType) {1675 if (valignType != null) {1676 SectPr sectPr =getDocSectPr(wordPackage);1677 CTVerticalJc valign =sectPr.getVAlign();1678 if (valign == null) {1679 valign = newCTVerticalJc();1680 sectPr.setVAlign(valign);1681 }1682 valign.setVal(valignType);1683 }1684 }1685

1686 /**

1687 * @Description:获取文档的可用宽度1688 */

1689 public int getWritableWidth(WordprocessingMLPackage wordPackage) throwsException {1690 return wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();1691 }1692

1693 }

 类似资料: