How to Insert Page Break into Word Documents in C#/VB.NET

 While drafting in Word, your text flows to next page as you type. As a matter of fact, Word has inserted an automatic page break to separate 2 pages. This happens only when the first page is filled up. This article will show you how to insert a manual page break to pin blocks of contents on different pages. It stops texts on 2 pages from merging even if you add to or delete contents on one of them.

  • Insert page break after a specific paragraph
  • Insert Page Break after a Specific Text
programming environment
In this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:

Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire.Doc -Version 10.8.0

Specific Steps:
Insert page break after a specific paragraph
  • Create a Document instance
  • Load a Word document using Document.LoadFromFile() method.
  • Get the desired section using Document.Sections[sectionIndex] property.
  • Get the desired paragraph using Section.Paragraphs[paragraphIndex] property.
  • Add a page break to the paragraph using Paragraph.AppendBreak(BreakType.PageBreak) method.
  • Save the result document using Document.SaveToFile() method

Full Code

C#

using Spire.Doc;

using Spire.Doc.Documents;

namespace InsertPageBreakAfterParagraph

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a Document instance

            Document document = new Document();

            //Load a Word document

            document.LoadFromFile("UK.docx");

            //Get the first section

            Section section = document.Sections[0];

            //Get the 2nd paragraph in the section

            Paragraph paragraph = section.Paragraphs[1];

            //Append a page break to the paragraph

            paragraph.AppendBreak(BreakType.PageBreak);

            //Save the result document

            document.SaveToFile("InsertPageBreak.docx", FileFormat.Docx2013);

        }

    }

}

VB.NET

Imports Spire.Doc

Imports Spire.Doc.Documents

Namespace InsertPageBreakAfterParagraph

    Friend Class Program

        Private Shared Sub Main(ByVal args As String())

            'Create a Document instance

            Dim document As Document = New Document()

            'Load a Word document

            document.LoadFromFile("UK.docx")

            'Get the first section

            Dim section As Section = document.Sections(0)

            'Get the 2nd paragraph in the section

            Dim paragraph As Paragraph = section.Paragraphs(1)

            'Append a page break to the paragraph

            paragraph.AppendBreak(BreakType.PageBreak)

            'Save the result document

            document.SaveToFile("InsertPageBreak.docx", FileFormat.Docx2013)

        End Sub

    End Class

End Namespace

Effective Shot


Insert Page Break after a Specific Text
The following are the steps to insert a page break after a specific text
  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Find a specific text using Document.FindString() method.
  • Access the text range of the searched text using TextSelection.GetAsOneRange() method.
  • Get the paragraph where the text range is located using ParagraphBase.OwnerParagraph property.
  • Get the position index of the text range in the paragraph using Paragraph.ChildObjects.IndexOf() method.
  • Initialize an instance of Break class to create a page break.
  • Insert the page break after the searched text using Paragraph.ChildObjects.Insert() method.
  • Save the result document using Document.SaveToFile() method.


Full Code

C#

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

using System;

namespace InsertPageBreakAfterText

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a Document instance

            Document document = new Document();

            //Load a Word document

            document.LoadFromFile("UK.docx");

            //Search a specific text

            TextSelection selection = document.FindString("varying powers", true, true);

            //Get the text range of the seached text

            TextRange range = selection.GetAsOneRange();

            //Get the paragraph where the text range is located

            Paragraph paragraph = range.OwnerParagraph;

            //Get the position index of the text range in the paragraph

            int index = paragraph.ChildObjects.IndexOf(range);

            //Create a page break

            Break pageBreak = new Break(document, BreakType.PageBreak);

            //Insert the page break after the searched text

            paragraph.ChildObjects.Insert(index + 1, pageBreak);

            //Save the result document

            document.SaveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx2013);

        }

    }

}

VB.NET

Imports Spire.Doc

Imports Spire.Doc.Documents

Imports Spire.Doc.Fields

Imports System

Namespace InsertPageBreakAfterText

    Class Program      

        Private Shared Sub Main(ByVal args() As String)

            'Create a Document instance

            Dim document As Document = New Document

            'Load a Word document

            document.LoadFromFile("UK.docx")

            'Search a specific text

            Dim selection As TextSelection = document.FindString("varying powers", true, true)

            'Get the text range of the seached text

            Dim range As TextRange = selection.GetAsOneRange

            'Get the paragraph where the text range is located

            Dim paragraph As Paragraph = range.OwnerParagraph

            'Get the position index of the text range in the paragraph

            Dim index As Integer = paragraph.ChildObjects.IndexOf(range)

            'Create a page break

            Dim pageBreak As Break = New Break(document, BreakType.PageBreak)

            'Insert the page break after the searched text

            paragraph.ChildObjects.Insert((index + 1), pageBreak)

            'Save the result document

            document.SaveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx2013)

        End Sub

    End Class

End Namespace

Effective Shot

Conclusion:

     In this post, you have learned how to Insert Page Break into Word Documents in C#/VB.NET. Not only that, we also have other functions, such as, Insert page break at a specified location, Set Page Size of Word Document and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for .NET.


Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application

Java: How to encrypt or decrypt PDF documents?