翻譯|使用教程|編輯:胡濤|2022-04-13 13:46:08.397|閱讀 310 次
概述:在本文中,我們將學習如何使用 Spire.Doc for .NET 按分頁符拆分 word 文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
借助 Spire.Doc for .NET,我們不僅可以按節拆分 word 文檔,還可以按分頁符拆分。我們已經介紹了如何通過分節符將一個 Word 文檔拆分為多個文檔。在本文中,我們將學習如何使用 Spire.Doc for .NET 按分頁符拆分 word 文檔。
請查看以下原始word文檔的屏幕截圖,該文檔在第一頁和第二頁的末尾有兩個分頁符。
現在參考以下詳細步驟將其按分頁符拆分為 3 個單獨的文檔。
第 1步:創建一個word文檔并加載原始word文檔。
Document original = new Document();
original.LoadFromFile("New Zealand.docx");
第 2 步:創建一個新的 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產品
如需獲取更多產品相關信息請咨詢
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn