C# 根据分节符和分页符拆分 Word 文档

2025-11-03 14:03:56

1、下载并安装Spire.Doc for .NET。

2、引用Spire.Doc.dll到项目中。

3、将代码放入Visual Studio中。

根据分节符拆分Word文档:

【C#】

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Spire.Doc;

using Spire.Doc.Documents;

namespace Split_by_Sectionbreak

{

    class Program

    {

        static void Main(string[] args)

        {

            //实例化Document对象

            Document document = new Document();

            //载入待拆分的Word文档

            document.LoadFromFile("测试文档.docx");

            //插入分节符到第一节的第一个段落结束位置

            document.Sections[0].Paragraphs[0].InsertSectionBreak(SectionBreakType.NoBreak);

            //插入分节符到第二节的第二个段落结束位置

            document.Sections[1].Paragraphs[1].InsertSectionBreak(SectionBreakType.NoBreak);

            Document newWord;

            for (int i = 0; i < document.Sections.Count; i++)

            {

                //每有一个section就创建一个新的文档

                newWord = new Document();

                //复制section内容到新文档

                newWord.Sections.Add(document.Sections[i].Clone());

                //保存文档

                newWord.SaveToFile(String.Format("分节符拆分的结果文档_{0}.docx", i));

            }

        }

    }

}

代码调试运行后,结果如下图:

C# 根据分节符和分页符拆分 Word 文档

4、根据分页符拆分Word文档:

【C#】

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Spire.Doc;

using Spire.Doc.Documents;

namespace Split_by_pagebreak

{

    class Program

    {

        static void Main(string[] args)

        {

            //实例化Document对象

            Document original = new Document();

            //载入待拆分的Word文档

            original.LoadFromFile("测试文档.docx");

            //插入分页符到第一节的第四个段落的结束位置

            original.Sections[0].Paragraphs[3].AppendBreak(BreakType.PageBreak);

            //插入分页符到第一节的第六个段落的结束位置

            original.Sections[0].Paragraphs[5].AppendBreak(BreakType.PageBreak);

            //实例化一个新的文档并添加新章节

            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("分页符拆分的结果文档_{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("分页符拆分的结果文档_{0}.docx", index), FileFormat.Docx);

        }

    }

}

代码调试运行后,Word文档被拆分为三个新的文档,结果如下:

C# 根据分节符和分页符拆分 Word 文档

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢