Articles in this section
Category / Section

How to change section break in ASP.NET Core Word document?

4 mins read

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can change section break in a Word document as page break using C#.

Steps to change section break in Word document as page break:

  1. Create a new C# .NET Core console application project.

Create .NET Core console application in Visual Studio

  1. Install the Syncfusion.DocIO.NET.Core NuGet packages as a reference to your ASP.NET Core application from NuGet.org.

Add DocIO NuGet packages of ASP.NET Core

  1. Include the following namespaces in the Program.cs file:

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. Use the following code snippet to change section break in a Word document as page break:

C#

using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    //Open an existing Word document.
    using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
    {
        //Replace the section break with page break in the Word document.
        ReplaceSectionBreakWithPageBreak(document);
        //Create file stream.
        using (FileStream outputFileStream = new FileStream(Path.GetFullPath("../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
        {
            //Save the Word document to file stream.
            document.Save(outputFileStream, FormatType.Docx); 
        }
    }
}

5. Helper method to change section break in a Word document as page break.

C#

/// <summary>
/// Replace the section break with page break in the Word document.
/// </summary>
private static void ReplaceSectionBreakWithPageBreak(WordDocument document)
{
    //Add page break and removes section break by moving the section items to the first section.
    while (document.Sections.Count > 1)
    {
        WSection sec = document.Sections[1];
        //Add page break in last paragraph of the section.
         (document.Sections[0].Body.AddParagraph()).AppendBreak(BreakType.PageBreak);
        //Iterate the section items in the Word document.
        foreach (Entity entity in sec.Body.ChildEntities)
        {
            //Merge the next section to first section before removing it.
            int lastItemIndex = document.Sections[0].Body.ChildEntities.Count;
            document.Sections[0].Body.ChildEntities.Insert(lastItemIndex, entity.Clone());
        }
        //Remove section at the specified index from the Word document.
        document.Sections.RemoveAt(1);
    }
}

A complete working sample to change section break in a Word document as page break using C# can be downloaded from GitHub.

By executing the program, you will get the output document as follows.

Output Word document generated in ASP.NET Core

Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


Conclusion
I hope you enjoyed learning how to change section break in ASP.NET Core Word document? 


You can refer to our ASP.NET Core feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET CoreASP.NET Core Example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!




 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied