Search in sources :

Example 1 with TraversalUtil

use of org.docx4j.TraversalUtil in project docx4j-template by vindell.

the class Docx4j_替换模板 method replaceTemplateDocx.

public void replaceTemplateDocx() throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    HashMap<String, String> staticMap = getStaticData();
    // 替换普通变量
    documentPart.variableReplace(staticMap);
    Document document = (Document) documentPart.getContents();
    Body body = document.getBody();
    MyTblFinder tblFinder = new MyTblFinder();
    new TraversalUtil(body, tblFinder);
    // 替换表格模板第三行数据
    Tbl firstTbl = tblFinder.getTbls().get(0);
    List<Object> trObjList = firstTbl.getContent();
    Tr tr = (Tr) trObjList.get(2);
    replaceTrTotalData(tr, getTotalData());
    int lvlIndex = 3;
    // 替换表格模板第四行数据
    int lvlTotalSize = 4;
    tr = (Tr) trObjList.get(lvlIndex);
    List<String[]> lvDataList = getLvDataList(lvlTotalSize);
    replaceTrData(firstTbl, tr, lvDataList, lvlIndex);
    // 重新获取表格数据
    trObjList = firstTbl.getContent();
    int sexTotalSize = 2;
    // 替换表格模板第五行数据
    tr = (Tr) trObjList.get(lvlIndex + lvlTotalSize);
    List<String[]> sexDataList = getSexDataList(2);
    replaceTrSexData(tr, sexDataList.get(0));
    // 替换表格模板第六行数据
    tr = (Tr) trObjList.get(4 + lvlTotalSize);
    replaceTrSexData(tr, sexDataList.get(1));
    // 替换表格模板第七行数据
    tr = (Tr) trObjList.get(5 + lvlTotalSize);
    int nationTotalSize = 56;
    List<String[]> nationDataList = getNationDataList(nationTotalSize);
    replaceTrData(firstTbl, tr, nationDataList, lvlIndex + lvlTotalSize + sexTotalSize);
    // 合并层次单元格 层次位于第3行
    mergeCellsVertically(firstTbl, 0, lvlIndex, lvlIndex + lvlTotalSize - 1);
    // 合并民族单元格 层次+性别2行
    mergeCellsVertically(firstTbl, 0, lvlIndex + lvlTotalSize + sexTotalSize, lvlIndex + lvlTotalSize + sexTotalSize + nationTotalSize - 1);
    // 替换图表数据
    String[] chartArr = getChartData();
    replacePieChartData(wordMLPackage, chartArr);
    // 保存结果
    saveWordPackage(wordMLPackage, outputfilepath);
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Document(org.docx4j.wml.Document) File(java.io.File) TraversalUtil(org.docx4j.TraversalUtil) MyTblFinder(org.docx4j.template.wml.MyTblFinder) Body(org.docx4j.wml.Body) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Example 2 with TraversalUtil

use of org.docx4j.TraversalUtil in project docx4j-template by vindell.

the class SdtFinder method printSdtContent.

public void printSdtContent(String filePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    Document wmlDocumentEl = (Document) mdp.getContents();
    Body body = wmlDocumentEl.getBody();
    SdtFinder sdtFinder = new SdtFinder();
    new TraversalUtil(body, sdtFinder);
    for (Child sdtChild : sdtFinder.sdtList) {
        if (sdtChild instanceof SdtRun) {
            SdtRun sdtRun = (SdtRun) sdtChild;
            SdtPr sdtPr = sdtRun.getSdtPr();
            printSdtPrContent(sdtPr);
            // System.out.println(XmlUtils.marshaltoString(sdtPr, true,
            // true));
            SdtContent sdtContent = sdtRun.getSdtContent();
            System.out.println("-----------p content=" + getContentAccessorContent(sdtContent));
        } else if (sdtChild instanceof CTSdtCell) {
            CTSdtCell sdtCell = (CTSdtCell) sdtChild;
            SdtPr sdtPr = sdtCell.getSdtPr();
            printSdtPrContent(sdtPr);
            SdtContent sdtContent = sdtCell.getSdtContent();
            System.out.println("-----------table content=" + getContentAccessorContent(sdtContent));
        } else if (sdtChild instanceof SdtBlock) {
            SdtBlock sdtBlock = (SdtBlock) sdtChild;
            SdtPr sdtPr = sdtBlock.getSdtPr();
            printSdtPrContent(sdtPr);
            SdtContent sdtContent = sdtBlock.getSdtContent();
            System.out.println("-----------sdtblock content=" + getContentAccessorContent(sdtContent));
        }
    }
}
Also used : SdtPr(org.docx4j.wml.SdtPr) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Document(org.docx4j.wml.Document) CTSdtCell(org.docx4j.wml.CTSdtCell) TraversalUtil(org.docx4j.TraversalUtil) SdtRun(org.docx4j.wml.SdtRun) SdtContent(org.docx4j.wml.SdtContent) File(java.io.File) Body(org.docx4j.wml.Body) Child(org.jvnet.jaxb2_commons.ppp.Child) SdtBlock(org.docx4j.wml.SdtBlock)

Example 3 with TraversalUtil

use of org.docx4j.TraversalUtil in project docx4j-template by vindell.

the class Docx4j_删除所有批注_S3_Test method removeAllComment.

// 这里2个路径可以一致
public void removeAllComment(String filePath, String savePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filePath));
    // 清空comments.xml内容
    Parts parts = wordMLPackage.getParts();
    HashMap<PartName, Part> partMap = parts.getParts();
    CommentsPart commentPart = (CommentsPart) partMap.get(new PartName("/word/comments.xml"));
    Comments comments = commentPart.getContents();
    List<Comment> commentList = comments.getComment();
    for (int i = 0, len = commentList.size(); i < len; i++) {
        commentList.remove(0);
    }
    // 清空document.xml文件中批注
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getContents();
    Body body = wmlDocumentEl.getBody();
    CommentFinder cf = new CommentFinder();
    new TraversalUtil(body, cf);
    for (Child commentElement : cf.commentElements) {
        System.out.println(commentElement.getClass().getName());
        Object parent = commentElement.getParent();
        List<Object> theList = ((ContentAccessor) parent).getContent();
        boolean removeResult = remove(theList, commentElement);
        System.out.println(removeResult);
    }
    wordMLPackage.save(new FileOutputStream(savePath));
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) ContentAccessor(org.docx4j.wml.ContentAccessor) Body(org.docx4j.wml.Body) Child(org.jvnet.jaxb2_commons.ppp.Child) Comment(org.docx4j.wml.Comments.Comment) Comments(org.docx4j.wml.Comments) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) TraversalUtil(org.docx4j.TraversalUtil) Part(org.docx4j.openpackaging.parts.Part) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) FileOutputStream(java.io.FileOutputStream)

Example 4 with TraversalUtil

use of org.docx4j.TraversalUtil in project docx4j-template by vindell.

the class SdtFinder_2 method printSdtContent.

public void printSdtContent(String filePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    Document wmlDocumentEl = (Document) mdp.getContents();
    Body body = wmlDocumentEl.getBody();
    SdtFinder_2 sdtFinder = new SdtFinder_2();
    new TraversalUtil(body, sdtFinder);
    for (Child sdtChild : sdtFinder.sdtList) {
        if (sdtChild instanceof SdtRun) {
            SdtRun sdtRun = (SdtRun) sdtChild;
            SdtPr sdtPr = sdtRun.getSdtPr();
            printSdtPrContent(sdtPr);
            SdtContent sdtContent = sdtRun.getSdtContent();
            System.out.println("-----------p content=" + getSdtContentContent(sdtContent));
        } else if (sdtChild instanceof CTSdtCell) {
            CTSdtCell sdtCell = (CTSdtCell) sdtChild;
            SdtPr sdtPr = sdtCell.getSdtPr();
            printSdtPrContent(sdtPr);
            SdtContent sdtContent = sdtCell.getSdtContent();
            System.out.println("-----------table content=" + getSdtContentContent(sdtContent));
        } else if (sdtChild instanceof SdtBlock) {
            SdtBlock sdtBlock = (SdtBlock) sdtChild;
            SdtPr sdtPr = sdtBlock.getSdtPr();
            printSdtPrContent(sdtPr);
            SdtContent sdtContent = sdtBlock.getSdtContent();
            System.out.println("-----------sdtblock content=" + getSdtContentContent(sdtContent));
        }
    }
}
Also used : SdtPr(org.docx4j.wml.SdtPr) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Document(org.docx4j.wml.Document) CTSdtCell(org.docx4j.wml.CTSdtCell) TraversalUtil(org.docx4j.TraversalUtil) SdtRun(org.docx4j.wml.SdtRun) SdtContent(org.docx4j.wml.SdtContent) File(java.io.File) Body(org.docx4j.wml.Body) Child(org.jvnet.jaxb2_commons.ppp.Child) SdtBlock(org.docx4j.wml.SdtBlock)

Aggregations

TraversalUtil (org.docx4j.TraversalUtil)4 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)4 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)4 Body (org.docx4j.wml.Body)4 File (java.io.File)3 Document (org.docx4j.wml.Document)3 Child (org.jvnet.jaxb2_commons.ppp.Child)3 CTSdtCell (org.docx4j.wml.CTSdtCell)2 SdtBlock (org.docx4j.wml.SdtBlock)2 SdtContent (org.docx4j.wml.SdtContent)2 SdtPr (org.docx4j.wml.SdtPr)2 SdtRun (org.docx4j.wml.SdtRun)2 FileOutputStream (java.io.FileOutputStream)1 Part (org.docx4j.openpackaging.parts.Part)1 PartName (org.docx4j.openpackaging.parts.PartName)1 Parts (org.docx4j.openpackaging.parts.Parts)1 CommentsPart (org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart)1 MyTblFinder (org.docx4j.template.wml.MyTblFinder)1 Comments (org.docx4j.wml.Comments)1 Comment (org.docx4j.wml.Comments.Comment)1