將 Word 文檔合并在一起
Aspose.Words是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
頁面設置是一組格式屬性,存儲在 Word 文檔的每個部分中。Microsoft Word Automation 的 ActiveDocument.Range.PageSetup 是為文檔的所有部分設置相同頁面設置的“快捷方式”。Aspose.Words僅通過Section.PageSetup屬性提供對各個部分的頁面設置的訪問,因此對頁面設置的任何文檔范圍的更改都必須應用于所有部分。
VSTO
string mypath = "Document.docx"; Word.Application wordApp = Application; wordApp.Documents.Open(mypath); int recordCount = 2; int i = 0; for (i = 0; i <= recordCount; i++) wordApp.Selection.WholeStory(); wordApp.Selection.EndOf(); wordApp.Selection.InsertFile("DetailsList.docx"); if (i < recordCount) { wordApp.Selection.Range.InsertBreak(2); }
點擊復制
上面的代碼循環運行,并在當前文檔的末尾插入一個文檔。每個加入的文檔中的內容均由分節符分隔,并且此新部分的頁眉和頁腳未鏈接,因此它們不會從上一節的頁眉和頁腳繼續。 當遷移到Aspose.Words時,您會發現上面的任務非常容易實現。Aspose.Words 為此提供了一個特殊的 Document.AppendDocument 方法,用于將兩個文檔連接在一起。 此方法將源文檔中的部分復制到目標文檔。這消除了插入自動化所需的任何分節符的需要。
Aspose.Words
// The document that the other documents will be appended to. Document dstDoc = new Document( ); // We should call this method to clear this document of any existing content. dstDoc.RemoveAllChildren(); int recordCount = 1; for (int i = 1; i <= recordCount; i++) { // Open the document to join. Document srcDoc = new Document( "src.doc"); // Append the source document at the end of the destination document. dstDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles); Document doc2 = new Document("Section.ModifyPageSetupInAllSections.doc"); dstDoc.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles); // In automation you were required to insert a new section break at this point, however in // Aspose.Words we don't need to do anything here as the appended document is imported as separate sectons already. // If this is the second document or above being appended then unlink all headers footers in // this section from the headers and footers of the previous section. if (i > 1) dstDoc.Sections[i].HeadersFooters.LinkToPrevious(false); } dstDoc.Save("updated.doc");
點擊復制
請注意,您可以通過使用適當的Section對象的PageSetup.SectionStart屬性 來控制文檔如何連接在一起(即連續顯示或在新頁面上顯示)。
下載示例代碼