需求:需要将其他模块生成的各个word汇总成一个整的Word。

参考官方介绍地址:https://apireference.aspose.com/java/words/com.aspose.words/DocumentBuilder

public Document appendDocument(Document mainDoc, Document addDoc, boolean isPortrait) {
     //设置书签,指定文档拼接的位置 String bookmark
= "插入的位置"; DocumentBuilder builder = null; try { builder = new DocumentBuilder(mainDoc); BookmarkCollection bms = mainDoc.getRange().getBookmarks(); Bookmark bm = bms.get(bookmark); if (bm != null) { builder.moveToBookmark(bookmark, true, false); builder.writeln(); Node insertAfterNode = builder.getCurrentParagraph().getPreviousSibling(); insertDocumentAfterNode(insertAfterNode, mainDoc, addDoc); }
       //设置纸张大小 builder.getPageSetup().setPaperSize(PaperSize.A4);
if (isPortrait) { //纵向纸张, builder.getPageSetup().setOrientation(Orientation.PORTRAIT); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); } else { //横向 builder.getPageSetup().setOrientation(Orientation.LANDSCAPE); builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); } //builder.insertBreak(BreakType.PAGE_BREAK); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return mainDoc; }

 之前少了拼接的关键代码,现在补上(2019-10-8):

/**
      * @Description 向书签后插入文档
      * @param mainDoc 主文档
      * @param tobeInserted 拼接的文档
      * @param bookmark 书签
      * @Return com.aspose.words.Document
      * @Author Mr.Walloce
      * @Date 2019/7/27 18:33
      */
    private static Document insertDocumentAfterBookMark(Document mainDoc, Document tobeInserted, String bookmark)
            throws Exception {
        if (mainDoc == null) {
            return null;
        } else if (tobeInserted == null) {
            return mainDoc;
        } else {
            //构建新文档
            DocumentBuilder mainDocBuilder = new DocumentBuilder(mainDoc);
            if (bookmark != null && bookmark.length() > 0) {
                //获取到书签
                BookmarkCollection bms = mainDoc.getRange().getBookmarks();
                Bookmark bm = bms.get(bookmark);
                if (bm != null) {
                    mainDocBuilder.moveToBookmark(bookmark, true, false);
                    mainDocBuilder.writeln();
                    //获取到插入的位置
                    Node insertAfterNode = mainDocBuilder.getCurrentParagraph().getPreviousSibling();
                    insertDocumentAfterNode(insertAfterNode, mainDoc, tobeInserted);
                }
            } else {
                appendDoc(mainDoc, tobeInserted, true);
            }

            return mainDoc;
        }
    }

    /**
      * @Description TODO
      * @param insertAfterNode 插入的位置
      * @param mainDoc
      * @param srcDoc
      * @Return void
      * @Author Mr.Walloce
      * @Date 2019/7/27 14:51
      */
    private static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc)
            throws Exception {
        if (insertAfterNode.getNodeType() != 8 & insertAfterNode.getNodeType() != 5) {
            throw new Exception("The destination node should be either a paragraph or table.");
        } else {
            CompositeNode dstStory = insertAfterNode.getParentNode();

            while (null != srcDoc.getLastSection().getBody().getLastParagraph()
                    && !srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes()) {
                srcDoc.getLastSection().getBody().getLastParagraph().remove();
            }

            NodeImporter importer = new NodeImporter(srcDoc, mainDoc, 1);
            int sectCount = srcDoc.getSections().getCount();

            for (int sectIndex = 0; sectIndex < sectCount; ++sectIndex) {
                Section srcSection = srcDoc.getSections().get(sectIndex);
                int nodeCount = srcSection.getBody().getChildNodes().getCount();

                for (int nodeIndex = 0; nodeIndex < nodeCount; ++nodeIndex) {
                    Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);
                    Node newNode = importer.importNode(srcNode, true);
                    dstStory.insertAfter(newNode, insertAfterNode);
                    insertAfterNode = newNode;
                }
            }

        }
    }

    /**
      * @Description 文档拼接
      * @param dstDoc
      * @param srcDoc
      * @param includeSection
      * @Return void
      * @Author Mr.Walloce
      * @Date 2019/7/27 14:53
      */
    private static void appendDoc(Document dstDoc, Document srcDoc, boolean includeSection) throws Exception {
        if (includeSection) {
            Iterator<Section> var3 = srcDoc.getSections().iterator();
            while (var3.hasNext()) {
                Section srcSection = (Section) var3.next();
                Node dstNode = dstDoc.importNode(srcSection, true, 0);
                dstDoc.appendChild(dstNode);
            }
        } else {
            Node node = dstDoc.getLastSection().getBody().getLastParagraph();
            if (node == null) {
                node = new Paragraph(srcDoc);
                dstDoc.getLastSection().getBody().appendChild(node);
            }

            if (node.getNodeType() != 8 & node.getNodeType() != 5) {
                throw new Exception("Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false)");
            }

            insertDocumentAfterNode(node, dstDoc, srcDoc);
        }

 

版权声明:本文为yin1361866686原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yin1361866686/p/9561006.html