C#: Insert or Remove Section Breaks in Word

Section breaks allow you to divide a document into sections, each with its own formatting and layout settings, giving you greater control over the overall structure and appearance of your document. Whether you're compiling a complex report or a lengthy dissertation, mastering section breaks can greatly improve your document management skills. This article will demonstrate how to insert or remove section breaks in Word in C# using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Insert Section Breaks in Word in C#

Spire.Doc for .NET provides the Paragraph.InsertSectionBreak(SectionBreakType breakType) method to insert a specified type of section break to a paragraph. The following table provides an overview of the supported section break types, along with their corresponding Enums and descriptions:

Section Break Enum Description
New page SectionBreakType.NewPage Start the new section on a new page.
Continuous SectionBreakType.NoBreak Start the new section on the same page, allowing for continuous content flow.
Odd page SectionBreakType.OddPage Start the new section on the next odd-numbered page.
Even page SectionBreakType.EvenPage Start the new section on the next even-numbered page.
New column SectionBreakType.NewColumn Start the new section in the next column if columns are enabled.

The following are the detailed steps to insert a continuous section break:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified paragraph of the section using Section.Paragraphs[] property.
  • Add a section break to the end of the paragraph using Paragraph.InsertSectionBreak() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertSectionBreak
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("sample.docx");

            //Get the first section in the document
            Section sec = doc.Sections[0];

            //Get the second paragraph in the section 
            Paragraph para = sec.Paragraphs[1];

            //Insert a continuous section break
            para.InsertSectionBreak(SectionBreakType.NoBreak);

            //Save the result document
            doc.SaveToFile("InsertSectionBreak.docx", FileFormat.Docx);
        }
    }
}

C#: Insert or Remove Section Breaks in Word

Remove Section Breaks in Word in C#

To delete all sections breaks in a Word document, we need to access the first section in the document, then copy the contents of the other sections to the first section and delete them. The following are the detailed steps:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section using Document.Sections[] property.
  • Iterate through other sections in the document.
  • Get the second section, and then iterate through to get its child objects.
  • Clone the child objects of the second section and add them to the first section using Section.Body.ChildObjects.Add() method.
  • Delete the second section using Document.Sections.Remove() method.
  • Repeat the process to copy and delete the remaining sections.
  • Save the result document using Document.SaveToFile() method.
  • C#
using Spire.Doc;

namespace DeleteSectionBreak
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("Report.docx");

            //Get the first section in the document
            Section sec = doc.Sections[0];

            //Iterate through other sections in the document
            for (int i = 0; i < doc.Sections.Count - 1; i++)
            {
                //Get the second section in the document
                Section section = doc.Sections[1];

                //Iterate through all child objects of the second section
                for (int j = 0; j < section.Body.ChildObjects.Count; j++)
                {
                    //Get the child objects
                    DocumentObject obj = section.Body.ChildObjects[j];
                    //Clone the child objects to the first section
                    sec.Body.ChildObjects.Add(obj.Clone());
                }

                //Remove the second section
                doc.Sections.Remove(section);
            }

            //Save the result document
            doc.SaveToFile("RemoveSectionBreaks.docx", FileFormat.Docx);
        }
    }
}

C#: Insert or Remove Section Breaks in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Monday, 01 July 2024 01:32