1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.InputStream; 4 import java.io.StringWriter; 5 import java.math.BigInteger; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import javax.xml.bind.JAXBElement; 10 11 import org.apache.commons.io.IOUtils; 12 import org.apache.commons.lang3.StringUtils; 13 import org.docx4j.TextUtils; 14 import org.docx4j.XmlUtils; 15 import org.docx4j.dml.wordprocessingDrawing.Inline; 16 import org.docx4j.model.properties.table.tr.TrHeight; 17 import org.docx4j.openpackaging.packages.OpcPackage; 18 import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 19 import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; 20 import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; 21 import org.docx4j.openpackaging.parts.relationships.Namespaces; 22 import org.docx4j.wml.BooleanDefaultTrue; 23 import org.docx4j.wml.Br; 24 import org.docx4j.wml.CTBackground; 25 import org.docx4j.wml.CTBorder; 26 import org.docx4j.wml.CTEm; 27 import org.docx4j.wml.CTHeight; 28 import org.docx4j.wml.CTLineNumber; 29 import org.docx4j.wml.CTShd; 30 import org.docx4j.wml.CTSignedHpsMeasure; 31 import org.docx4j.wml.CTSignedTwipsMeasure; 32 import org.docx4j.wml.CTTblCellMar; 33 import org.docx4j.wml.CTTextScale; 34 import org.docx4j.wml.CTVerticalAlignRun; 35 import org.docx4j.wml.CTVerticalJc; 36 import org.docx4j.wml.Color; 37 import org.docx4j.wml.ContentAccessor; 38 import org.docx4j.wml.Drawing; 39 import org.docx4j.wml.Highlight; 40 import org.docx4j.wml.HpsMeasure; 41 import org.docx4j.wml.Jc; 42 import org.docx4j.wml.JcEnumeration; 43 import org.docx4j.wml.ObjectFactory; 44 import org.docx4j.wml.P; 45 import org.docx4j.wml.P.Hyperlink; 46 import org.docx4j.wml.PPr; 47 import org.docx4j.wml.PPrBase.Ind; 48 import org.docx4j.wml.PPrBase.PBdr; 49 import org.docx4j.wml.PPrBase.Spacing; 50 import org.docx4j.wml.ParaRPr; 51 import org.docx4j.wml.R; 52 import org.docx4j.wml.RFonts; 53 import org.docx4j.wml.RPr; 54 import org.docx4j.wml.STBorder; 55 import org.docx4j.wml.STBrType; 56 import org.docx4j.wml.STEm; 57 import org.docx4j.wml.STLineNumberRestart; 58 import org.docx4j.wml.STLineSpacingRule; 59 import org.docx4j.wml.STPageOrientation; 60 import org.docx4j.wml.STShd; 61 import org.docx4j.wml.STVerticalAlignRun; 62 import org.docx4j.wml.STVerticalJc; 63 import org.docx4j.wml.SectPr; 64 import org.docx4j.wml.SectPr.PgBorders; 65 import org.docx4j.wml.SectPr.PgMar; 66 import org.docx4j.wml.SectPr.PgSz; 67 import org.docx4j.wml.SectPr.Type; 68 import org.docx4j.wml.Tbl; 69 import org.docx4j.wml.TblBorders; 70 import org.docx4j.wml.TblGrid; 71 import org.docx4j.wml.TblGridCol; 72 import org.docx4j.wml.TblPr; 73 import org.docx4j.wml.TblWidth; 74 import org.docx4j.wml.Tc; 75 import org.docx4j.wml.TcPr; 76 import org.docx4j.wml.TcPrInner.GridSpan; 77 import org.docx4j.wml.TcPrInner.HMerge; 78 import org.docx4j.wml.TcPrInner.VMerge; 79 import org.docx4j.wml.Text; 80 import org.docx4j.wml.TextDirection; 81 import org.docx4j.wml.Tr; 82 import org.docx4j.wml.TrPr; 83 import org.docx4j.wml.U; 84 import org.docx4j.wml.UnderlineEnumeration; 85 86 //代码基于docx4j-3.2.0 87 public class Docx4j_工具类_S3_Test { 88 89 /*------------------------------------other--------------------------------------------------- */ 90 /** 91 * @Description:新增超链接 92 */ 93 public void createHyperlink(WordprocessingMLPackage wordMLPackage, MainDocumentPart mainPart, ObjectFactory factory, 94 P paragraph, String url, String value, String cnFontName, String enFontName, String fontSize) 95 throws Exception { 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 = new org.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 = new StringBuffer(); 112 // addRelationship sets the rel's @Id 113 sb.append("<w:hyperlink r:id=\""); 114 sb.append(rel.getId()); 115 sb.append("\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" "); 116 sb.append("xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >"); 117 sb.append("<w:r><w:rPr><w:rStyle w:val=\"Hyperlink\" />"); 118 sb.append("<w:rFonts w:ascii=\""); 119 sb.append(enFontName); 120 sb.append("\" w:hAnsi=\""); 121 sb.append(enFontName); 122 sb.append("\" w:eastAsia=\""); 123 sb.append(cnFontName); 124 sb.append("\" w:hint=\"eastAsia\"/>"); 125 sb.append("<w:sz w:val=\""); 126 sb.append(fontSize); 127 sb.append("\"/><w:szCs w:val=\""); 128 sb.append(fontSize); 129 sb.append("\"/></w:rPr><w:t>"); 130 sb.append(value); 131 sb.append("</w:t></w:r></w:hyperlink>"); 132 133 Hyperlink link = (Hyperlink) XmlUtils.unmarshalString(sb.toString()); 134 paragraph.getContent().add(link); 135 } 136 137 public String getElementContent(Object obj) throws Exception { 138 StringWriter stringWriter = new StringWriter(); 139 TextUtils.extractText(obj, stringWriter); 140 return stringWriter.toString(); 141 } 142 143 /** 144 * @Description:得到指定类型的元素 145 */ 146 public static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) { 147 List<Object> result = new ArrayList<Object>(); 148 if (obj instanceof JAXBElement) 149 obj = ((JAXBElement<?>) obj).getValue(); 150 if (obj.getClass().equals(toSearch)) 151 result.add(obj); 152 else if (obj instanceof ContentAccessor) { 153 List<?> children = ((ContentAccessor) obj).getContent(); 154 for (Object child : children) { 155 result.addAll(getAllElementFromObject(child, toSearch)); 156 } 157 } 158 return result; 159 } 160 161 /** 162 * @Description:保存WordprocessingMLPackage 163 */ 164 public void saveWordPackage(WordprocessingMLPackage wordPackage, File file) throws Exception { 165 wordPackage.save(file); 166 } 167 168 /** 169 * @Description:新建WordprocessingMLPackage 170 */ 171 public WordprocessingMLPackage createWordprocessingMLPackage() throws Exception { 172 return WordprocessingMLPackage.createPackage(); 173 } 174 175 /** 176 * @Description:加载带密码WordprocessingMLPackage 177 */ 178 public WordprocessingMLPackage loadWordprocessingMLPackageWithPwd(String filePath, String password) 179 throws Exception { 180 OpcPackage opcPackage = WordprocessingMLPackage.load(new java.io.File(filePath), password); 181 WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage) opcPackage; 182 return wordMLPackage; 183 } 184 185 /** 186 * @Description:加载WordprocessingMLPackage 187 */ 188 public WordprocessingMLPackage loadWordprocessingMLPackage(String filePath) throws Exception { 189 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filePath)); 190 return wordMLPackage; 191 } 192 193 /*------------------------------------Word 表格相关--------------------------------------------------- */ 194 /** 195 * @Description: 跨列合并 196 */ 197 public void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell, int toCell) { 198 if (row < 0 || fromCell < 0 || toCell < 0) { 199 return; 200 } 201 List<Tr> trList = getTblAllTr(tbl); 202 if (row > trList.size()) { 203 return; 204 } 205 Tr tr = trList.get(row); 206 List<Tc> 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 = new GridSpan(); 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, int toCell) { 227 if (row < 0 || fromCell < 0 || toCell < 0) { 228 return; 229 } 230 List<Tr> trList = getTblAllTr(tbl); 231 if (row > trList.size()) { 232 return; 233 } 234 Tr tr = trList.get(row); 235 List<Tc> 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 = new HMerge(); 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, int toRow) { 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 = new VMerge(); 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, int cell) { 282 if (row < 0 || cell < 0) { 283 return null; 284 } 285 List<Tr> trList = getTblAllTr(tbl); 286 if (row >= trList.size()) { 287 return null; 288 } 289 List<Tc> tcList = getTrAllCell(trList.get(row)); 290 if (cell >= tcList.size()) { 291 return null; 292 } 293 return tcList.get(cell); 294 } 295 296 /** 297 * @Description:得到所有表格 298 */ 299 public List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) { 300 MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart(); 301 List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class); 302 if (objList == null) { 303 return null; 304 } 305 List<Tbl> tblList = new ArrayList<Tbl>(); 306 for (Object obj : objList) { 307 if (obj instanceof Tbl) { 308 Tbl tbl = (Tbl) obj; 309 tblList.add(tbl); 310 } 311 } 312 return tblList; 313 } 314 315 /** 316 * @Description:删除指定位置的表格,删除后表格数量减一 317 */ 318 public boolean removeTableByIndex(WordprocessingMLPackage wordMLPackage, int index) throws Exception { 319 boolean flag = false; 320 if (index < 0) { 321 return flag; 322 } 323 List<Object> objList = wordMLPackage.getMainDocumentPart().getContent(); 324 if (objList == null) { 325 return flag; 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 instanceof Tbl) { 331 k++; 332 if (k == index) { 333 wordMLPackage.getMainDocumentPart().getContent().remove(i); 334 flag = true; 335 break; 336 } 337 } 338 } 339 return flag; 340 } 341 342 /** 343 * @Description: 获取单元格内容,无分割符 344 */ 345 public String getTblContentStr(Tbl tbl) throws Exception { 346 return getElementContent(tbl); 347 } 348 349 /** 350 * @Description: 获取表格内容 351 */ 352 public List<String> getTblContentList(Tbl tbl) throws Exception { 353 List<String> resultList = new ArrayList<String>(); 354 List<Tr> trList = getTblAllTr(tbl); 355 for (Tr tr : trList) { 356 StringBuffer sb = new StringBuffer(); 357 List<Tc> tcList = getTrAllCell(tr); 358 for (Tc tc : tcList) { 359 sb.append(getElementContent(tc) + ","); 360 } 361 resultList.add(sb.toString()); 362 } 363 return resultList; 364 } 365 366 public TblPr getTblPr(Tbl tbl) { 367 TblPr tblPr = tbl.getTblPr(); 368 if (tblPr == null) { 369 tblPr = new TblPr(); 370 tbl.setTblPr(tblPr); 371 } 372 return tblPr; 373 } 374 375 /** 376 * @Description: 设置表格总宽度 377 */ 378 public void setTableWidth(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 = new TblWidth(); 384 tblPr.setTblW(tblW); 385 } 386 tblW.setW(new BigInteger(width)); 387 tblW.setType("dxa"); 388 } 389 } 390 391 /** 392 * @Description:创建表格(默认水平居中,垂直居中) 393 */ 394 public Tbl createTable(WordprocessingMLPackage wordPackage, int rowNum, int colsNum) throws Exception { 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 return createTable(rowNum, colsNum, widthArr); 404 } 405 406 /** 407 * @Description:创建表格(默认水平居中,垂直居中) 408 */ 409 public Tbl createTable(int rowNum, int colsNum, int[] widthArr) throws Exception { 410 colsNum = Math.max(1, Math.min(colsNum, widthArr.length)); 411 rowNum = Math.max(1, rowNum); 412 Tbl tbl = new Tbl(); 413 StringBuffer tblSb = new StringBuffer(); 414 tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION).append(">"); 415 tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>"); 416 tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>"); 417 // 上边框 418 tblSb.append("<w:tblBorders>"); 419 tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>"); 420 // 左边框 421 tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>"); 422 // 下边框 423 tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>"); 424 // 右边框 425 tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>"); 426 tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>"); 427 tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>"); 428 tblSb.append("</w:tblBorders>"); 429 tblSb.append("</w:tblPr>"); 430 TblPr tblPr = null; 431 tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString()); 432 Jc jc = new Jc(); 433 // 单元格居中对齐 434 jc.setVal(JcEnumeration.CENTER); 435 tblPr.setJc(jc); 436 437 tbl.setTblPr(tblPr); 438 439 // 设定各单元格宽度 440 TblGrid tblGrid = new TblGrid(); 441 tbl.setTblGrid(tblGrid); 442 for (int i = 0; i < colsNum; i++) { 443 TblGridCol gridCol = new TblGridCol(); 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 = new Tr(); 450 tbl.getContent().add(tr); 451 // 列 452 for (int i = 0; i < colsNum; i++) { 453 Tc tc = new Tc(); 454 tr.getContent().add(tc); 455 456 TcPr tcPr = new TcPr(); 457 TblWidth cellWidth = new TblWidth(); 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 = new P(); 466 PPr pPr = new PPr(); 467 pPr.setJc(jc); 468 p.setPPr(pPr); 469 R run = new R(); 470 p.getContent().add(run); 471 tc.getContent().add(p); 472 } 473 } 474 return tbl; 475 } 476 477 /** 478 * @Description:表格增加边框 可以设置上下左右四个边框样式以及横竖水平线样式 479 */ 480 public void setTblBorders(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 = new TblBorders(); 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 void setTblJcAlign(Tbl tbl, JcEnumeration jcType) { 511 if (jcType != null) { 512 TblPr tblPr = getTblPr(tbl); 513 Jc jc = tblPr.getJc(); 514 if (jc == null) { 515 jc = new Jc(); 516 tblPr.setJc(jc); 517 } 518 jc.setVal(jcType); 519 } 520 } 521 522 /** 523 * @Description: 设置表格水平对齐方式(包括单元格),只对该方法前面产生的单元格起作用 524 */ 525 public void setTblAllJcAlign(Tbl tbl, JcEnumeration jcType) { 526 if (jcType != null) { 527 setTblJcAlign(tbl, jcType); 528 List<Tr> trList = getTblAllTr(tbl); 529 for (Tr tr : trList) { 530 List<Tc> tcList = getTrAllCell(tr); 531 for (Tc tc : tcList) { 532 setTcJcAlign(tc, jcType); 533 } 534 } 535 } 536 } 537 538 /** 539 * @Description: 设置表格垂直对齐方式(包括单元格),只对该方法前面产生的单元格起作用 540 */ 541 public void setTblAllVAlign(Tbl tbl, STVerticalJc vAlignType) { 542 if (vAlignType != null) { 543 List<Tr> trList = getTblAllTr(tbl); 544 for (Tr tr : trList) { 545 List<Tc> tcList = getTrAllCell(tr); 546 for (Tc tc : tcList) { 547 setTcVAlign(tc, vAlignType); 548 } 549 } 550 } 551 } 552 553 /** 554 * @Description: 设置单元格Margin 555 */ 556 public void setTableCellMargin(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 = new CTTblCellMar(); 561 tblPr.setTblCellMar(cellMar); 562 } 563 if (StringUtils.isNotBlank(top)) { 564 TblWidth topW = new TblWidth(); 565 topW.setW(new BigInteger(top)); 566 topW.setType("dxa"); 567 cellMar.setTop(topW); 568 } 569 if (StringUtils.isNotBlank(right)) { 570 TblWidth rightW = new TblWidth(); 571 rightW.setW(new BigInteger(right)); 572 rightW.setType("dxa"); 573 cellMar.setRight(rightW); 574 } 575 if (StringUtils.isNotBlank(bottom)) { 576 TblWidth btW = new TblWidth(); 577 btW.setW(new BigInteger(bottom)); 578 btW.setType("dxa"); 579 cellMar.setBottom(btW); 580 } 581 if (StringUtils.isNotBlank(left)) { 582 TblWidth leftW = new TblWidth(); 583 leftW.setW(new BigInteger(left)); 584 leftW.setType("dxa"); 585 cellMar.setLeft(leftW); 586 } 587 } 588 589 /** 590 * @Description: 得到表格所有的行 591 */ 592 public List<Tr> getTblAllTr(Tbl tbl) { 593 List<Object> objList = getAllElementFromObject(tbl, Tr.class); 594 List<Tr> trList = new ArrayList<Tr>(); 595 if (objList == null) { 596 return trList; 597 } 598 for (Object obj : objList) { 599 if (obj instanceof Tr) { 600 Tr tr = (Tr) obj; 601 trList.add(tr); 602 } 603 } 604 return trList; 605 606 } 607 608 /** 609 * @Description:设置tr高度 610 */ 611 public void setTrHeight(Tr tr, String heigth) { 612 TrPr trPr = getTrPr(tr); 613 CTHeight ctHeight = new CTHeight(); 614 ctHeight.setVal(new BigInteger(heigth)); 615 TrHeight trHeight = new TrHeight(ctHeight); 616 trHeight.set(trPr); 617 } 618 619 /** 620 * @Description: 在表格指定位置新增一行,默认居中 621 */ 622 public void addTrByIndex(Tbl tbl, int index) { 623 addTrByIndex(tbl, index, STVerticalJc.CENTER, JcEnumeration.CENTER); 624 } 625 626 /** 627 * @Description: 在表格指定位置新增一行(默认按表格定义的列数添加) 628 */ 629 public void addTrByIndex(Tbl tbl, int index, STVerticalJc vAlign, JcEnumeration hAlign) { 630 TblGrid tblGrid = tbl.getTblGrid(); 631 Tr tr = new Tr(); 632 if (tblGrid != null) { 633 List<TblGridCol> gridList = tblGrid.getGridCol(); 634 for (TblGridCol tblGridCol : gridList) { 635 Tc tc = new Tc(); 636 setTcWidth(tc, tblGridCol.getW().toString()); 637 if (vAlign != null) { 638 // 垂直居中 639 setTcVAlign(tc, vAlign); 640 } 641 P p = new P(); 642 if (hAlign != null) { 643 PPr pPr = new PPr(); 644 Jc jc = new Jc(); 645 // 单元格居中对齐 646 jc.setVal(hAlign); 647 pPr.setJc(jc); 648 p.setPPr(pPr); 649 } 650 R run = new R(); 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 = new Tc(); 661 if (vAlign != null) { 662 // 垂直居中 663 setTcVAlign(tc, vAlign); 664 } 665 P p = new P(); 666 if (hAlign != null) { 667 PPr pPr = new PPr(); 668 Jc jc = new Jc(); 669 // 单元格居中对齐 670 jc.setVal(hAlign); 671 pPr.setJc(jc); 672 p.setPPr(pPr); 673 } 674 R run = new R(); 675 p.getContent().add(run); 676 tc.getContent().add(p); 677 tr.getContent().add(tc); 678 } 679 } 680 if (index >= 0 && index < tbl.getContent().size()) { 681 tbl.getContent().add(index, tr); 682 } else { 683 tbl.getContent().add(tr); 684 } 685 } 686 687 /** 688 * @Description: 得到行的列数 689 */ 690 public int getTcCellSizeWithMergeNum(Tr tr) { 691 int cellSize = 1; 692 List<Tc> tcList = getTrAllCell(tr); 693 if (tcList == null || tcList.size() == 0) { 694 return cellSize; 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 return cellSize; 705 } 706 707 /** 708 * @Description: 删除指定行 删除后行数减一 709 */ 710 public boolean removeTrByIndex(Tbl tbl, int index) { 711 boolean flag = false; 712 if (index < 0) { 713 return flag; 714 } 715 List<Object> objList = tbl.getContent(); 716 if (objList == null) { 717 return flag; 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 instanceof Tr) { 723 k++; 724 if (k == index) { 725 tbl.getContent().remove(i); 726 flag = true; 727 break; 728 } 729 } 730 } 731 return flag; 732 } 733 734 public TrPr getTrPr(Tr tr) { 735 TrPr trPr = tr.getTrPr(); 736 if (trPr == null) { 737 trPr = new TrPr(); 738 tr.setTrPr(trPr); 739 } 740 return trPr; 741 } 742 743 /** 744 * @Description:隐藏行(只对表格中间的部分起作用,不包括首尾行) 745 */ 746 public void setTrHidden(Tr tr, boolean hidden) { 747 List<Tc> tcList = getTrAllCell(tr); 748 for (Tc tc : tcList) { 749 setTcHidden(tc, hidden); 750 } 751 } 752 753 /** 754 * @Description: 设置单元格宽度 755 */ 756 public void setTcWidth(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 = new TblWidth(); 762 tcPr.setTcW(tcW); 763 } 764 tcW.setW(new BigInteger(width)); 765 tcW.setType("dxa"); 766 } 767 } 768 769 /** 770 * @Description: 隐藏单元格内容 771 */ 772 public void setTcHidden(Tc tc, boolean hidden) { 773 List<P> pList = getTcAllP(tc); 774 for (P p : pList) { 775 PPr ppr = getPPr(p); 776 List<Object> objRList = getAllElementFromObject(p, R.class); 777 if (objRList == null) { 778 continue; 779 } 780 for (Object objR : objRList) { 781 if (objR instanceof R) { 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<P> getTcAllP(Tc tc) { 792 List<Object> objList = getAllElementFromObject(tc, P.class); 793 List<P> pList = new ArrayList<P>(); 794 if (objList == null) { 795 return pList; 796 } 797 for (Object obj : objList) { 798 if (obj instanceof P) { 799 P p = (P) obj; 800 pList.add(p); 801 } 802 } 803 return pList; 804 } 805 806 public TcPr getTcPr(Tc tc) { 807 TcPr tcPr = tc.getTcPr(); 808 if (tcPr == null) { 809 tcPr = new TcPr(); 810 tc.setTcPr(tcPr); 811 } 812 return tcPr; 813 } 814 815 /** 816 * @Description: 设置单元格垂直对齐方式 817 */ 818 public void setTcVAlign(Tc tc, STVerticalJc vAlignType) { 819 if (vAlignType != null) { 820 TcPr tcPr = getTcPr(tc); 821 CTVerticalJc vAlign = new CTVerticalJc(); 822 vAlign.setVal(vAlignType); 823 tcPr.setVAlign(vAlign); 824 } 825 } 826 827 /** 828 * @Description: 设置单元格水平对齐方式 829 */ 830 public void setTcJcAlign(Tc tc, JcEnumeration jcType) { 831 if (jcType != null) { 832 List<P> pList = getTcAllP(tc); 833 for (P p : pList) { 834 setParaJcAlign(p, jcType); 835 } 836 } 837 } 838 839 public RPr getRPr(R r) { 840 RPr rpr = r.getRPr(); 841 if (rpr == null) { 842 rpr = new RPr(); 843 r.setRPr(rpr); 844 } 845 return rpr; 846 } 847 848 /** 849 * @Description: 获取所有的单元格 850 */ 851 public List<Tc> getTrAllCell(Tr tr) { 852 List<Object> objList = getAllElementFromObject(tr, Tc.class); 853 List<Tc> tcList = new ArrayList<Tc>(); 854 if (objList == null) { 855 return tcList; 856 } 857 for (Object tcObj : objList) { 858 if (tcObj instanceof Tc) { 859 Tc objTc = (Tc) tcObj; 860 tcList.add(objTc); 861 } 862 } 863 return tcList; 864 } 865 866 /** 867 * @Description: 获取单元格内容 868 */ 869 public String getTcContent(Tc tc) throws Exception { 870 return getElementContent(tc); 871 } 872 873 /** 874 * @Description:设置单元格内容,content为null则清除单元格内容 875 */ 876 public void setTcContent(Tc tc, RPr rpr, String content) { 877 List<Object> pList = tc.getContent(); 878 P p = null; 879 if (pList != null && pList.size() > 0) { 880 if (pList.get(0) instanceof P) { 881 p = (P) pList.get(0); 882 } 883 } else { 884 p = new P(); 885 tc.getContent().add(p); 886 } 887 R run = null; 888 List<Object> 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 = new R(); 896 p.getContent().add(run); 897 if (content != null) { 898 String[] contentArr = content.split("\n"); 899 Text text = new Text(); 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 = new Br(); 907 run.getContent().add(br);// 换行 908 text = new Text(); 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 void removeTcContent(Tc tc) { 921 List<Object> pList = tc.getContent(); 922 P p = null; 923 if (pList != null && pList.size() > 0) { 924 if (pList.get(0) instanceof P) { 925 p = (P) pList.get(0); 926 } 927 } else { 928 return; 929 } 930 List<Object> 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) throws Exception { 944 if (index < 0) { 945 return; 946 } 947 final String xpath = "(//w:tbl)[" + index + "]"; 948 final List<Object> 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:获取NodeList 956 * @deprecated 957 */ 958 public List<Object> getObjectByXpath(WordprocessingMLPackage wordMLPackage, String xpath) throws Exception { 959 final List<Object> jaxbNodes = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(xpath, true); 960 return jaxbNodes; 961 } 962 963 /*------------------------------------Word 段落相关--------------------------------------------------- */ 964 /** 965 * @Description: 只删除单独的段落,不包括表格内或其他内的段落 966 */ 967 public boolean removeParaByIndex(WordprocessingMLPackage wordMLPackage, int index) { 968 boolean flag = false; 969 if (index < 0) { 970 return flag; 971 } 972 List<Object> objList = wordMLPackage.getMainDocumentPart().getContent(); 973 if (objList == null) { 974 return flag; 975 } 976 int k = -1; 977 for (int i = 0, len = objList.size(); i < len; i++) { 978 if (objList.get(i) instanceof P) { 979 k++; 980 if (k == index) { 981 wordMLPackage.getMainDocumentPart().getContent().remove(i); 982 flag = true; 983 break; 984 } 985 } 986 } 987 return flag; 988 } 989 990 /** 991 * @Description: 设置段落水平对齐方式 992 */ 993 public void setParaJcAlign(P paragraph, JcEnumeration hAlign) { 994 if (hAlign != null) { 995 PPr pprop = paragraph.getPPr(); 996 if (pprop == null) { 997 pprop = new PPr(); 998 paragraph.setPPr(pprop); 999 } 1000 Jc align = new Jc(); 1001 align.setVal(hAlign); 1002 pprop.setJc(align); 1003 } 1004 } 1005 1006 /** 1007 * @Description: 设置段落内容 1008 */ 1009 public void setParaRContent(P p, RPr runProperties, String content) { 1010 R run = null; 1011 List<Object> 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 = new R(); 1019 p.getContent().add(run); 1020 if (content != null) { 1021 String[] contentArr = content.split("\n"); 1022 Text text = new Text(); 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 = new Br(); 1030 run.getContent().add(br);// 换行 1031 text = new Text(); 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 void appendParaRContent(P p, RPr runProperties, String content) { 1044 if (content != null) { 1045 R run = new R(); 1046 p.getContent().add(run); 1047 String[] contentArr = content.split("\n"); 1048 Text text = new Text(); 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 = new Br(); 1056 run.getContent().add(br);// 换行 1057 text = new Text(); 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 void addImageToPara(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, P paragraph, 1070 String filePath, String content, RPr rpr, String altText, int id1, int id2) throws Exception { 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 = new FileInputStream(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 void addPageBreak(P para, STBrType sTBrType) { 1094 Br breakObj = new Br(); 1095 breakObj.setType(sTBrType); 1096 para.getContent().add(breakObj); 1097 } 1098 1099 /** 1100 * @Description: 设置段落是否禁止行号(禁止用于当前行号) 1101 */ 1102 public void setParagraphSuppressLineNum(P p) { 1103 PPr ppr = getPPr(p); 1104 BooleanDefaultTrue line = ppr.getSuppressLineNumbers(); 1105 if (line == null) { 1106 line = new BooleanDefaultTrue(); 1107 } 1108 line.setVal(true); 1109 ppr.setSuppressLineNumbers(line); 1110 } 1111 1112 /** 1113 * @Description: 设置段落底纹(对整段文字起作用) 1114 */ 1115 public void setParagraphShdStyle(P p, STShd shdType, String shdColor) { 1116 PPr ppr = getPPr(p); 1117 CTShd ctShd = ppr.getShd(); 1118 if (ctShd == null) { 1119 ctShd = new CTShd(); 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 * @param isSpace 1132 * 是否设置段前段后值 1133 * @param before 1134 * 段前磅数 1135 * @param after 1136 * 段后磅数 1137 * @param beforeLines 1138 * 段前行数 1139 * @param afterLines 1140 * 段后行数 1141 * @param isLine 1142 * 是否设置行距 1143 * @param lineValue 1144 * 行距值 1145 * @param sTLineSpacingRule 1146 * 自动auto 固定exact 最小 atLeast 1磅=20 1行=100 单倍行距=240 1147 */ 1148 public void setParagraphSpacing(P p, boolean isSpace, String before, String after, String beforeLines, 1149 String afterLines, boolean isLine, String lineValue, STLineSpacingRule sTLineSpacingRule) { 1150 PPr pPr = getPPr(p); 1151 Spacing spacing = pPr.getSpacing(); 1152 if (spacing == null) { 1153 spacing = new Spacing(); 1154 pPr.setSpacing(spacing); 1155 } 1156 if (isSpace) { 1157 if (StringUtils.isNotBlank(before)) { 1158 // 段前磅数 1159 spacing.setBefore(new BigInteger(before)); 1160 } 1161 if (StringUtils.isNotBlank(after)) { 1162 // 段后磅数 1163 spacing.setAfter(new BigInteger(after)); 1164 } 1165 if (StringUtils.isNotBlank(beforeLines)) { 1166 // 段前行数 1167 spacing.setBeforeLines(new BigInteger(beforeLines)); 1168 } 1169 if (StringUtils.isNotBlank(afterLines)) { 1170 // 段后行数 1171 spacing.setAfterLines(new BigInteger(afterLines)); 1172 } 1173 } 1174 if (isLine) { 1175 if (StringUtils.isNotBlank(lineValue)) { 1176 spacing.setLine(new BigInteger(lineValue)); 1177 } 1178 if (sTLineSpacingRule != null) { 1179 spacing.setLineRule(sTLineSpacingRule); 1180 } 1181 } 1182 } 1183 1184 /** 1185 * @Description: 设置段落缩进信息 1厘米≈567 1186 */ 1187 public void setParagraphIndInfo(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 = new Ind(); 1193 ppr.setInd(ind); 1194 } 1195 if (StringUtils.isNotBlank(firstLine)) { 1196 ind.setFirstLine(new BigInteger(firstLine)); 1197 } 1198 if (StringUtils.isNotBlank(firstLineChar)) { 1199 ind.setFirstLineChars(new BigInteger(firstLineChar)); 1200 } 1201 if (StringUtils.isNotBlank(hanging)) { 1202 ind.setHanging(new BigInteger(hanging)); 1203 } 1204 if (StringUtils.isNotBlank(hangingChar)) { 1205 ind.setHangingChars(new BigInteger(hangingChar)); 1206 } 1207 if (StringUtils.isNotBlank(left)) { 1208 ind.setLeft(new BigInteger(left)); 1209 } 1210 if (StringUtils.isNotBlank(leftChar)) { 1211 ind.setLeftChars(new BigInteger(leftChar)); 1212 } 1213 if (StringUtils.isNotBlank(right)) { 1214 ind.setRight(new BigInteger(right)); 1215 } 1216 if (StringUtils.isNotBlank(rigthChar)) { 1217 ind.setRightChars(new BigInteger(rigthChar)); 1218 } 1219 } 1220 1221 public PPr getPPr(P p) { 1222 PPr ppr = p.getPPr(); 1223 if (ppr == null) { 1224 ppr = new PPr(); 1225 p.setPPr(ppr); 1226 } 1227 return ppr; 1228 } 1229 1230 public ParaRPr getParaRPr(PPr ppr) { 1231 ParaRPr parRpr = ppr.getRPr(); 1232 if (parRpr == null) { 1233 parRpr = new ParaRPr(); 1234 ppr.setRPr(parRpr); 1235 } 1236 return parRpr; 1237 1238 } 1239 1240 public void setParaVanish(PPr ppr, boolean isVanish) { 1241 ParaRPr parRpr = getParaRPr(ppr); 1242 BooleanDefaultTrue vanish = parRpr.getVanish(); 1243 if (vanish != null) { 1244 vanish.setVal(isVanish); 1245 } else { 1246 vanish = new BooleanDefaultTrue(); 1247 parRpr.setVanish(vanish); 1248 vanish.setVal(isVanish); 1249 } 1250 } 1251 1252 /** 1253 * @Description: 设置段落边框样式 1254 */ 1255 public void setParagraghBorders(P p, CTBorder topBorder, CTBorder bottomBorder, CTBorder leftBorder, 1256 CTBorder rightBorder) { 1257 PPr ppr = getPPr(p); 1258 PBdr pBdr = new PBdr(); 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 void setFontStyle(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 void setFontSize(RPr runProperties, String fontSize) { 1288 if (StringUtils.isNotBlank(fontSize)) { 1289 HpsMeasure size = new HpsMeasure(); 1290 size.setVal(new BigInteger(fontSize)); 1291 runProperties.setSz(size); 1292 runProperties.setSzCs(size); 1293 } 1294 } 1295 1296 /** 1297 * @Description: 设置字体 1298 */ 1299 public void setFontFamily(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 = new RFonts(); 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 void setFontColor(RPr runProperties, String color) { 1319 if (color != null) { 1320 Color c = new Color(); 1321 c.setVal(color); 1322 runProperties.setColor(c); 1323 } 1324 } 1325 1326 /** 1327 * @Description: 设置字符边框 1328 */ 1329 public void addRPrBorderStyle(RPr runProperties, String size, STBorder bordType, String space, String color) { 1330 CTBorder value = new CTBorder(); 1331 if (StringUtils.isNotBlank(color)) { 1332 value.setColor(color); 1333 } 1334 if (StringUtils.isNotBlank(size)) { 1335 value.setSz(new BigInteger(size)); 1336 } 1337 if (StringUtils.isNotBlank(space)) { 1338 value.setSpace(new BigInteger(space)); 1339 } 1340 if (bordType != null) { 1341 value.setVal(bordType); 1342 } 1343 runProperties.setBdr(value); 1344 } 1345 1346 /** 1347 * @Description:着重号 1348 */ 1349 public void addRPrEmStyle(RPr runProperties, STEm emType) { 1350 if (emType != null) { 1351 CTEm em = new CTEm(); 1352 em.setVal(emType); 1353 runProperties.setEm(em); 1354 } 1355 } 1356 1357 /** 1358 * @Description: 空心 1359 */ 1360 public void addRPrOutlineStyle(RPr runProperties) { 1361 BooleanDefaultTrue outline = new BooleanDefaultTrue(); 1362 outline.setVal(true); 1363 runProperties.setOutline(outline); 1364 } 1365 1366 /** 1367 * @Description: 设置上标下标 1368 */ 1369 public void addRPrcaleStyle(RPr runProperties, STVerticalAlignRun vAlign) { 1370 if (vAlign != null) { 1371 CTVerticalAlignRun value = new CTVerticalAlignRun(); 1372 value.setVal(vAlign); 1373 runProperties.setVertAlign(value); 1374 } 1375 } 1376 1377 /** 1378 * @Description: 设置字符间距缩进 1379 */ 1380 public void addRPrScaleStyle(RPr runProperties, int indent) { 1381 CTTextScale value = new CTTextScale(); 1382 value.setVal(indent); 1383 runProperties.setW(value); 1384 } 1385 1386 /** 1387 * @Description: 设置字符间距信息 1388 */ 1389 public void addRPrtSpacingStyle(RPr runProperties, int spacing) { 1390 CTSignedTwipsMeasure value = new CTSignedTwipsMeasure(); 1391 value.setVal(BigInteger.valueOf(spacing)); 1392 runProperties.setSpacing(value); 1393 } 1394 1395 /** 1396 * @Description: 设置文本位置 1397 */ 1398 public void addRPrtPositionStyle(RPr runProperties, int position) { 1399 CTSignedHpsMeasure ctPosition = new CTSignedHpsMeasure(); 1400 ctPosition.setVal(BigInteger.valueOf(position)); 1401 runProperties.setPosition(ctPosition); 1402 } 1403 1404 /** 1405 * @Description: 阴文 1406 */ 1407 public void addRPrImprintStyle(RPr runProperties) { 1408 BooleanDefaultTrue imprint = new BooleanDefaultTrue(); 1409 imprint.setVal(true); 1410 runProperties.setImprint(imprint); 1411 } 1412 1413 /** 1414 * @Description: 阳文 1415 */ 1416 public void addRPrEmbossStyle(RPr runProperties) { 1417 BooleanDefaultTrue emboss = new BooleanDefaultTrue(); 1418 emboss.setVal(true); 1419 runProperties.setEmboss(emboss); 1420 } 1421 1422 /** 1423 * @Description: 设置隐藏 1424 */ 1425 public void setRPrVanishStyle(RPr runProperties, boolean isVanish) { 1426 BooleanDefaultTrue vanish = runProperties.getVanish(); 1427 if (vanish != null) { 1428 vanish.setVal(isVanish); 1429 } else { 1430 vanish = new BooleanDefaultTrue(); 1431 vanish.setVal(isVanish); 1432 runProperties.setVanish(vanish); 1433 } 1434 } 1435 1436 /** 1437 * @Description: 设置阴影 1438 */ 1439 public void addRPrShadowStyle(RPr runProperties) { 1440 BooleanDefaultTrue shadow = new BooleanDefaultTrue(); 1441 shadow.setVal(true); 1442 runProperties.setShadow(shadow); 1443 } 1444 1445 /** 1446 * @Description: 设置底纹 1447 */ 1448 public void addRPrShdStyle(RPr runProperties, STShd shdtype) { 1449 if (shdtype != null) { 1450 CTShd shd = new CTShd(); 1451 shd.setVal(shdtype); 1452 runProperties.setShd(shd); 1453 } 1454 } 1455 1456 /** 1457 * @Description: 设置突出显示文本 1458 */ 1459 public void addRPrHightLightStyle(RPr runProperties, String hightlight) { 1460 if (StringUtils.isNotBlank(hightlight)) { 1461 Highlight highlight = new Highlight(); 1462 highlight.setVal(hightlight); 1463 runProperties.setHighlight(highlight); 1464 } 1465 } 1466 1467 /** 1468 * @Description: 设置删除线样式 1469 */ 1470 public void addRPrStrikeStyle(RPr runProperties, boolean isStrike, boolean isDStrike) { 1471 // 删除线 1472 if (isStrike) { 1473 BooleanDefaultTrue strike = new BooleanDefaultTrue(); 1474 strike.setVal(true); 1475 runProperties.setStrike(strike); 1476 } 1477 // 双删除线 1478 if (isDStrike) { 1479 BooleanDefaultTrue dStrike = new BooleanDefaultTrue(); 1480 dStrike.setVal(true); 1481 runProperties.setDstrike(dStrike); 1482 } 1483 } 1484 1485 /** 1486 * @Description: 加粗 1487 */ 1488 public void addRPrBoldStyle(RPr runProperties) { 1489 BooleanDefaultTrue b = new BooleanDefaultTrue(); 1490 b.setVal(true); 1491 runProperties.setB(b); 1492 } 1493 1494 /** 1495 * @Description: 倾斜 1496 */ 1497 public void addRPrItalicStyle(RPr runProperties) { 1498 BooleanDefaultTrue b = new BooleanDefaultTrue(); 1499 b.setVal(true); 1500 runProperties.setI(b); 1501 } 1502 1503 /** 1504 * @Description: 添加下划线 1505 */ 1506 public void addRPrUnderlineStyle(RPr runProperties, UnderlineEnumeration enumType) { 1507 U val = new U(); 1508 val.setVal(enumType); 1509 runProperties.setU(val); 1510 } 1511 1512 /*------------------------------------Word 相关--------------------------------------------------- */ 1513 /** 1514 * @Description: 设置分节符 nextPage:下一页 continuous:连续 evenPage:偶数页 oddPage:奇数页 1515 */ 1516 public void setDocSectionBreak(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 = new Type(); 1522 sectPr.setType(sectType); 1523 } 1524 sectType.setVal(sectValType); 1525 } 1526 } 1527 1528 /** 1529 * @Description: 设置页面背景色 1530 */ 1531 public void setDocumentBackGround(WordprocessingMLPackage wordPackage, ObjectFactory factory, String color) 1532 throws Exception { 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 void setDocumentBorders(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 void setDocumentSize(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(new BigInteger(width)); 1582 } 1583 if (StringUtils.isNotBlank(height)) { 1584 pgSz.setH(new BigInteger(height)); 1585 } 1586 if (stValue != null) { 1587 pgSz.setOrient(stValue); 1588 } 1589 } 1590 1591 public SectPr getDocSectPr(WordprocessingMLPackage wordPackage) { 1592 SectPr sectPr = wordPackage.getDocumentModel().getSections().get(0).getSectPr(); 1593 return sectPr; 1594 } 1595 1596 /** 1597 * @Description:设置页边距 1598 */ 1599 public void setDocMarginSpace(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(new BigInteger(top)); 1609 } 1610 if (StringUtils.isNotBlank(bottom)) { 1611 pg.setBottom(new BigInteger(bottom)); 1612 } 1613 if (StringUtils.isNotBlank(left)) { 1614 pg.setLeft(new BigInteger(left)); 1615 } 1616 if (StringUtils.isNotBlank(right)) { 1617 pg.setRight(new BigInteger(right)); 1618 } 1619 } 1620 1621 /** 1622 * @Description: 设置行号 1623 * @param distance 1624 * :距正文距离 1厘米=567 1625 * @param start 1626 * :起始编号(0开始) 1627 * @param countBy 1628 * :行号间隔 1629 * @param restartType 1630 * :STLineNumberRestart.CONTINUOUS(continuous连续编号)<br/> 1631 * STLineNumberRestart.NEW_PAGE(每页重新编号)<br/> 1632 * STLineNumberRestart.NEW_SECTION(每节重新编号) 1633 */ 1634 public void setDocInNumType(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 = new CTLineNumber(); 1640 sectPr.setLnNumType(lnNumType); 1641 } 1642 if (StringUtils.isNotBlank(countBy)) { 1643 lnNumType.setCountBy(new BigInteger(countBy)); 1644 } 1645 if (StringUtils.isNotBlank(distance)) { 1646 lnNumType.setDistance(new BigInteger(distance)); 1647 } 1648 if (StringUtils.isNotBlank(start)) { 1649 lnNumType.setStart(new BigInteger(start)); 1650 } 1651 if (restartType != null) { 1652 lnNumType.setRestart(restartType); 1653 } 1654 } 1655 1656 /** 1657 * @Description:设置文字方向 tbRl 垂直 1658 */ 1659 public void setDocTextDirection(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 = new TextDirection(); 1665 sectPr.setTextDirection(textDir); 1666 } 1667 textDir.setVal(textDirection); 1668 } 1669 } 1670 1671 /** 1672 * @Description:设置word 垂直对齐方式(Word默认方式都是"顶端对齐") 1673 */ 1674 public void setDocVAlign(WordprocessingMLPackage wordPackage, STVerticalJc valignType) { 1675 if (valignType != null) { 1676 SectPr sectPr = getDocSectPr(wordPackage); 1677 CTVerticalJc valign = sectPr.getVAlign(); 1678 if (valign == null) { 1679 valign = new CTVerticalJc(); 1680 sectPr.setVAlign(valign); 1681 } 1682 valign.setVal(valignType); 1683 } 1684 } 1685 1686 /** 1687 * @Description:获取文档的可用宽度 1688 */ 1689 public int getWritableWidth(WordprocessingMLPackage wordPackage) throws Exception { 1690 return wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips(); 1691 } 1692 1693 }