文檔首頁>>E-iceblue中文文檔>>按分頁符拆分word文檔
按分頁符拆分word文檔
借助 Spire.Doc for .NET,我們不僅可以按節(jié)拆分 word 文檔,還可以按分頁符拆分。我們已經(jīng)介紹了如何通過分節(jié)符將一個 Word 文檔拆分為多個文檔。在本文中,我們將學習如何使用 Spire.Doc for .NET 按分頁符拆分 word 文檔。
請查看以下原始word文檔的屏幕截圖,該文檔在第一頁和第二頁的末尾有兩個分頁符。
現(xiàn)在參考以下詳細步驟將其按分頁符拆分為 3 個單獨的文檔。
第 1步:創(chuàng)建一個word文檔并加載原始word文檔。
Document original = new Document(); original.LoadFromFile("New Zealand.docx");
第 2 步:創(chuàng)建一個新的 Word 文檔并在其中添加一個部分。
Document newWord = new Document(); Section section = newWord.AddSection();
第 3 步:將原word文檔按照分頁符拆分成單獨的文檔。
int index = 0; //traverse through all sections of original document foreach (Section sec in original.Sections) { //traverse through all body child objects of each section foreach (DocumentObject obj in sec.Body.ChildObjects) { if (obj is Paragraph) { Paragraph para = obj as Paragraph; //add paragraph object in original section into section of new document section.Body.ChildObjects.Add(para.Clone()); foreach (DocumentObject parobj in para.ChildObjects) { if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak) { //get the index of page break in paragraph int i = para.ChildObjects.IndexOf(parobj); //remove the page break from its paragraph section.Body.LastParagraph.ChildObjects.RemoveAt(i); //save the new document to a .docx file. newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx); index++; //create a new document newWord = new Document(); //add a section for document section = newWord.AddSection(); //add paragraph object in original section into section of new document section.Body.ChildObjects.Add(para.Clone()); if (section.Paragraphs[0].ChildObjects.Count == 0) { //remove the first blank paragraph section.Body.ChildObjects.RemoveAt(0); } else { //remove the child objects before the page break while (i >= 0) { section.Paragraphs[0].ChildObjects.RemoveAt(i); i--; } } } } } if (obj is Table) { //add table object in original section into section of new document section.Body.ChildObjects.Add(obj.Clone()); } } } //save to a .docx file newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx);
輸出:
完整代碼:
using System; using Spire.Doc; using Spire.Doc.Documents; namespace Split_Word_Document_by_Page_Break { class Program { static void Main(string[] args) { Document original = new Document(); original.LoadFromFile("New Zealand.docx"); Document newWord = new Document(); Section section = newWord.AddSection(); int index = 0; foreach (Section sec in original.Sections) { foreach (DocumentObject obj in sec.Body.ChildObjects) { if (obj is Paragraph) { Paragraph para = obj as Paragraph; section.Body.ChildObjects.Add(para.Clone()); foreach (DocumentObject parobj in para.ChildObjects) { if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak) { int i = para.ChildObjects.IndexOf(parobj); section.Body.LastParagraph.ChildObjects.RemoveAt(i); newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx); index++; newWord = new Document(); section = newWord.AddSection(); section.Body.ChildObjects.Add(para.Clone()); if (section.Paragraphs[0].ChildObjects.Count == 0) { section.Body.ChildObjects.RemoveAt(0); } else { while (i >= 0) { section.Paragraphs[0].ChildObjects.RemoveAt(i); i--; } } } } } if (obj is Table) { section.Body.ChildObjects.Add(obj.Clone()); } } } newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx); } } }
歡迎下載|體驗更多E-iceblue產(chǎn)品
如需獲取更多產(chǎn)品相關(guān)信息請咨詢慧都在線客服