《C#利用 AsposeWords在文档指定位置插入doc文档.docx》由会员分享,可在线阅读,更多相关《C#利用 AsposeWords在文档指定位置插入doc文档.docx(7页珍藏版)》请在三一办公上搜索。
1、C#利用 AsposeWords在文档指定位置插入doc文档using System;using System.Collections.Generic;using System.Linq;using System.Text;using Aspose.Words;namespace WindowsFormsApplication1class Class1public static Document insertDocumentAfterBookMark(Document mainDoc,Document tobeInserted, string bookmark) / check maindoc
2、if (mainDoc = null)return null;/ check to be inserted docif (tobeInserted = null) return mainDoc; DocumentBuilder mainDocBuilder = new DocumentBuilder(mainDoc);/ check bookmark and then processif (bookmark != null & bookmark.Trim.Length > 0)Bookmark bm = mainDoc.Range.Bookmarksbookmark;if (bm != n
3、ull) mainDocBuilder.MoveToBookmark(bookmark);mainDocBuilder.Writeln;Node insertAfterNode = mainDocBuilder.CurrentParagraph.PreviousSibling;insertDocumentAfterNode(insertAfterNode, mainDoc, tobeInserted); else / if bookmark is not provided, add the document at the endappendDoc(mainDoc, tobeInserted);
4、return mainDoc;public static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc)/ Make sure that the node is either a pargraph or table.if (insertAfterNode.NodeType != NodeType.Paragraph)& (insertAfterNode.NodeType != NodeType.Table)throw new Exception(The destinati
5、on node should be either a paragraph or table.);/We will be inserting into the parent of the destination paragraph.CompositeNode dstStory = insertAfterNode.ParentNode;/Remove empty paragraphs from the end of documentwhile (null != srcDoc.LastSection.Body.LastParagraph & !srcDoc.LastSection.Body.Last
6、Paragraph.HasChildNodes)srcDoc.LastSection.Body.LastParagraph.Remove;NodeImporter importer = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KeepSourceFormatting);/Loop through all sections in the source document.int sectCount = srcDoc.Sections.Count;for(int sectIndex=0; sectIndex<sectCount; se
7、ctIndex+)Section srcSection = srcDoc.SectionssectIndex;/Loop through all block level nodes (paragraphs and tables) in the body of the section.int nodeCount = srcSection.Body.ChildNodes.Count;for(int nodeIndex=0; nodeIndex<nodeCount; nodeIndex+)Node srcNode = srcSection.Body.ChildNodesnodeIndex;Nod
8、e newNode = importer.ImportNode(srcNode, true);dstStory.InsertAfter(newNode, insertAfterNode);insertAfterNode = newNode;/* Appends a document to another.* param dstDoc - Destination document* param srcDoc - Source document* param includeSection - if true the sections from srcDoc will be copied as it
9、 is, else only the internal nodes will be copied* throws Throwable*/public static void appendDoc(Document dstDoc, Document srcDoc, bool includeSection)/ Loop through all sections in the source document./ Section nodes are immediate children of the Document node so we can/ just enumerate the Document
10、.if(includeSection) foreach (Section srcSection in srcDoc.Sections)Node dstNode = dstDoc.ImportNode(srcSection, true,ImportFormatMode.UseDestinationStyles);dstDoc.AppendChild(dstNode); else/find the last paragraph of the last sectionNode node = dstDoc.LastSection.Body.LastParagraph;if(node = null) n
11、ode = new Paragraph(srcDoc);dstDoc.LastSection.Body.AppendChild(node);if (node.NodeType!= NodeType.Paragraph)& (node.NodeType != NodeType.Table) throw new Exception(Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false);insertDocumentAfterNode(node, dstDoc, srcDoc);public static void appendDoc(Document dstDoc, Document srcDoc) appendDoc(dstDoc, srcDoc, true);