docx4j方法總結

  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,  
  94.             MainDocumentPart mainPart, ObjectFactory factory, P paragraph,  
  95.             String url, String value, String cnFontName, String enFontName,  
  96.             String fontSize) throws Exception {  
  97.         if (StringUtils.isBlank(enFontName)) {  
  98.             enFontName = "Times New Roman";  
  99.         }  
  100.         if (StringUtils.isBlank(cnFontName)) {  
  101.             cnFontName = "微軟雅黑";  
  102.         }  
  103.         if (StringUtils.isBlank(fontSize)) {  
  104.             fontSize = "22";  
  105.         }  
  106.         org.docx4j.relationships.ObjectFactory reFactory = new org.docx4j.relationships.ObjectFactory();  
  107.         org.docx4j.relationships.Relationship rel = reFactory  
  108.                 .createRelationship();  
  109.         rel.setType(Namespaces.HYPERLINK);  
  110.         rel.setTarget(url);  
  111.         rel.setTargetMode("External");  
  112.         mainPart.getRelationshipsPart().addRelationship(rel);  
  113.         StringBuffer sb = new StringBuffer();  
  114.         // addRelationship sets the rel's @Id  
  115.         sb.append("<w:hyperlink r:id=\"");  
  116.         sb.append(rel.getId());  
  117.         sb.append("\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ");  
  118.         sb.append("xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >");  
  119.         sb.append("<w:r><w:rPr><w:rStyle w:val=\"Hyperlink\" />");  
  120.         sb.append("<w:rFonts  w:ascii=\"");  
  121.         sb.append(enFontName);  
  122.         sb.append("\"  w:hAnsi=\"");  
  123.         sb.append(enFontName);  
  124.         sb.append("\"  w:eastAsia=\"");  
  125.         sb.append(cnFontName);  
  126.         sb.append("\" w:hint=\"eastAsia\"/>");  
  127.         sb.append("<w:sz w:val=\"");  
  128.         sb.append(fontSize);  
  129.         sb.append("\"/><w:szCs w:val=\"");  
  130.         sb.append(fontSize);  
  131.         sb.append("\"/></w:rPr><w:t>");  
  132.         sb.append(value);  
  133.         sb.append("</w:t></w:r></w:hyperlink>");  
  134.   
  135.         Hyperlink link = (Hyperlink) XmlUtils.unmarshalString(sb.toString());  
  136.         paragraph.getContent().add(link);  
  137.     }  
  138.   
  139.     public String getElementContent(Object obj) throws Exception {  
  140.         StringWriter stringWriter = new StringWriter();  
  141.         TextUtils.extractText(obj, stringWriter);  
  142.         return stringWriter.toString();  
  143.     }  
  144.   
  145.     /** 
  146.      * @Description:得到指定類型的元素 
  147.      */  
  148.     public static List<Object> getAllElementFromObject(Object obj,  
  149.             Class<?> toSearch) {  
  150.         List<Object> result = new ArrayList<Object>();  
  151.         if (obj instanceof JAXBElement)  
  152.             obj = ((JAXBElement<?>) obj).getValue();  
  153.         if (obj.getClass().equals(toSearch))  
  154.             result.add(obj);  
  155.         else if (obj instanceof ContentAccessor) {  
  156.             List<?> children = ((ContentAccessor) obj).getContent();  
  157.             for (Object child : children) {  
  158.                 result.addAll(getAllElementFromObject(child, toSearch));  
  159.             }  
  160.         }  
  161.         return result;  
  162.     }  
  163.   
  164.     /** 
  165.      * @Description:保存WordprocessingMLPackage 
  166.      */  
  167.     public void saveWordPackage(WordprocessingMLPackage wordPackage, File file)  
  168.             throws Exception {  
  169.         wordPackage.save(file);  
  170.     }  
  171.   
  172.     /** 
  173.      * @Description:新建WordprocessingMLPackage 
  174.      */  
  175.     public WordprocessingMLPackage createWordprocessingMLPackage()  
  176.             throws Exception {  
  177.         return WordprocessingMLPackage.createPackage();  
  178.     }  
  179.   
  180.     /** 
  181.      * @Description:加載帶密碼WordprocessingMLPackage 
  182.      */  
  183.     public WordprocessingMLPackage loadWordprocessingMLPackageWithPwd(  
  184.             String filePath, String password) throws Exception {  
  185.         OpcPackage opcPackage = WordprocessingMLPackage.load(new java.io.File(  
  186.                 filePath), password);  
  187.         WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage) opcPackage;  
  188.         return wordMLPackage;  
  189.     }  
  190.   
  191.     /** 
  192.      * @Description:加載WordprocessingMLPackage 
  193.      */  
  194.     public WordprocessingMLPackage loadWordprocessingMLPackage(String filePath)  
  195.             throws Exception {  
  196.         WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage  
  197.                 .load(new java.io.File(filePath));  
  198.         return wordMLPackage;  
  199.     }  
  200.   
  201.     /*------------------------------------Word 表格相關---------------------------------------------------  */  
  202.     /** 
  203.      * @Description: 跨列合併 
  204.      */  
  205.     public void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell,  
  206.             int toCell) {  
  207.         if (row < 0 || fromCell < 0 || toCell < 0) {  
  208.             return;  
  209.         }  
  210.         List<Tr> trList = getTblAllTr(tbl);  
  211.         if (row > trList.size()) {  
  212.             return;  
  213.         }  
  214.         Tr tr = trList.get(row);  
  215.         List<Tc> tcList = getTrAllCell(tr);  
  216.         for (int cellIndex = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) {  
  217.             Tc tc = tcList.get(cellIndex);  
  218.             TcPr tcPr = getTcPr(tc);  
  219.             if (cellIndex == fromCell) {  
  220.                 GridSpan gridSpan = tcPr.getGridSpan();  
  221.                 if (gridSpan == null) {  
  222.                     gridSpan = new GridSpan();  
  223.                     tcPr.setGridSpan(gridSpan);  
  224.                 }  
  225.                 gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1,  
  226.                         toCell) - fromCell + 1));  
  227.             } else {  
  228.                 tr.getContent().remove(cellIndex);  
  229.             }  
  230.         }  
  231.     }  
  232.   
  233.     /** 
  234.      * @Description: 跨列合併 
  235.      */  
  236.     public void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) {  
  237.         if (row < 0 || fromCell < 0 || toCell < 0) {  
  238.             return;  
  239.         }  
  240.         List<Tr> trList = getTblAllTr(tbl);  
  241.         if (row > trList.size()) {  
  242.             return;  
  243.         }  
  244.         Tr tr = trList.get(row);  
  245.         List<Tc> tcList = getTrAllCell(tr);  
  246.         for (int cellIndex = fromCell, len = Math  
  247.                 .min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) {  
  248.             Tc tc = tcList.get(cellIndex);  
  249.             TcPr tcPr = getTcPr(tc);  
  250.             HMerge hMerge = tcPr.getHMerge();  
  251.             if (hMerge == null) {  
  252.                 hMerge = new HMerge();  
  253.                 tcPr.setHMerge(hMerge);  
  254.             }  
  255.             if (cellIndex == fromCell) {  
  256.                 hMerge.setVal("restart");  
  257.             } else {  
  258.                 hMerge.setVal("continue");  
  259.             }  
  260.         }  
  261.     }  
  262.   
  263.     /** 
  264.      * @Description: 跨行合併 
  265.      */  
  266.     public void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) {  
  267.         if (col < 0 || fromRow < 0 || toRow < 0) {  
  268.             return;  
  269.         }  
  270.         for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {  
  271.             Tc tc = getTc(tbl, rowIndex, col);  
  272.             if (tc == null) {  
  273.                 break;  
  274.             }  
  275.             TcPr tcPr = getTcPr(tc);  
  276.             VMerge vMerge = tcPr.getVMerge();  
  277.             if (vMerge == null) {  
  278.                 vMerge = new VMerge();  
  279.                 tcPr.setVMerge(vMerge);  
  280.             }  
  281.             if (rowIndex == fromRow) {  
  282.                 vMerge.setVal("restart");  
  283.             } else {  
  284.                 vMerge.setVal("continue");  
  285.             }  
  286.         }  
  287.     }  
  288.   
  289.     /** 
  290.      * @Description:得到指定位置的單元格 
  291.      */  
  292.     public Tc getTc(Tbl tbl, int row, int cell) {  
  293.         if (row < 0 || cell < 0) {  
  294.             return null;  
  295.         }  
  296.         List<Tr> trList = getTblAllTr(tbl);  
  297.         if (row >= trList.size()) {  
  298.             return null;  
  299.         }  
  300.         List<Tc> tcList = getTrAllCell(trList.get(row));  
  301.         if (cell >= tcList.size()) {  
  302.             return null;  
  303.         }  
  304.         return tcList.get(cell);  
  305.     }  
  306.   
  307.     /** 
  308.      * @Description:得到所有表格 
  309.      */  
  310.     public List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) {  
  311.         MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart();  
  312.         List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class);  
  313.         if (objList == null) {  
  314.             return null;  
  315.         }  
  316.         List<Tbl> tblList = new ArrayList<Tbl>();  
  317.         for (Object obj : objList) {  
  318.             if (obj instanceof Tbl) {  
  319.                 Tbl tbl = (Tbl) obj;  
  320.                 tblList.add(tbl);  
  321.             }  
  322.         }  
  323.         return tblList;  
  324.     }  
  325.   
  326.     /** 
  327.      * @Description:刪除指定位置的表格,刪除後表格數量減一 
  328.      */  
  329.     public boolean removeTableByIndex(WordprocessingMLPackage wordMLPackage,  
  330.             int index) throws Exception {  
  331.         boolean flag = false;  
  332.         if (index < 0) {  
  333.             return flag;  
  334.         }  
  335.         List<Object> objList = wordMLPackage.getMainDocumentPart().getContent();  
  336.         if (objList == null) {  
  337.             return flag;  
  338.         }  
  339.         int k = -1;  
  340.         for (int i = 0, len = objList.size(); i < len; i++) {  
  341.             Object obj = XmlUtils.unwrap(objList.get(i));  
  342.             if (obj instanceof Tbl) {  
  343.                 k++;  
  344.                 if (k == index) {  
  345.                     wordMLPackage.getMainDocumentPart().getContent().remove(i);  
  346.                     flag = true;  
  347.                     break;  
  348.                 }  
  349.             }  
  350.         }  
  351.         return flag;  
  352.     }  
  353.   
  354.     /** 
  355.      * @Description: 獲取單元格內容,無分割符 
  356.      */  
  357.     public String getTblContentStr(Tbl tbl) throws Exception {  
  358.         return getElementContent(tbl);  
  359.     }  
  360.   
  361.   
  362.     /** 
  363.      * @Description: 獲取表格內容 
  364.      */  
  365.     public List<String> getTblContentList(Tbl tbl) throws Exception {  
  366.         List<String> resultList = new ArrayList<String>();  
  367.         List<Tr> trList = getTblAllTr(tbl);  
  368.         for (Tr tr : trList) {  
  369.             StringBuffer sb = new StringBuffer();  
  370.             List<Tc> tcList = getTrAllCell(tr);  
  371.             for (Tc tc : tcList) {  
  372.                 sb.append(getElementContent(tc) + ",");  
  373.             }  
  374.             resultList.add(sb.toString());  
  375.         }  
  376.         return resultList;  
  377.     }  
  378.   
  379.     public TblPr getTblPr(Tbl tbl) {  
  380.         TblPr tblPr = tbl.getTblPr();  
  381.         if (tblPr == null) {  
  382.             tblPr = new TblPr();  
  383.             tbl.setTblPr(tblPr);  
  384.         }  
  385.         return tblPr;  
  386.     }  
  387.   
  388.     /** 
  389.      * @Description: 設置表格總寬度 
  390.      */  
  391.     public void setTableWidth(Tbl tbl, String width) {  
  392.         if (StringUtils.isNotBlank(width)) {  
  393.             TblPr tblPr = getTblPr(tbl);  
  394.             TblWidth tblW = tblPr.getTblW();  
  395.             if (tblW == null) {  
  396.                 tblW = new TblWidth();  
  397.                 tblPr.setTblW(tblW);  
  398.             }  
  399.             tblW.setW(new BigInteger(width));  
  400.             tblW.setType("dxa");  
  401.         }  
  402.     }  
  403.   
  404.     /** 
  405.      * @Description:創建表格(默認水平居中,垂直居中) 
  406.      */  
  407.     public Tbl createTable(WordprocessingMLPackage wordPackage, int rowNum,  
  408.             int colsNum) throws Exception {  
  409.         colsNum = Math.max(1, colsNum);  
  410.         rowNum = Math.max(1, rowNum);  
  411.         int widthTwips = getWritableWidth(wordPackage);  
  412.         int colWidth = widthTwips / colsNum;  
  413.         int[] widthArr = new int[colsNum];  
  414.         for (int i = 0; i < colsNum; i++) {  
  415.             widthArr[i] = colWidth;  
  416.         }  
  417.         return createTable(rowNum, colsNum, widthArr);  
  418.     }  
  419.   
  420.     /** 
  421.      * @Description:創建表格(默認水平居中,垂直居中) 
  422.      */  
  423.     public Tbl createTable(int rowNum, int colsNum, int[] widthArr)  
  424.             throws Exception {  
  425.         colsNum = Math.max(1, Math.min(colsNum, widthArr.length));  
  426.         rowNum = Math.max(1, rowNum);  
  427.         Tbl tbl = new Tbl();  
  428.         StringBuffer tblSb = new StringBuffer();  
  429.         tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION)  
  430.                 .append(">");  
  431.         tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");  
  432.         tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");  
  433.         // 上邊框  
  434.         tblSb.append("<w:tblBorders>");  
  435.         tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");  
  436.         // 左邊框  
  437.         tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");  
  438.         // 下邊框  
  439.         tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");  
  440.         // 右邊框  
  441.         tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");  
  442.         tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");  
  443.         tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");  
  444.         tblSb.append("</w:tblBorders>");  
  445.         tblSb.append("</w:tblPr>");  
  446.         TblPr tblPr = null;  
  447.         tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());  
  448.         Jc jc = new Jc();  
  449.         // 單元格居中對齊  
  450.         jc.setVal(JcEnumeration.CENTER);  
  451.         tblPr.setJc(jc);  
  452.   
  453.         tbl.setTblPr(tblPr);  
  454.   
  455.         // 設定各單元格寬度  
  456.         TblGrid tblGrid = new TblGrid();  
  457.         tbl.setTblGrid(tblGrid);  
  458.         for (int i = 0; i < colsNum; i++) {  
  459.             TblGridCol gridCol = new TblGridCol();  
  460.             gridCol.setW(BigInteger.valueOf(widthArr[i]));  
  461.             tblGrid.getGridCol().add(gridCol);  
  462.         }  
  463.         // 新增行  
  464.         for (int j = 0; j < rowNum; j++) {  
  465.             Tr tr = new Tr();  
  466.             tbl.getContent().add(tr);  
  467.             // 列  
  468.             for (int i = 0; i < colsNum; i++) {  
  469.                 Tc tc = new Tc();  
  470.                 tr.getContent().add(tc);  
  471.   
  472.                 TcPr tcPr = new TcPr();  
  473.                 TblWidth cellWidth = new TblWidth();  
  474.                 cellWidth.setType("dxa");  
  475.                 cellWidth.setW(BigInteger.valueOf(widthArr[i]));  
  476.                 tcPr.setTcW(cellWidth);  
  477.                 tc.setTcPr(tcPr);  
  478.   
  479.                 // 垂直居中  
  480.                 setTcVAlign(tc, STVerticalJc.CENTER);  
  481.                 P p = new P();  
  482.                 PPr pPr = new PPr();  
  483.                 pPr.setJc(jc);  
  484.                 p.setPPr(pPr);  
  485.                 R run = new R();  
  486.                 p.getContent().add(run);  
  487.                 tc.getContent().add(p);  
  488.             }  
  489.         }  
  490.         return tbl;  
  491.     }  
  492.   
  493.     /** 
  494.      * @Description:表格增加邊框 可以設置上下左右四個邊框樣式以及橫豎水平線樣式 
  495.      */  
  496.     public void setTblBorders(TblPr tblPr, CTBorder topBorder,  
  497.             CTBorder rightBorder, CTBorder bottomBorder, CTBorder leftBorder,  
  498.             CTBorder hBorder, CTBorder vBorder) {  
  499.         TblBorders borders = tblPr.getTblBorders();  
  500.         if (borders == null) {  
  501.             borders = new TblBorders();  
  502.             tblPr.setTblBorders(borders);  
  503.         }  
  504.         if (topBorder != null) {  
  505.             borders.setTop(topBorder);  
  506.         }  
  507.         if (rightBorder != null) {  
  508.             borders.setRight(rightBorder);  
  509.         }  
  510.         if (bottomBorder != null) {  
  511.             borders.setBottom(bottomBorder);  
  512.         }  
  513.         if (leftBorder != null) {  
  514.             borders.setLeft(leftBorder);  
  515.         }  
  516.         if (hBorder != null) {  
  517.             borders.setInsideH(hBorder);  
  518.         }  
  519.         if (vBorder != null) {  
  520.             borders.setInsideV(vBorder);  
  521.         }  
  522.     }  
  523.   
  524.     /** 
  525.      * @Description: 設置表格水平對齊方式(僅對錶格起作用,單元格不一定水平對齊) 
  526.      */  
  527.     public void setTblJcAlign(Tbl tbl, JcEnumeration jcType) {  
  528.         if (jcType != null) {  
  529.             TblPr tblPr = getTblPr(tbl);  
  530.             Jc jc = tblPr.getJc();  
  531.             if (jc == null) {  
  532.                 jc = new Jc();  
  533.                 tblPr.setJc(jc);  
  534.             }  
  535.             jc.setVal(jcType);  
  536.         }  
  537.     }  
  538.   
  539.     /** 
  540.      * @Description: 設置表格水平對齊方式(包括單元格),只對該方法前面產生的單元格起作用 
  541.      */  
  542.     public void setTblAllJcAlign(Tbl tbl, JcEnumeration jcType) {  
  543.         if (jcType != null) {  
  544.             setTblJcAlign(tbl, jcType);  
  545.             List<Tr> trList = getTblAllTr(tbl);  
  546.             for (Tr tr : trList) {  
  547.                 List<Tc> tcList = getTrAllCell(tr);  
  548.                 for (Tc tc : tcList) {  
  549.                     setTcJcAlign(tc, jcType);  
  550.                 }  
  551.             }  
  552.         }  
  553.     }  
  554.   
  555.     /** 
  556.      * @Description: 設置表格垂直對齊方式(包括單元格),只對該方法前面產生的單元格起作用 
  557.      */  
  558.     public void setTblAllVAlign(Tbl tbl, STVerticalJc vAlignType) {  
  559.         if (vAlignType != null) {  
  560.             List<Tr> trList = getTblAllTr(tbl);  
  561.             for (Tr tr : trList) {  
  562.                 List<Tc> tcList = getTrAllCell(tr);  
  563.                 for (Tc tc : tcList) {  
  564.                     setTcVAlign(tc, vAlignType);  
  565.                 }  
  566.             }  
  567.         }  
  568.     }  
  569.   
  570.     /** 
  571.      * @Description: 設置單元格Margin 
  572.      */  
  573.     public void setTableCellMargin(Tbl tbl, String top, String right,  
  574.             String bottom, String left) {  
  575.         TblPr tblPr = getTblPr(tbl);  
  576.         CTTblCellMar cellMar = tblPr.getTblCellMar();  
  577.         if (cellMar == null) {  
  578.             cellMar = new CTTblCellMar();  
  579.             tblPr.setTblCellMar(cellMar);  
  580.         }  
  581.         if (StringUtils.isNotBlank(top)) {  
  582.             TblWidth topW = new TblWidth();  
  583.             topW.setW(new BigInteger(top));  
  584.             topW.setType("dxa");  
  585.             cellMar.setTop(topW);  
  586.         }  
  587.         if (StringUtils.isNotBlank(right)) {  
  588.             TblWidth rightW = new TblWidth();  
  589.             rightW.setW(new BigInteger(right));  
  590.             rightW.setType("dxa");  
  591.             cellMar.setRight(rightW);  
  592.         }  
  593.         if (StringUtils.isNotBlank(bottom)) {  
  594.             TblWidth btW = new TblWidth();  
  595.             btW.setW(new BigInteger(bottom));  
  596.             btW.setType("dxa");  
  597.             cellMar.setBottom(btW);  
  598.         }  
  599.         if (StringUtils.isNotBlank(left)) {  
  600.             TblWidth leftW = new TblWidth();  
  601.             leftW.setW(new BigInteger(left));  
  602.             leftW.setType("dxa");  
  603.             cellMar.setLeft(leftW);  
  604.         }  
  605.     }  
  606.   
  607.     /** 
  608.      * @Description: 得到表格所有的行 
  609.      */  
  610.     public List<Tr> getTblAllTr(Tbl tbl) {  
  611.         List<Object> objList = getAllElementFromObject(tbl, Tr.class);  
  612.         List<Tr> trList = new ArrayList<Tr>();  
  613.         if (objList == null) {  
  614.             return trList;  
  615.         }  
  616.         for (Object obj : objList) {  
  617.             if (obj instanceof Tr) {  
  618.                 Tr tr = (Tr) obj;  
  619.                 trList.add(tr);  
  620.             }  
  621.         }  
  622.         return trList;  
  623.   
  624.     }  
  625.   
  626.     /** 
  627.      * @Description:設置tr高度 
  628.      */  
  629.     public void setTrHeight(Tr tr, String heigth) {  
  630.         TrPr trPr = getTrPr(tr);  
  631.         CTHeight ctHeight = new CTHeight();  
  632.         ctHeight.setVal(new BigInteger(heigth));  
  633.         TrHeight trHeight = new TrHeight(ctHeight);  
  634.         trHeight.set(trPr);  
  635.     }  
  636.   
  637.     /** 
  638.      * @Description: 在表格指定位置新增一行,默認居中 
  639.      */  
  640.     public void addTrByIndex(Tbl tbl, int index) {  
  641.         addTrByIndex(tbl, index, STVerticalJc.CENTER, JcEnumeration.CENTER);  
  642.     }  
  643.   
  644.     /** 
  645.      * @Description: 在表格指定位置新增一行(默認按表格定義的列數添加) 
  646.      */  
  647.     public void addTrByIndex(Tbl tbl, int index, STVerticalJc vAlign,  
  648.             JcEnumeration hAlign) {  
  649.         TblGrid tblGrid = tbl.getTblGrid();  
  650.         Tr tr = new Tr();  
  651.         if (tblGrid != null) {  
  652.             List<TblGridCol> gridList = tblGrid.getGridCol();  
  653.             for (TblGridCol tblGridCol : gridList) {  
  654.                 Tc tc = new Tc();  
  655.                 setTcWidth(tc, tblGridCol.getW().toString());  
  656.                 if (vAlign != null) {  
  657.                     // 垂直居中  
  658.                     setTcVAlign(tc, vAlign);  
  659.                 }  
  660.                 P p = new P();  
  661.                 if (hAlign != null) {  
  662.                     PPr pPr = new PPr();  
  663.                     Jc jc = new Jc();  
  664.                     // 單元格居中對齊  
  665.                     jc.setVal(hAlign);  
  666.                     pPr.setJc(jc);  
  667.                     p.setPPr(pPr);  
  668.                 }  
  669.                 R run = new R();  
  670.                 p.getContent().add(run);  
  671.                 tc.getContent().add(p);  
  672.                 tr.getContent().add(tc);  
  673.             }  
  674.         } else {  
  675.             // 大部分情況都不會走到這一步  
  676.             Tr firstTr = getTblAllTr(tbl).get(0);  
  677.             int cellSize = getTcCellSizeWithMergeNum(firstTr);  
  678.             for (int i = 0; i < cellSize; i++) {  
  679.                 Tc tc = new Tc();  
  680.                 if (vAlign != null) {  
  681.                     // 垂直居中  
  682.                     setTcVAlign(tc, vAlign);  
  683.                 }  
  684.                 P p = new P();  
  685.                 if (hAlign != null) {  
  686.                     PPr pPr = new PPr();  
  687.                     Jc jc = new Jc();  
  688.                     // 單元格居中對齊  
  689.                     jc.setVal(hAlign);  
  690.                     pPr.setJc(jc);  
  691.                     p.setPPr(pPr);  
  692.                 }  
  693.                 R run = new R();  
  694.                 p.getContent().add(run);  
  695.                 tc.getContent().add(p);  
  696.                 tr.getContent().add(tc);  
  697.             }  
  698.         }  
  699.         if (index >= 0&&index<tbl.getContent().size()) {  
  700.             tbl.getContent().add(index, tr);  
  701.         } else {  
  702.             tbl.getContent().add(tr);  
  703.         }  
  704.     }  
  705.   
  706.       
  707.     /** 
  708.      * @Description: 得到行的列數 
  709.      */  
  710.     public int getTcCellSizeWithMergeNum(Tr tr) {  
  711.         int cellSize = 1;  
  712.         List<Tc> tcList = getTrAllCell(tr);  
  713.         if (tcList == null || tcList.size() == 0) {  
  714.             return cellSize;  
  715.         }  
  716.         cellSize = tcList.size();  
  717.         for (Tc tc : tcList) {  
  718.             TcPr tcPr = getTcPr(tc);  
  719.             GridSpan gridSpan = tcPr.getGridSpan();  
  720.             if (gridSpan != null) {  
  721.                 cellSize += gridSpan.getVal().intValue() - 1;  
  722.             }  
  723.         }  
  724.         return cellSize;  
  725.     }  
  726.   
  727.     /** 
  728.      * @Description: 刪除指定行 刪除後行數減一 
  729.      */  
  730.     public boolean removeTrByIndex(Tbl tbl, int index) {  
  731.         boolean flag = false;  
  732.         if (index < 0) {  
  733.             return flag;  
  734.         }  
  735.         List<Object> objList = tbl.getContent();  
  736.         if (objList == null) {  
  737.             return flag;  
  738.         }  
  739.         int k = -1;  
  740.         for (int i = 0, len = objList.size(); i < len; i++) {  
  741.             Object obj = XmlUtils.unwrap(objList.get(i));  
  742.             if (obj instanceof Tr) {  
  743.                 k++;  
  744.                 if (k == index) {  
  745.                     tbl.getContent().remove(i);  
  746.                     flag = true;  
  747.                     break;  
  748.                 }  
  749.             }  
  750.         }  
  751.         return flag;  
  752.     }  
  753.   
  754.     public TrPr getTrPr(Tr tr) {  
  755.         TrPr trPr = tr.getTrPr();  
  756.         if (trPr == null) {  
  757.             trPr = new TrPr();  
  758.             tr.setTrPr(trPr);  
  759.         }  
  760.         return trPr;  
  761.     }  
  762.   
  763.     /** 
  764.      * @Description:隱藏行(只對表格中間的部分起作用,不包括首尾行) 
  765.      */  
  766.     public void setTrHidden(Tr tr, boolean hidden) {  
  767.         List<Tc> tcList = getTrAllCell(tr);  
  768.         for (Tc tc : tcList) {  
  769.             setTcHidden(tc, hidden);  
  770.         }  
  771.     }  
  772.   
  773.     /** 
  774.      * @Description: 設置單元格寬度 
  775.      */  
  776.     public void setTcWidth(Tc tc, String width) {  
  777.         if (StringUtils.isNotBlank(width)) {  
  778.             TcPr tcPr = getTcPr(tc);  
  779.             TblWidth tcW = tcPr.getTcW();  
  780.             if (tcW == null) {  
  781.                 tcW = new TblWidth();  
  782.                 tcPr.setTcW(tcW);  
  783.             }  
  784.             tcW.setW(new BigInteger(width));  
  785.             tcW.setType("dxa");  
  786.         }  
  787.     }  
  788.   
  789.     /** 
  790.      * @Description: 隱藏單元格內容 
  791.      */  
  792.     public void setTcHidden(Tc tc, boolean hidden) {  
  793.         List<P> pList = getTcAllP(tc);  
  794.         for (P p : pList) {  
  795.             PPr ppr = getPPr(p);  
  796.             List<Object> objRList = getAllElementFromObject(p, R.class);  
  797.             if (objRList == null) {  
  798.                 continue;  
  799.             }  
  800.             for (Object objR : objRList) {  
  801.                 if (objR instanceof R) {  
  802.                     R r = (R) objR;  
  803.                     RPr rpr = getRPr(r);  
  804.                     setRPrVanishStyle(rpr, hidden);  
  805.                 }  
  806.             }  
  807.             setParaVanish(ppr, hidden);  
  808.         }  
  809.     }  
  810.   
  811.     public List<P> getTcAllP(Tc tc) {  
  812.         List<Object> objList = getAllElementFromObject(tc, P.class);  
  813.         List<P> pList = new ArrayList<P>();  
  814.         if (objList == null) {  
  815.             return pList;  
  816.         }  
  817.         for (Object obj : objList) {  
  818.             if (obj instanceof P) {  
  819.                 P p = (P) obj;  
  820.                 pList.add(p);  
  821.             }  
  822.         }  
  823.         return pList;  
  824.     }  
  825.   
  826.     public TcPr getTcPr(Tc tc) {  
  827.         TcPr tcPr = tc.getTcPr();  
  828.         if (tcPr == null) {  
  829.             tcPr = new TcPr();  
  830.             tc.setTcPr(tcPr);  
  831.         }  
  832.         return tcPr;  
  833.     }  
  834.   
  835.     /** 
  836.      * @Description: 設置單元格垂直對齊方式 
  837.      */  
  838.     public void setTcVAlign(Tc tc, STVerticalJc vAlignType) {  
  839.         if (vAlignType != null) {  
  840.             TcPr tcPr = getTcPr(tc);  
  841.             CTVerticalJc vAlign = new CTVerticalJc();  
  842.             vAlign.setVal(vAlignType);  
  843.             tcPr.setVAlign(vAlign);  
  844.         }  
  845.     }  
  846.   
  847.     /** 
  848.      * @Description: 設置單元格水平對齊方式 
  849.      */  
  850.     public void setTcJcAlign(Tc tc, JcEnumeration jcType) {  
  851.         if (jcType != null) {  
  852.             List<P> pList = getTcAllP(tc);  
  853.             for (P p : pList) {  
  854.                 setParaJcAlign(p, jcType);  
  855.             }  
  856.         }  
  857.     }  
  858.   
  859.     public RPr getRPr(R r) {  
  860.         RPr rpr = r.getRPr();  
  861.         if (rpr == null) {  
  862.             rpr = new RPr();  
  863.             r.setRPr(rpr);  
  864.         }  
  865.         return rpr;  
  866.     }  
  867.   
  868.     /** 
  869.      * @Description: 獲取所有的單元格 
  870.      */  
  871.     public List<Tc> getTrAllCell(Tr tr) {  
  872.         List<Object> objList = getAllElementFromObject(tr, Tc.class);  
  873.         List<Tc> tcList = new ArrayList<Tc>();  
  874.         if (objList == null) {  
  875.             return tcList;  
  876.         }  
  877.         for (Object tcObj : objList) {  
  878.             if (tcObj instanceof Tc) {  
  879.                 Tc objTc = (Tc) tcObj;  
  880.                 tcList.add(objTc);  
  881.             }  
  882.         }  
  883.         return tcList;  
  884.     }  
  885.   
  886.     /** 
  887.      * @Description: 獲取單元格內容 
  888.      */  
  889.     public String getTcContent(Tc tc) throws Exception {  
  890.         return getElementContent(tc);  
  891.     }  
  892.   
  893.     /** 
  894.      * @Description:設置單元格內容,content爲null則清除單元格內容 
  895.      */  
  896.     public void setTcContent(Tc tc, RPr rpr, String content) {  
  897.         List<Object> pList = tc.getContent();  
  898.         P p = null;  
  899.         if (pList != null && pList.size() > 0) {  
  900.             if (pList.get(0instanceof P) {  
  901.                 p = (P) pList.get(0);  
  902.             }  
  903.         } else {  
  904.             p = new P();  
  905.             tc.getContent().add(p);  
  906.         }  
  907.         R run = null;  
  908.         List<Object> rList = p.getContent();  
  909.         if (rList != null && rList.size() > 0) {  
  910.             for (int i = 0, len = rList.size(); i < len; i++) {  
  911.                 // 清除內容(所有的r  
  912.                 p.getContent().remove(0);  
  913.             }  
  914.         }  
  915.         run = new R();  
  916.         p.getContent().add(run);  
  917.         if (content != null) {  
  918.             String[] contentArr = content.split("\n");  
  919.             Text text = new Text();  
  920.             text.setSpace("preserve");  
  921.             text.setValue(contentArr[0]);  
  922.             run.setRPr(rpr);  
  923.             run.getContent().add(text);  
  924.   
  925.             for (int i = 1, len = contentArr.length; i < len; i++) {  
  926.                 Br br = new Br();  
  927.                 run.getContent().add(br);// 換行  
  928.                 text = new Text();  
  929.                 text.setSpace("preserve");  
  930.                 text.setValue(contentArr[i]);  
  931.                 run.setRPr(rpr);  
  932.                 run.getContent().add(text);  
  933.             }  
  934.         }  
  935.     }  
  936.   
  937.     /** 
  938.      * @Description:設置單元格內容,content爲null則清除單元格內容 
  939.      */  
  940.     public void removeTcContent(Tc tc) {  
  941.         List<Object> pList = tc.getContent();  
  942.         P p = null;  
  943.         if (pList != null && pList.size() > 0) {  
  944.             if (pList.get(0instanceof P) {  
  945.                 p = (P) pList.get(0);  
  946.             }  
  947.         } else {  
  948.             return;  
  949.         }  
  950.         List<Object> rList = p.getContent();  
  951.         if (rList != null && rList.size() > 0) {  
  952.             for (int i = 0, len = rList.size(); i < len; i++) {  
  953.                 // 清除內容(所有的r  
  954.                 p.getContent().remove(0);  
  955.             }  
  956.         }  
  957.     }  
  958.   
  959.     /** 
  960.      * @Description:刪除指定位置的表格 
  961.      * @deprecated 
  962.      */  
  963.     public void deleteTableByIndex2(WordprocessingMLPackage wordMLPackage,  
  964.             int index) throws Exception {  
  965.         if (index < 0) {  
  966.             return;  
  967.         }  
  968.         final String xpath = "(//w:tbl)[" + index + "]";  
  969.         final List<Object> jaxbNodes = wordMLPackage.getMainDocumentPart()  
  970.                 .getJAXBNodesViaXPath(xpath, true);  
  971.         if (jaxbNodes != null && jaxbNodes.size() > 0) {  
  972.             wordMLPackage.getMainDocumentPart().getContent()  
  973.                     .remove(jaxbNodes.get(0));  
  974.         }  
  975.     }  
  976.   
  977.     /** 
  978.      * @Description:獲取NodeList 
  979.      * @deprecated 
  980.      */  
  981.     public List<Object> getObjectByXpath(WordprocessingMLPackage wordMLPackage,  
  982.             String xpath) throws Exception {  
  983.         final List<Object> jaxbNodes = wordMLPackage.getMainDocumentPart()  
  984.                 .getJAXBNodesViaXPath(xpath, true);  
  985.         return jaxbNodes;  
  986.     }  
  987.   
  988.     /*------------------------------------Word 段落相關---------------------------------------------------  */  
  989.     /** 
  990.      * @Description: 只刪除單獨的段落,不包括表格內或其他內的段落 
  991.      */  
  992.     public boolean removeParaByIndex(WordprocessingMLPackage wordMLPackage,  
  993.             int index) {  
  994.         boolean flag = false;  
  995.         if (index < 0) {  
  996.             return flag;  
  997.         }  
  998.         List<Object> objList = wordMLPackage.getMainDocumentPart().getContent();  
  999.         if (objList == null) {  
  1000.             return flag;  
  1001.         }  
  1002.         int k = -1;  
  1003.         for (int i = 0, len = objList.size(); i < len; i++) {  
  1004.             if (objList.get(i) instanceof P) {  
  1005.                 k++;  
  1006.                 if (k == index) {  
  1007.                     wordMLPackage.getMainDocumentPart().getContent().remove(i);  
  1008.                     flag = true;  
  1009.                     break;  
  1010.                 }  
  1011.             }  
  1012.         }  
  1013.         return flag;  
  1014.     }  
  1015.   
  1016.     /** 
  1017.      * @Description: 設置段落水平對齊方式 
  1018.      */  
  1019.     public void setParaJcAlign(P paragraph, JcEnumeration hAlign) {  
  1020.         if (hAlign != null) {  
  1021.             PPr pprop = paragraph.getPPr();  
  1022.             if (pprop == null) {  
  1023.                 pprop = new PPr();  
  1024.                 paragraph.setPPr(pprop);  
  1025.             }  
  1026.             Jc align = new Jc();  
  1027.             align.setVal(hAlign);  
  1028.             pprop.setJc(align);  
  1029.         }  
  1030.     }  
  1031.   
  1032.     /** 
  1033.      * @Description: 設置段落內容 
  1034.      */  
  1035.     public void setParaRContent(P p, RPr runProperties, String content) {  
  1036.         R run = null;  
  1037.         List<Object> rList = p.getContent();  
  1038.         if (rList != null && rList.size() > 0) {  
  1039.             for (int i = 0, len = rList.size(); i < len; i++) {  
  1040.                 // 清除內容(所有的r  
  1041.                 p.getContent().remove(0);  
  1042.             }  
  1043.         }  
  1044.         run = new R();  
  1045.         p.getContent().add(run);  
  1046.         if (content != null) {  
  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 appendParaRContent(P p, RPr runProperties, String content) {  
  1070.         if (content != null) {  
  1071.             R run = new R();  
  1072.             p.getContent().add(run);  
  1073.             String[] contentArr = content.split("\n");  
  1074.             Text text = new Text();  
  1075.             text.setSpace("preserve");  
  1076.             text.setValue(contentArr[0]);  
  1077.             run.setRPr(runProperties);  
  1078.             run.getContent().add(text);  
  1079.   
  1080.             for (int i = 1, len = contentArr.length; i < len; i++) {  
  1081.                 Br br = new Br();  
  1082.                 run.getContent().add(br);// 換行  
  1083.                 text = new Text();  
  1084.                 text.setSpace("preserve");  
  1085.                 text.setValue(contentArr[i]);  
  1086.                 run.setRPr(runProperties);  
  1087.                 run.getContent().add(text);  
  1088.             }  
  1089.         }  
  1090.     }  
  1091.   
  1092.     /** 
  1093.      * @Description: 添加圖片到段落 
  1094.      */  
  1095.     public void addImageToPara(WordprocessingMLPackage wordMLPackage,  
  1096.             ObjectFactory factory, P paragraph, String filePath,  
  1097.             String content, RPr rpr, String altText, int id1, int id2)  
  1098.             throws Exception {  
  1099.         R run = factory.createR();  
  1100.         if (content != null) {  
  1101.             Text text = factory.createText();  
  1102.             text.setValue(content);  
  1103.             text.setSpace("preserve");  
  1104.             run.setRPr(rpr);  
  1105.             run.getContent().add(text);  
  1106.         }  
  1107.   
  1108.         InputStream is = new FileInputStream(filePath);  
  1109.         byte[] bytes = IOUtils.toByteArray(is);  
  1110.         BinaryPartAbstractImage imagePart = BinaryPartAbstractImage  
  1111.                 .createImagePart(wordMLPackage, bytes);  
  1112.         Inline inline = imagePart.createImageInline(filePath, altText, id1,  
  1113.                 id2, false);  
  1114.         Drawing drawing = factory.createDrawing();  
  1115.         drawing.getAnchorOrInline().add(inline);  
  1116.         run.getContent().add(drawing);  
  1117.         paragraph.getContent().add(run);  
  1118.     }  
  1119.   
  1120.     /** 
  1121.      * @Description: 段落添加Br 頁面Break(分頁符) 
  1122.      */  
  1123.     public void addPageBreak(P para, STBrType sTBrType) {  
  1124.         Br breakObj = new Br();  
  1125.         breakObj.setType(sTBrType);  
  1126.         para.getContent().add(breakObj);  
  1127.     }  
  1128.   
  1129.     /** 
  1130.      * @Description: 設置段落是否禁止行號(禁止用於當前行號) 
  1131.      */  
  1132.     public void setParagraphSuppressLineNum(P p) {  
  1133.         PPr ppr = getPPr(p);  
  1134.         BooleanDefaultTrue line = ppr.getSuppressLineNumbers();  
  1135.         if (line == null) {  
  1136.             line = new BooleanDefaultTrue();  
  1137.         }  
  1138.         line.setVal(true);  
  1139.         ppr.setSuppressLineNumbers(line);  
  1140.     }  
  1141.   
  1142.     /** 
  1143.      * @Description: 設置段落底紋(對整段文字起作用) 
  1144.      */  
  1145.     public void setParagraphShdStyle(P p, STShd shdType, String shdColor) {  
  1146.         PPr ppr = getPPr(p);  
  1147.         CTShd ctShd = ppr.getShd();  
  1148.         if (ctShd == null) {  
  1149.             ctShd = new CTShd();  
  1150.         }  
  1151.         if (StringUtils.isNotBlank(shdColor)) {  
  1152.             ctShd.setColor(shdColor);  
  1153.         }  
  1154.         if (shdType != null) {  
  1155.             ctShd.setVal(shdType);  
  1156.         }  
  1157.         ppr.setShd(ctShd);  
  1158.     }  
  1159.   
  1160.     /** 
  1161.      * @param isSpace 
  1162.      *            是否設置段前段後值 
  1163.      * @param before 
  1164.      *            段前磅數 
  1165.      * @param after 
  1166.      *            段後磅數 
  1167.      * @param beforeLines 
  1168.      *            段前行數 
  1169.      * @param afterLines 
  1170.      *            段後行數 
  1171.      * @param isLine 
  1172.      *            是否設置行距 
  1173.      * @param lineValue 
  1174.      *            行距值 
  1175.      * @param sTLineSpacingRule 
  1176.      *            自動auto 固定exact 最小 atLeast 1磅=20 1行=100 單倍行距=240 
  1177.      */  
  1178.     public void setParagraphSpacing(P p, boolean isSpace, String before,  
  1179.             String after, String beforeLines, String afterLines,  
  1180.             boolean isLine, String lineValue,  
  1181.             STLineSpacingRule sTLineSpacingRule) {  
  1182.         PPr pPr = getPPr(p);  
  1183.         Spacing spacing = pPr.getSpacing();  
  1184.         if (spacing == null) {  
  1185.             spacing = new Spacing();  
  1186.             pPr.setSpacing(spacing);  
  1187.         }  
  1188.         if (isSpace) {  
  1189.             if (StringUtils.isNotBlank(before)) {  
  1190.                 // 段前磅數  
  1191.                 spacing.setBefore(new BigInteger(before));  
  1192.             }  
  1193.             if (StringUtils.isNotBlank(after)) {  
  1194.                 // 段後磅數  
  1195.                 spacing.setAfter(new BigInteger(after));  
  1196.             }  
  1197.             if (StringUtils.isNotBlank(beforeLines)) {  
  1198.                 // 段前行數  
  1199.                 spacing.setBeforeLines(new BigInteger(beforeLines));  
  1200.             }  
  1201.             if (StringUtils.isNotBlank(afterLines)) {  
  1202.                 // 段後行數  
  1203.                 spacing.setAfterLines(new BigInteger(afterLines));  
  1204.             }  
  1205.         }  
  1206.         if (isLine) {  
  1207.             if (StringUtils.isNotBlank(lineValue)) {  
  1208.                 spacing.setLine(new BigInteger(lineValue));  
  1209.             }  
  1210.             if (sTLineSpacingRule != null) {  
  1211.                 spacing.setLineRule(sTLineSpacingRule);  
  1212.             }  
  1213.         }  
  1214.     }  
  1215.   
  1216.     /** 
  1217.      * @Description: 設置段落縮進信息 1釐米≈567 
  1218.      */  
  1219.     public void setParagraphIndInfo(P p, String firstLine,  
  1220.             String firstLineChar, String hanging, String hangingChar,  
  1221.             String right, String rigthChar, String left, String leftChar) {  
  1222.         PPr ppr = getPPr(p);  
  1223.         Ind ind = ppr.getInd();  
  1224.         if (ind == null) {  
  1225.             ind = new Ind();  
  1226.             ppr.setInd(ind);  
  1227.         }  
  1228.         if (StringUtils.isNotBlank(firstLine)) {  
  1229.             ind.setFirstLine(new BigInteger(firstLine));  
  1230.         }  
  1231.         if (StringUtils.isNotBlank(firstLineChar)) {  
  1232.             ind.setFirstLineChars(new BigInteger(firstLineChar));  
  1233.         }  
  1234.         if (StringUtils.isNotBlank(hanging)) {  
  1235.             ind.setHanging(new BigInteger(hanging));  
  1236.         }  
  1237.         if (StringUtils.isNotBlank(hangingChar)) {  
  1238.             ind.setHangingChars(new BigInteger(hangingChar));  
  1239.         }  
  1240.         if (StringUtils.isNotBlank(left)) {  
  1241.             ind.setLeft(new BigInteger(left));  
  1242.         }  
  1243.         if (StringUtils.isNotBlank(leftChar)) {  
  1244.             ind.setLeftChars(new BigInteger(leftChar));  
  1245.         }  
  1246.         if (StringUtils.isNotBlank(right)) {  
  1247.             ind.setRight(new BigInteger(right));  
  1248.         }  
  1249.         if (StringUtils.isNotBlank(rigthChar)) {  
  1250.             ind.setRightChars(new BigInteger(rigthChar));  
  1251.         }  
  1252.     }  
  1253.   
  1254.     public PPr getPPr(P p) {  
  1255.         PPr ppr = p.getPPr();  
  1256.         if (ppr == null) {  
  1257.             ppr = new PPr();  
  1258.             p.setPPr(ppr);  
  1259.         }  
  1260.         return ppr;  
  1261.     }  
  1262.   
  1263.     public ParaRPr getParaRPr(PPr ppr) {  
  1264.         ParaRPr parRpr = ppr.getRPr();  
  1265.         if (parRpr == null) {  
  1266.             parRpr = new ParaRPr();  
  1267.             ppr.setRPr(parRpr);  
  1268.         }  
  1269.         return parRpr;  
  1270.   
  1271.     }  
  1272.   
  1273.     public void setParaVanish(PPr ppr, boolean isVanish) {  
  1274.         ParaRPr parRpr = getParaRPr(ppr);  
  1275.         BooleanDefaultTrue vanish = parRpr.getVanish();  
  1276.         if (vanish != null) {  
  1277.             vanish.setVal(isVanish);  
  1278.         } else {  
  1279.             vanish = new BooleanDefaultTrue();  
  1280.             parRpr.setVanish(vanish);  
  1281.             vanish.setVal(isVanish);  
  1282.         }  
  1283.     }  
  1284.   
  1285.     /** 
  1286.      * @Description: 設置段落邊框樣式 
  1287.      */  
  1288.     public void setParagraghBorders(P p, CTBorder topBorder,  
  1289.             CTBorder bottomBorder, CTBorder leftBorder, CTBorder rightBorder) {  
  1290.         PPr ppr = getPPr(p);  
  1291.         PBdr pBdr = new PBdr();  
  1292.         if (topBorder != null) {  
  1293.             pBdr.setTop(topBorder);  
  1294.         }  
  1295.         if (bottomBorder != null) {  
  1296.             pBdr.setBottom(bottomBorder);  
  1297.         }  
  1298.         if (leftBorder != null) {  
  1299.             pBdr.setLeft(leftBorder);  
  1300.         }  
  1301.         if (rightBorder != null) {  
  1302.             pBdr.setRight(rightBorder);  
  1303.         }  
  1304.         ppr.setPBdr(pBdr);  
  1305.     }  
  1306.   
  1307.     /** 
  1308.      * @Description: 設置字體信息 
  1309.      */  
  1310.     public void setFontStyle(RPr runProperties, String cnFontFamily,  
  1311.             String enFontFamily, String fontSize, String color) {  
  1312.         setFontFamily(runProperties, cnFontFamily, enFontFamily);  
  1313.         setFontSize(runProperties, fontSize);  
  1314.         setFontColor(runProperties, color);  
  1315.     }  
  1316.   
  1317.     /** 
  1318.      * @Description: 設置字體大小 
  1319.      */  
  1320.     public void setFontSize(RPr runProperties, String fontSize) {  
  1321.         if (StringUtils.isNotBlank(fontSize)) {  
  1322.             HpsMeasure size = new HpsMeasure();  
  1323.             size.setVal(new BigInteger(fontSize));  
  1324.             runProperties.setSz(size);  
  1325.             runProperties.setSzCs(size);  
  1326.         }  
  1327.     }  
  1328.   
  1329.     /** 
  1330.      * @Description: 設置字體 
  1331.      */  
  1332.     public void setFontFamily(RPr runProperties, String cnFontFamily,  
  1333.             String enFontFamily) {  
  1334.         if (StringUtils.isNotBlank(cnFontFamily)  
  1335.                 || StringUtils.isNotBlank(enFontFamily)) {  
  1336.             RFonts rf = runProperties.getRFonts();  
  1337.             if (rf == null) {  
  1338.                 rf = new RFonts();  
  1339.                 runProperties.setRFonts(rf);  
  1340.             }  
  1341.             if (cnFontFamily != null) {  
  1342.                 rf.setEastAsia(cnFontFamily);  
  1343.             }  
  1344.             if (enFontFamily != null) {  
  1345.                 rf.setAscii(enFontFamily);  
  1346.             }  
  1347.         }  
  1348.     }  
  1349.   
  1350.     /** 
  1351.      * @Description: 設置字體顏色 
  1352.      */  
  1353.     public void setFontColor(RPr runProperties, String color) {  
  1354.         if (color != null) {  
  1355.             Color c = new Color();  
  1356.             c.setVal(color);  
  1357.             runProperties.setColor(c);  
  1358.         }  
  1359.     }  
  1360.   
  1361.     /** 
  1362.      * @Description: 設置字符邊框 
  1363.      */  
  1364.     public void addRPrBorderStyle(RPr runProperties, String size,  
  1365.             STBorder bordType, String space, String color) {  
  1366.         CTBorder value = new CTBorder();  
  1367.         if (StringUtils.isNotBlank(color)) {  
  1368.             value.setColor(color);  
  1369.         }  
  1370.         if (StringUtils.isNotBlank(size)) {  
  1371.             value.setSz(new BigInteger(size));  
  1372.         }  
  1373.         if (StringUtils.isNotBlank(space)) {  
  1374.             value.setSpace(new BigInteger(space));  
  1375.         }  
  1376.         if (bordType != null) {  
  1377.             value.setVal(bordType);  
  1378.         }  
  1379.         runProperties.setBdr(value);  
  1380.     }  
  1381.   
  1382.     /** 
  1383.      * @Description:着重號 
  1384.      */  
  1385.     public void addRPrEmStyle(RPr runProperties, STEm emType) {  
  1386.         if (emType != null) {  
  1387.             CTEm em = new CTEm();  
  1388.             em.setVal(emType);  
  1389.             runProperties.setEm(em);  
  1390.         }  
  1391.     }  
  1392.   
  1393.     /** 
  1394.      * @Description: 空心 
  1395.      */  
  1396.     public void addRPrOutlineStyle(RPr runProperties) {  
  1397.         BooleanDefaultTrue outline = new BooleanDefaultTrue();  
  1398.         outline.setVal(true);  
  1399.         runProperties.setOutline(outline);  
  1400.     }  
  1401.   
  1402.     /** 
  1403.      * @Description: 設置上標下標 
  1404.      */  
  1405.     public void addRPrcaleStyle(RPr runProperties, STVerticalAlignRun vAlign) {  
  1406.         if (vAlign != null) {  
  1407.             CTVerticalAlignRun value = new CTVerticalAlignRun();  
  1408.             value.setVal(vAlign);  
  1409.             runProperties.setVertAlign(value);  
  1410.         }  
  1411.     }  
  1412.   
  1413.     /** 
  1414.      * @Description: 設置字符間距縮進 
  1415.      */  
  1416.     public void addRPrScaleStyle(RPr runProperties, int indent) {  
  1417.         CTTextScale value = new CTTextScale();  
  1418.         value.setVal(indent);  
  1419.         runProperties.setW(value);  
  1420.     }  
  1421.   
  1422.     /** 
  1423.      * @Description: 設置字符間距信息 
  1424.      */  
  1425.     public void addRPrtSpacingStyle(RPr runProperties, int spacing) {  
  1426.         CTSignedTwipsMeasure value = new CTSignedTwipsMeasure();  
  1427.         value.setVal(BigInteger.valueOf(spacing));  
  1428.         runProperties.setSpacing(value);  
  1429.     }  
  1430.   
  1431.     /** 
  1432.      * @Description: 設置文本位置 
  1433.      */  
  1434.     public void addRPrtPositionStyle(RPr runProperties, int position) {  
  1435.         CTSignedHpsMeasure ctPosition = new CTSignedHpsMeasure();  
  1436.         ctPosition.setVal(BigInteger.valueOf(position));  
  1437.         runProperties.setPosition(ctPosition);  
  1438.     }  
  1439.   
  1440.     /** 
  1441.      * @Description: 陰文 
  1442.      */  
  1443.     public void addRPrImprintStyle(RPr runProperties) {  
  1444.         BooleanDefaultTrue imprint = new BooleanDefaultTrue();  
  1445.         imprint.setVal(true);  
  1446.         runProperties.setImprint(imprint);  
  1447.     }  
  1448.   
  1449.     /** 
  1450.      * @Description: 陽文 
  1451.      */  
  1452.     public void addRPrEmbossStyle(RPr runProperties) {  
  1453.         BooleanDefaultTrue emboss = new BooleanDefaultTrue();  
  1454.         emboss.setVal(true);  
  1455.         runProperties.setEmboss(emboss);  
  1456.     }  
  1457.   
  1458.     /** 
  1459.      * @Description: 設置隱藏 
  1460.      */  
  1461.     public void setRPrVanishStyle(RPr runProperties, boolean isVanish) {  
  1462.         BooleanDefaultTrue vanish = runProperties.getVanish();  
  1463.         if (vanish != null) {  
  1464.             vanish.setVal(isVanish);  
  1465.         } else {  
  1466.             vanish = new BooleanDefaultTrue();  
  1467.             vanish.setVal(isVanish);  
  1468.             runProperties.setVanish(vanish);  
  1469.         }  
  1470.     }  
  1471.   
  1472.     /** 
  1473.      * @Description: 設置陰影 
  1474.      */  
  1475.     public void addRPrShadowStyle(RPr runProperties) {  
  1476.         BooleanDefaultTrue shadow = new BooleanDefaultTrue();  
  1477.         shadow.setVal(true);  
  1478.         runProperties.setShadow(shadow);  
  1479.     }  
  1480.   
  1481.     /** 
  1482.      * @Description: 設置底紋 
  1483.      */  
  1484.     public void addRPrShdStyle(RPr runProperties, STShd shdtype) {  
  1485.         if (shdtype != null) {  
  1486.             CTShd shd = new CTShd();  
  1487.             shd.setVal(shdtype);  
  1488.             runProperties.setShd(shd);  
  1489.         }  
  1490.     }  
  1491.   
  1492.     /** 
  1493.      * @Description: 設置突出顯示文本 
  1494.      */  
  1495.     public void addRPrHightLightStyle(RPr runProperties, String hightlight) {  
  1496.         if (StringUtils.isNotBlank(hightlight)) {  
  1497.             Highlight highlight = new Highlight();  
  1498.             highlight.setVal(hightlight);  
  1499.             runProperties.setHighlight(highlight);  
  1500.         }  
  1501.     }  
  1502.   
  1503.     /** 
  1504.      * @Description: 設置刪除線樣式 
  1505.      */  
  1506.     public void addRPrStrikeStyle(RPr runProperties, boolean isStrike,  
  1507.             boolean isDStrike) {  
  1508.         // 刪除線  
  1509.         if (isStrike) {  
  1510.             BooleanDefaultTrue strike = new BooleanDefaultTrue();  
  1511.             strike.setVal(true);  
  1512.             runProperties.setStrike(strike);  
  1513.         }  
  1514.         // 雙刪除線  
  1515.         if (isDStrike) {  
  1516.             BooleanDefaultTrue dStrike = new BooleanDefaultTrue();  
  1517.             dStrike.setVal(true);  
  1518.             runProperties.setDstrike(dStrike);  
  1519.         }  
  1520.     }  
  1521.   
  1522.     /** 
  1523.      * @Description: 加粗 
  1524.      */  
  1525.     public void addRPrBoldStyle(RPr runProperties) {  
  1526.         BooleanDefaultTrue b = new BooleanDefaultTrue();  
  1527.         b.setVal(true);  
  1528.         runProperties.setB(b);  
  1529.     }  
  1530.   
  1531.     /** 
  1532.      * @Description: 傾斜 
  1533.      */  
  1534.     public void addRPrItalicStyle(RPr runProperties) {  
  1535.         BooleanDefaultTrue b = new BooleanDefaultTrue();  
  1536.         b.setVal(true);  
  1537.         runProperties.setI(b);  
  1538.     }  
  1539.   
  1540.     /** 
  1541.      * @Description: 添加下劃線 
  1542.      */  
  1543.     public void addRPrUnderlineStyle(RPr runProperties,  
  1544.             UnderlineEnumeration enumType) {  
  1545.         U val = new U();  
  1546.         val.setVal(enumType);  
  1547.         runProperties.setU(val);  
  1548.     }  
  1549.   
  1550.     /*------------------------------------Word 相關---------------------------------------------------  */  
  1551.     /** 
  1552.      * @Description: 設置分節符 nextPage:下一頁 continuous:連續 evenPage:偶數頁 oddPage:奇數頁 
  1553.      */  
  1554.     public void setDocSectionBreak(WordprocessingMLPackage wordPackage,  
  1555.             String sectValType) {  
  1556.         if (StringUtils.isNotBlank(sectValType)) {  
  1557.             SectPr sectPr = getDocSectPr(wordPackage);  
  1558.             Type sectType = sectPr.getType();  
  1559.             if (sectType == null) {  
  1560.                 sectType = new Type();  
  1561.                 sectPr.setType(sectType);  
  1562.             }  
  1563.             sectType.setVal(sectValType);  
  1564.         }  
  1565.     }  
  1566.   
  1567.     /** 
  1568.      * @Description: 設置頁面背景色 
  1569.      */  
  1570.     public void setDocumentBackGround(WordprocessingMLPackage wordPackage,  
  1571.             ObjectFactory factory, String color) throws Exception {  
  1572.         MainDocumentPart mdp = wordPackage.getMainDocumentPart();  
  1573.         CTBackground bkground = mdp.getContents().getBackground();  
  1574.         if (StringUtils.isNotBlank(color)) {  
  1575.             if (bkground == null) {  
  1576.                 bkground = factory.createCTBackground();  
  1577.                 bkground.setColor(color);  
  1578.             }  
  1579.             mdp.getContents().setBackground(bkground);  
  1580.         }  
  1581.     }  
  1582.   
  1583.     /** 
  1584.      * @Description: 設置頁面邊框 
  1585.      */  
  1586.     public void setDocumentBorders(WordprocessingMLPackage wordPackage,  
  1587.             ObjectFactory factory, CTBorder top, CTBorder right,  
  1588.             CTBorder bottom, CTBorder left) {  
  1589.         SectPr sectPr = getDocSectPr(wordPackage);  
  1590.         PgBorders pgBorders = sectPr.getPgBorders();  
  1591.         if (pgBorders == null) {  
  1592.             pgBorders = factory.createSectPrPgBorders();  
  1593.             sectPr.setPgBorders(pgBorders);  
  1594.         }  
  1595.         if (top != null) {  
  1596.             pgBorders.setTop(top);  
  1597.         }  
  1598.         if (right != null) {  
  1599.             pgBorders.setRight(right);  
  1600.         }  
  1601.         if (bottom != null) {  
  1602.             pgBorders.setBottom(bottom);  
  1603.         }  
  1604.         if (left != null) {  
  1605.             pgBorders.setLeft(left);  
  1606.         }  
  1607.     }  
  1608.   
  1609.     /** 
  1610.      * @Description: 設置頁面大小及紙張方向 landscape橫向 
  1611.      */  
  1612.     public void setDocumentSize(WordprocessingMLPackage wordPackage,  
  1613.             ObjectFactory factory, String width, String height,  
  1614.             STPageOrientation stValue) {  
  1615.         SectPr sectPr = getDocSectPr(wordPackage);  
  1616.         PgSz pgSz = sectPr.getPgSz();  
  1617.         if (pgSz == null) {  
  1618.             pgSz = factory.createSectPrPgSz();  
  1619.             sectPr.setPgSz(pgSz);  
  1620.         }  
  1621.         if (StringUtils.isNotBlank(width)) {  
  1622.             pgSz.setW(new BigInteger(width));  
  1623.         }  
  1624.         if (StringUtils.isNotBlank(height)) {  
  1625.             pgSz.setH(new BigInteger(height));  
  1626.         }  
  1627.         if (stValue != null) {  
  1628.             pgSz.setOrient(stValue);  
  1629.         }  
  1630.     }  
  1631.   
  1632.     public SectPr getDocSectPr(WordprocessingMLPackage wordPackage) {  
  1633.         SectPr sectPr = wordPackage.getDocumentModel().getSections().get(0)  
  1634.                 .getSectPr();  
  1635.         return sectPr;  
  1636.     }  
  1637.   
  1638.     /** 
  1639.      * @Description:設置頁邊距 
  1640.      */  
  1641.     public void setDocMarginSpace(WordprocessingMLPackage wordPackage,  
  1642.             ObjectFactory factory, String top, String left, String bottom,  
  1643.             String right) {  
  1644.         SectPr sectPr = getDocSectPr(wordPackage);  
  1645.         PgMar pg = sectPr.getPgMar();  
  1646.         if (pg == null) {  
  1647.             pg = factory.createSectPrPgMar();  
  1648.             sectPr.setPgMar(pg);  
  1649.         }  
  1650.         if (StringUtils.isNotBlank(top)) {  
  1651.             pg.setTop(new BigInteger(top));  
  1652.         }  
  1653.         if (StringUtils.isNotBlank(bottom)) {  
  1654.             pg.setBottom(new BigInteger(bottom));  
  1655.         }  
  1656.         if (StringUtils.isNotBlank(left)) {  
  1657.             pg.setLeft(new BigInteger(left));  
  1658.         }  
  1659.         if (StringUtils.isNotBlank(right)) {  
  1660.             pg.setRight(new BigInteger(right));  
  1661.         }  
  1662.     }  
  1663.   
  1664.     /** 
  1665.      * @Description: 設置行號 
  1666.      * @param distance 
  1667.      *            :距正文距離 1釐米=567 
  1668.      * @param start 
  1669.      *            :起始編號(0開始) 
  1670.      * @param countBy 
  1671.      *            :行號間隔 
  1672.      * @param restartType 
  1673.      *            :STLineNumberRestart.CONTINUOUS(continuous連續編號)<br/> 
  1674.      *            STLineNumberRestart.NEW_PAGE(每頁重新編號)<br/> 
  1675.      *            STLineNumberRestart.NEW_SECTION(每節重新編號) 
  1676.      */  
  1677.     public void setDocInNumType(WordprocessingMLPackage wordPackage,  
  1678.             String countBy, String distance, String start,  
  1679.             STLineNumberRestart restartType) {  
  1680.         SectPr sectPr = getDocSectPr(wordPackage);  
  1681.         CTLineNumber lnNumType = sectPr.getLnNumType();  
  1682.         if (lnNumType == null) {  
  1683.             lnNumType = new CTLineNumber();  
  1684.             sectPr.setLnNumType(lnNumType);  
  1685.         }  
  1686.         if (StringUtils.isNotBlank(countBy)) {  
  1687.             lnNumType.setCountBy(new BigInteger(countBy));  
  1688.         }  
  1689.         if (StringUtils.isNotBlank(distance)) {  
  1690.             lnNumType.setDistance(new BigInteger(distance));  
  1691.         }  
  1692.         if (StringUtils.isNotBlank(start)) {  
  1693.             lnNumType.setStart(new BigInteger(start));  
  1694.         }  
  1695.         if (restartType != null) {  
  1696.             lnNumType.setRestart(restartType);  
  1697.         }  
  1698.     }  
  1699.   
  1700.     /** 
  1701.      * @Description:設置文字方向 tbRl 垂直 
  1702.      */  
  1703.     public void setDocTextDirection(WordprocessingMLPackage wordPackage,  
  1704.             String textDirection) {  
  1705.         if (StringUtils.isNotBlank(textDirection)) {  
  1706.             SectPr sectPr = getDocSectPr(wordPackage);  
  1707.             TextDirection textDir = sectPr.getTextDirection();  
  1708.             if (textDir == null) {  
  1709.                 textDir = new TextDirection();  
  1710.                 sectPr.setTextDirection(textDir);  
  1711.             }  
  1712.             textDir.setVal(textDirection);  
  1713.         }  
  1714.     }  
  1715.   
  1716.     /** 
  1717.      * @Description:設置word 垂直對齊方式(Word默認方式都是"頂端對齊") 
  1718.      */  
  1719.     public void setDocVAlign(WordprocessingMLPackage wordPackage,  
  1720.             STVerticalJc valignType) {  
  1721.         if (valignType != null) {  
  1722.             SectPr sectPr = getDocSectPr(wordPackage);  
  1723.             CTVerticalJc valign = sectPr.getVAlign();  
  1724.             if (valign == null) {  
  1725.                 valign = new CTVerticalJc();  
  1726.                 sectPr.setVAlign(valign);  
  1727.             }  
  1728.             valign.setVal(valignType);  
  1729.         }  
  1730.     }  
  1731.   
  1732.     /** 
  1733.      * @Description:獲取文檔的可用寬度 
  1734.      */  
  1735.     public int getWritableWidth(WordprocessingMLPackage wordPackage)  
  1736.             throws Exception {  
  1737.         return wordPackage.getDocumentModel().getSections().get(0)  
  1738.                 .getPageDimensions().getWritableWidthTwips();  
  1739.     }  
  1740.   
  1741. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章