zoukankan      html  css  js  c++  java
  • Aspose.Words 将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;
                    }
                }
            }
  • 相关阅读:
    Mybatis主配置文件常见使用讲解
    Mybatis导入原生配置文件
    分页查询时,使用cookie保存上次的查询条件。jQuery实现方法以及中间遇到的坑
    jQuery实现checkbox即点即改,批量计数,以及中间遇到的坑
    ()IT 职场经验)一位10年Java工作经验的架构师的经验分享,感觉很受用。
    SpringMVC的Controller
    Spring MVC执行原理和基于Java的配置过程
    集成学习综述笔记
    generalization error
    Failed to install 'unknown package' from GitHub: schannel: failed to receive handshake, SSL/TLS connection failed
  • 原文地址:https://www.cnblogs.com/wz122889488/p/9966637.html
Copyright © 2011-2022 走看看