Location>code7788 >text

Inserting or removing section breaks in Word via C#

Popularity:594 ℃/2024-09-09 16:46:34

In Word, section breaks are a powerful tool for dividing a document into different sections, each of which can have independent page settings such as margins, paper orientation, headers and footers. Correct use of section breaks can greatly enhance the organization and professionalism of the document, especially in long documents, the need to apply different styles in different parts. This article will show how to use a free .NET library through C# to achieve the insertion or deletion of Word section breaks.

 

Free .NET Word Library

Free for .NET NET class library for the operation of Word documents is a free , suitable for commercial or personal use (but with a certain page limit).

Installation.
We can search directly in Visual Studio via NuGet "", then click "Install" to reference it to the program. Alternatively, you can add it to the program byhyperlinkDownload the product package, unzip it and then manually add the dll file references to the program.

 

Word Section Break Types

In for .(SectionBreakType breakType) method is used to insert section breaks in a paragraph. One of theSectionBreakTypeSeveral types of section breaks included in the enumeration are listed below:

  • Indicates a continuous section separator, i.e. the content of the new section will start immediately after the content of the current page, without the need for a new page. It is suitable for separating different sections within the same page.
  • Indicates the next page break, i.e. the new section will start on the next page.
  • Indicates an odd page break, i.e. the new section will start on the next odd page.
  • Indicates an even page break, i.e. the new section will start on the next even page.
  • For documents with multiple columns, the new section will start in the next column.

API reference link:/apireference/net//html/T_Spire_Doc_Documents_SectionBreakType.htm

 

C# Inserting Section Breaks in Word

Main Steps:

  1. pass (a bill or inspection etc)LoadFromFile() method to load a Word document.
  2. Uses the specified paragraph in the specified section.
  3. utilizationInsertSectionBreak() method adds a paragraph break to the end of a paragraph.
  4. utilizationSaveToFile() method to save the result document.

Sample code:

using ;
using ;

namespace InsertSectionBreak
{
    class Program
    {
        static void Main(string[] args)
        {
            //Loading Word documents
            Document doc = new Document();
            ("Antarctica.docx");

            //Access to Section I
            Section sec = [0];

            //Get the first paragraph in the section
            Paragraph para = [0];

            //Inserting consecutive section breaks
            ();

            //Save file
            ("Consecutive section breaks.docx", );
        }
    }
}

 

Inserts consecutive section breaks:

 

 

C# Delete Section Break in Word

This example removes all section breaks by copying the contents of each section of the original Word document into a new Word document.
The main steps are as follows:

  1. pass (a bill or inspection etc)LoadFromFile() method to load the original Word document.
  2. Create a new Word document and add a section to it.
  3. Iterates through each section of the original document and gets the contents of its sub-objects.
  4. Copy the fetched child object and use the() method adds the child object contents to the new document.
  5. utilizationSaveToFile() method to save a new Word document.

Sample code:

using ;

namespace DeleteSectionBreak
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the original Word document
            Document doc = new Document();
            ("Experiment.docx");

            //Creating a New Word Document
            Document NewDoc = new Document();
            //Add a section
            Section sec = ();

            //Iterate over all sections in the original document
            for (int i = 0; i < ; i++)
            {
                //Get current section
                Section section = [i];

                //Iterate over all child objects in the section
                for (int j = 0; j < ; j++)
                {
                    //Get child objects and copy them to a new document
                    DocumentObject obj = [j];
                    (());
                }
            }

            //Save New Document
            ("Remove section breaks.docx", );
        }
    }
}

 

Rendering:

 

 

NET Word library also supports the operation of page breaks, margins, headers and footers and other page settings, more examples can be clicked to see:
/spiredoc/