A page break is a marker that controls where one page ends and where a new page begins. If you want to move the content after a certain place to the next page in your Word document, you can insert a page break. In this article, you will learn how to insert page break into Word documents in C# and VB.NET using Spire.Doc for .NET library.
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 Page Break after a Specific Paragraph
The following are the steps to 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.
- C#
- VB.NET
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("Sample.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); } } }
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.
- C#
- VB.NET
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("Sample.docx"); //Search a specific text TextSelection selection = document.FindString("celebration", 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); } } }
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.