C#利用 AsposeWords在文档指定位置插入doc文档.docx

上传人:小飞机 文档编号:3060453 上传时间:2023-03-10 格式:DOCX 页数:7 大小:37.48KB
返回 下载 相关 举报
C#利用 AsposeWords在文档指定位置插入doc文档.docx_第1页
第1页 / 共7页
C#利用 AsposeWords在文档指定位置插入doc文档.docx_第2页
第2页 / 共7页
C#利用 AsposeWords在文档指定位置插入doc文档.docx_第3页
第3页 / 共7页
C#利用 AsposeWords在文档指定位置插入doc文档.docx_第4页
第4页 / 共7页
C#利用 AsposeWords在文档指定位置插入doc文档.docx_第5页
第5页 / 共7页
亲,该文档总共7页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《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);

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号