Word控件Spire.Doc 【文档操作】教程(十一):如何在C#中按分页符拆分word文档
2023-09-11 14:14:49 时间
借助 Spire.Doc for .NET,我们不仅可以按节拆分 word 文档,还可以按分页符拆分。我们已经介绍了如何通过分节符将一个 Word 文档拆分为多个文档。在本文中,我们将学习如何使用 Spire.Doc for .NET 按分页符拆分 word 文档。
欢迎下载|体验更多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);
}
}
}
相关文章
- C# VS2012操作word文档 (一).创建文档
- 四种Sandcastle方法生成c#.net帮助类帮助文档
- C#类和结构体的异同点简单总结
- C# 文件流相关操作
- C# 视频监控系列(9):服务器端——数据捕获(抓图 + 录像)
- C# 将字符串(符合xml格式)转换为XmlDocument
- C# 将XmlDocument转化为string函数
- C# 移除数组中重复数据
- Atitit. Async await 优缺点 异步编程的原理and实现 java c# php
- 改善C#程序的157个建议——建议84学习笔记:使用PLINQ
- C# CHM帮助文档
- c# 叫号小程序
- (21)C# 访问Redis的测试
- CSharp(C#)语言_第七章(语句)
- C#创建Windows窗体应用程序实例10【目录管理】