zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Word控件Spire.Doc 【文档操作】教程(十一):如何在C#中按分页符拆分word文档

c#文档教程 如何 操作 控件 分页 word
2023-09-11 14:14:49 时间

借助 Spire.Doc for .NET,我们不仅可以按节拆分 word 文档,还可以按分页符拆分。我们已经介绍了如何通过分节符将一个 Word 文档拆分为多个文档。在本文中,我们将学习如何使用 Spire.Doc for .NET 按分页符拆分 word 文档。

Spire.Doc for.NET 最新下载 

欢迎下载|体验更多E-iceblue产品 技术交流Q群(767755948)   

请查看以下原始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);
}
}
}