原創|行業資訊|編輯:郝浩|2018-08-21 15:48:54.000|閱讀 2331 次
概述:本文將分別介紹如何使用Spire.Doc實現兩種不同的合并效果。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在使用MS Word時,用戶可以點擊“插入”-“對象”-“文件中的文字”快速將選定文件中的文本插入當前文檔。
Spire.Doc提供了類似的方法InsertTextFromFile來將不同的文檔合并到同一個文檔,但與Word的差別在于,目前InsertTextFromFile方法不支持選定插入位置。使用該方法合并文檔時,新加入的文檔默認從新的一頁開始顯示。如果需要新添加的文檔承接前一個文檔的段尾,則需要使用不同的合并方法。本文將分別介紹如何使用Spire.Doc實現兩種不同的合并效果。
/獲取文檔路徑 string filePath_1 = @"C:\Users\Administrator\Desktop\Word_1.docx"; string filePath_2 = @"C:\Users\Administrator\Desktop\Word_2.docx"; //加載文檔1到Document對象 Document doc= new Document(filePath_1); //使用InsertTextFromFile方法將文檔2合并到新文檔 doc.InsertTextFromFile(filePath_2, FileFormat.Docx2013); //保存文檔 doc.SaveToFile("合并文檔.docx", FileFormat.Docx2013);
//初始化兩個Document實例并加載兩個測試文檔 Document doc1 = new Document(@"C:\Users\Administrator\Desktop\測試文檔_1.docx"); Document doc2 = new Document(@"C:\Users\Administrator\Desktop\測試文檔_2.docx"); //獲取doc1的最后一個section Section lastSection = doc1.LastSection; //遍歷doc2的section和段落,將每一個段落添加到doc1的最后一個section foreach (Section section in doc2.Sections) { foreach (Paragraph paragraph in section.Paragraphs) { lastSection.Paragraphs.Add(paragraph.Clone() as Paragraph); } } //保存為新的文檔 doc1.SaveToFile("合并文檔_2.docx", FileFormat.Docx2013);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn