翻譯|使用教程|編輯:李顯亮|2020-08-31 09:54:46.927|閱讀 819 次
概述:MS Word文檔被廣泛用于保存和共享信息。在某些情況下,可能需要從可能位于不同部分或頁面的Word文檔中拆分數據。在本文中,將展示如何使用C#以編程方式拆分MS Word文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
MS Word文檔被廣泛用于保存和共享信息。在某些情況下,可能需要從可能位于不同部分或頁面的Word文檔中拆分數據。另外,還可能需要將單個文檔的頁面拆分為多個文檔。
根據這種情況,在本文中,將展示如何使用C#以編程方式拆分MS Word文檔。讓我們繼續學習以下用例:
>>Aspose.Words for .NET已經更新至v20.8,此常規的每月版本中有81項改進和修復,主要特點包括實現了Markdown的“內嵌圖片”功能、為字體名稱處理添加了新的字體替換規則等三大新功能。
部分是指文檔中可以應用不同格式的部分。一個部分可以由一個頁面,一個頁面范圍或整個文檔組成。分節符用于將文檔拆分為多個部分。以下是使用Aspose.Words for .NET根據文檔的部分拆分Word文檔的步驟。
下面的代碼示例演示如何使用C#按部分拆分MS Word文檔。
// Open a Word document Document doc = new Document("document.docx"); for (int i = 0; i < doc.Sections.Count; i++) { // Split a document into smaller parts, in this instance split by section Section section = doc.Sections[i].Clone(); // Create a new document Document newDoc = new Document(); newDoc.Sections.Clear(); Section newSection = (Section)newDoc.ImportNode(section, true); newDoc.Sections.Add(newSection); // Save each section as a separate document newDoc.Save($"splitted_{i}.docx"); }
在某些情況下,Word文檔在每頁上都包含類似類型的信息,例如發票或收據。在這種情況下,可以拆分文檔的頁面,以便將每個發票另存為單獨的文檔。若要逐頁拆分文檔,可以使用 基于Aspose.Words for .NET 的幫助程序類DocumentPageSplitter。可以按照以下步驟簡單地在項目中復制該類并逐頁拆分Word文檔。
下面的代碼示例演示如何使用C#按頁面拆分Word文檔。
// Open a Word document Document doc = new Document("Document.docx"); // Create and initialize the document page splitter DocumentPageSplitter splitter = new DocumentPageSplitter(doc); // Save each page as a separate document for (int page = 1; page <= doc.PageCount; page++) { Document pageDoc = splitter.GetDocumentOfPage(page); pageDoc.Save($"spliteed_{page}.docx"); }
還可以使用DocumentPageSplitter 類指定頁面范圍以將其與原始文檔分開。例如,如果需要將頁面從2拆分為4,只需在DocumentPageSplitter.GetDocumentOfPageRange(int StartIndex,int EndIndex)方法中指定起始頁和結束頁的索引即可。
下面的代碼示例演示如何使用C#從Word文檔中拆分頁面范圍。
// Open a Word document Document doc = new Document("document.docx"); // Create and initialize document page splitter DocumentPageSplitter splitter = new DocumentPageSplitter(doc); // Get the page range Document pageDoc = splitter.GetDocumentOfPageRange(3, 6); pageDoc.Save("splitted.docx");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn