package wordDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBElement;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.docx4j.TextUtils;
import org.docx4j.XmlUtils;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.model.properties.table.tr.TrHeight;
import org.docx4j.openpackaging.packages.OpcPackage;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.Br;
import org.docx4j.wml.CTBackground;
import org.docx4j.wml.CTBorder;
import org.docx4j.wml.CTEm;
import org.docx4j.wml.CTHeight;
import org.docx4j.wml.CTLineNumber;
import org.docx4j.wml.CTShd;
import org.docx4j.wml.CTSignedHpsMeasure;
import org.docx4j.wml.CTSignedTwipsMeasure;
import org.docx4j.wml.CTTblCellMar;
import org.docx4j.wml.CTTextScale;
import org.docx4j.wml.CTVerticalAlignRun;
import org.docx4j.wml.CTVerticalJc;
import org.docx4j.wml.Color;
import org.docx4j.wml.ContentAccessor;
import org.docx4j.wml.Drawing;
import org.docx4j.wml.Highlight;
import org.docx4j.wml.HpsMeasure;
import org.docx4j.wml.Jc;
import org.docx4j.wml.JcEnumeration;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.P.Hyperlink;
import org.docx4j.wml.PPr;
import org.docx4j.wml.PPrBase.Ind;
import org.docx4j.wml.PPrBase.PBdr;
import org.docx4j.wml.PPrBase.Spacing;
import org.docx4j.wml.ParaRPr;
import org.docx4j.wml.R;
import org.docx4j.wml.RFonts;
import org.docx4j.wml.RPr;
import org.docx4j.wml.STBorder;
import org.docx4j.wml.STBrType;
import org.docx4j.wml.STEm;
import org.docx4j.wml.STLineNumberRestart;
import org.docx4j.wml.STLineSpacingRule;
import org.docx4j.wml.STPageOrientation;
import org.docx4j.wml.STShd;
import org.docx4j.wml.STVerticalAlignRun;
import org.docx4j.wml.STVerticalJc;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.SectPr.PgBorders;
import org.docx4j.wml.SectPr.PgMar;
import org.docx4j.wml.SectPr.PgSz;
import org.docx4j.wml.SectPr.Type;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.TblBorders;
import org.docx4j.wml.TblGrid;
import org.docx4j.wml.TblGridCol;
import org.docx4j.wml.TblPr;
import org.docx4j.wml.TblWidth;
import org.docx4j.wml.Tc;
import org.docx4j.wml.TcPr;
import org.docx4j.wml.TcPrInner.GridSpan;
import org.docx4j.wml.TcPrInner.HMerge;
import org.docx4j.wml.TcPrInner.VMerge;
import org.docx4j.wml.Text;
import org.docx4j.wml.TextDirection;
import org.docx4j.wml.Tr;
import org.docx4j.wml.TrPr;
import org.docx4j.wml.U;
import org.docx4j.wml.UnderlineEnumeration;
public class workDealDoc4j {
/*------------------------------------other--------------------------------------------------- */
/**
* @Description:新增超链接
*/
public void createHyperlink(WordprocessingMLPackage wordMLPackage,
MainDocumentPart mainPart, ObjectFactory factory, P paragraph,
String url, String value, String cnFontName, String enFontName,
String fontSize) throws Exception {
if (StringUtils.isBlank(enFontName)) {
enFontName = "Times New Roman";
}
if (StringUtils.isBlank(cnFontName)) {
cnFontName = "微软雅黑";
}
if (StringUtils.isBlank(fontSize)) {
fontSize = "22";
}
org.docx4j.relationships.ObjectFactory reFactory = new org.docx4j.relationships.ObjectFactory();
org.docx4j.relationships.Relationship rel = reFactory
.createRelationship();
rel.setType(Namespaces.HYPERLINK);
rel.setTarget(url);
rel.setTargetMode("External");
mainPart.getRelationshipsPart().addRelationship(rel);
StringBuffer sb = new StringBuffer();
// addRelationship sets the rel's @Id
sb.append("<w:hyperlink r:id=\"");
sb.append(rel.getId());
sb.append("\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ");
sb.append("xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >");
sb.append("<w:r><w:rPr><w:rStyle w:val=\"Hyperlink\" />");
sb.append("<w:rFonts w:ascii=\"");
sb.append(enFontName);
sb.append("\" w:hAnsi=\"");
sb.append(enFontName);
sb.append("\" w:eastAsia=\"");
sb.append(cnFontName);
sb.append("\" w:hint=\"eastAsia\"/>");
sb.append("<w:sz w:val=\"");
sb.append(fontSize);
sb.append("\"/><w:szCs w:val=\"");
sb.append(fontSize);
sb.append("\"/></w:rPr><w:t>");
sb.append(value);
sb.append("</w:t></w:r></w:hyperlink>");
Hyperlink link = (Hyperlink) XmlUtils.unmarshalString(sb.toString());
paragraph.getContent().add(link);
}
public String getElementContent(Object obj) throws Exception {
StringWriter stringWriter = new StringWriter();
TextUtils.extractText(obj, stringWriter);
return stringWriter.toString();
}
/**
* @Description:得到指定类型的元素
*/
public static List<Object> getAllElementFromObject(Object obj,
Class<?> toSearch) {
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement)
obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch))
result.add(obj);
else if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
return result;
}
/**
* @Description:保存WordprocessingMLPackage
*/
public void saveWordPackage(WordprocessingMLPackage wordPackage, File file)
throws Exception {
wordPackage.save(file);
}
/**
* @Description:新建WordprocessingMLPackage
*/
public WordprocessingMLPackage createWordprocessingMLPackage()
throws Exception {
return WordprocessingMLPackage.createPackage();
}
/**
* @Description:加载带密码WordprocessingMLPackage
*/
public WordprocessingMLPackage loadWordprocessingMLPackageWithPwd(
String filePath, String password) throws Exception {
OpcPackage opcPackage = WordprocessingMLPackage.load(new java.io.File(
filePath), password);
WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage) opcPackage;
return wordMLPackage;
}
/**
* @Description:加载WordprocessingMLPackage
*/
public WordprocessingMLPackage loadWordprocessingMLPackage(String filePath)
throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(filePath));
return wordMLPackage;
}
/*------------------------------------Word 表格相关--------------------------------------------------- */
/**
* @Description: 跨列合并
*/
public void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell,
int toCell) {
if (row < 0 || fromCell < 0 || toCell < 0) {
return;
}
List<Tr> trList = getTblAllTr(tbl);
if (row > trList.size()) {
return;
}
Tr tr = trList.get(row);
List<Tc> tcList = getTrA