借助 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 documentforeach (Section sec in original.Sections){//traverse through all body child objects of each sectionforeach (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 documentsection.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 paragraphint i = para.ChildObjects.IndexOf(parobj);//remove the page break from its paragraphsection.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 documentnewWord = new Document();//add a section for documentsection = newWord.AddSection();//add paragraph object in original section into section of new documentsection.Body.ChildObjects.Add(para.Clone());if (section.Paragraphs[0].ChildObjects.Count == 0){//remove the first blank paragraphsection.Body.ChildObjects.RemoveAt(0);}else{//remove the child objects before the page breakwhile (i >= 0){section.Paragraphs[0].ChildObjects.RemoveAt(i);i--;}}}}}if (obj is Table){//add table object in original section into section of new documentsection.Body.ChildObjects.Add(obj.Clone());}}}//save to a .docx filenewWord.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产品
如需获取更多产品相关信息请咨询在线客服
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!