欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Aspose.Words 将word2中的内容插入到word1中的指定位置

程序员文章站 2023-01-29 16:10:42
将word2中的内容插入到word1中的指定位置(经测试可用) 在官网找到的例子,记录一下: ......

将word2中的内容插入到word1中的指定位置(经测试可用)

在官网找到的例子,记录一下:

        public static void insertdocumentatbookmark(string datadir)
        {
            document maindoc = new document(datadir + "insertdocument1.doc");
            document subdoc = new document(datadir + "insertdocument2.doc");

            //定位到书签:insertionplace
            bookmark bookmark = maindoc.range.bookmarks["insertionplace"];
            //将subdoc中的内容插入到maindoc中的书签“insertionplace”位置
            insertdocument(bookmark.bookmarkstart.parentnode, subdoc);
            datadir = datadir + "insertdocumentatbookmark_out.doc";
            maindoc.save(datadir);
        }

        /// <summary>
        /// 在指定节点之后插入外部文档的内容。
        /// 插入文档的分节符和节格式将被忽略。
        /// </summary>
        /// <param name="insertafternode">node in the destination document after which the content
        /// should be inserted. this node should be a block level node (paragraph or table).</param>
        /// <param name="srcdoc">the document to insert.</param>
        static void insertdocument(node insertafternode, document srcdoc)
        {
            // make sure that the node is either a paragraph or table.
            if ((!insertafternode.nodetype.equals(nodetype.paragraph)) &
              (!insertafternode.nodetype.equals(nodetype.table)))
                throw new argumentexception("the destination node should be either a paragraph or table.");

            // we will be inserting into the parent of the destination paragraph.
            compositenode dststory = insertafternode.parentnode;

            // this object will be translating styles and lists during the import.
            nodeimporter importer = new nodeimporter(srcdoc, insertafternode.document, importformatmode.keepsourceformatting);

            // loop through all sections in the source document.
            foreach (section srcsection in srcdoc.sections)
            {
                // loop through all block level nodes (paragraphs and tables) in the body of the section.
                foreach (node srcnode in srcsection.body)
                {
                    // let's skip the node if it is a last empty paragraph in a section.
                    if (srcnode.nodetype.equals(nodetype.paragraph))
                    {
                        paragraph para = (paragraph)srcnode;
                        if (para.isendofsection && !para.haschildnodes)
                            continue;
                    }

                    // this creates a clone of the node, suitable for insertion into the destination document.
                    node newnode = importer.importnode(srcnode, true);

                    // insert new node after the reference node.
                    dststory.insertafter(newnode, insertafternode);
                    insertafternode = newnode;
                }
            }
        }