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

Word Break is used to start a new line, section or page.  It defines ending location of old contents and beginning of new contents. There are four kinds of breaks, page break, column break, text wrapping and section break.

Generally speaking, after words are full of one line or page, text wrapping and page break will be inserted automatically. However, sometimes, people would like to insert break manually. For example, I just want to add a heading in page 1 to be as a cover, while write body from page 2. At that time, I need to insert a page break after adding heading in page 1 to start with body.

In this post, I want to introduce a method to insert page break in Word with C#/VB.NET.

In order to have this function realized more quickly and easily, I have used a .NET Word Component, Spire.Doc for .NET in my example. So, its dll file is added as reference in my project at the beginning.

Detailed Steps Shown as Following:

  1. Create a new document and add section.
  2. Add paragraph in this section. Then, define a paragraph style, including font type, color and line spacing.
  3. Append text which introduce what I will do in this paragraph and apply style for this paragraph.
  4. Add a new paragraph. Then, append text and set format for text, including font type and color.
  5. Insert section break. Add a new section and assign section break type for section.BreakCod. In this example, the type I use is NewPage.
  6. Add paragraph and append text in new page. And apply style which has been set in step 2 for this paragraph.
  7. Save and launch file.

Main Coding:

C#

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace InsertBreak
{
    class Break
    {
        static void Main(string[] args)
        {

            //Create Document and Add Section
            Document document = new Document();
            Section section = document.AddSection();

            //Add Paragraph and Set Paragraph Style
            Paragraph paragraph = section.AddParagraph();
            ParagraphStyle style1 = new ParagraphStyle(document);
            style1.Name = “Style1”;
            style1.CharacterFormat.Font = new Font(“Century Gothic”, 20F);
            style1.ParagraphFormat.LineSpacing = 14;
            style1.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;

            //Append Text and Apply Style
            paragraph.AppendText(“This example demonstrates how to insert page break in Word.”);
            document.Styles.Add(style1);
            paragraph.ApplyStyle(style1.Name);

            //Add New Paragrah, Append Text and Set Format
            section.AddParagraph();
            paragraph = section.AddParagraph();
            TextRange txtRange = paragraph.AppendText(“Insert New Page”);
            txtRange.CharacterFormat.Font= new Font(“Century Gothic”, 20F);
            txtRange.CharacterFormat.TextColor = Color.DarkBlue;

            //Insert Break
            section = document.AddSection();
            section.BreakCode = SectionBreakType.NewPage;

            //Append Text and Set Format
            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText(“Next Page”);
            paragraph.ApplyStyle(style1.Name);

            //Save and Launch
            document.SaveToFile(“Break.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Break.docx”);
        }
    }
}

VB.NET:

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace InsertBreak
    Friend Class Break
        Shared Sub Main(ByVal args() As String)
            ‘Create Document and Add Section
            Dim document As New Document()
            Dim section As Section = document.AddSection()

            ‘Add Paragraph and Set Paragraph Style
            Dim paragraph As Paragraph = section.AddParagraph()
            Dim style1 As New ParagraphStyle(document)
            style1.Name = “Style1”
            style1.CharacterFormat.Font = New Font(“Century Gothic”, 20.0F)
            style1.ParagraphFormat.LineSpacing = 14
            style1.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple

            ‘Append Text and Apply Style
            paragraph.AppendText(“This example demonstrates how to insert page break in Word.”)
            document.Styles.Add(style1)
            paragraph.ApplyStyle(style1.Name)

            ‘Add New Paragrah, Append Text and Set Format
            section.AddParagraph()
            paragraph = section.AddParagraph()
            Dim txtRange As TextRange = paragraph.AppendText(“Insert New Page”)
            txtRange.CharacterFormat.Font = New Font(“Century Gothic”, 20.0F)
            txtRange.CharacterFormat.TextColor = Color.DarkBlue

            ‘Insert Break
            section = document.AddSection()
            section.BreakCode = SectionBreakType.NewPage

            ‘Append Text and Set Format
            section.AddParagraph()
            paragraph = section.AddParagraph()
            paragraph.AppendText(“Next Page”)
            paragraph.ApplyStyle(style1.Name)

            ‘Save and Launch
            document.SaveToFile(“Break.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Break.docx”)
        End Sub
    End Class
End Namespace

For other break settings, please use the following sentence:

            paragraph.AppendBreak(BreakType.PageBreak);

BreakType can be changed as ColumnBreak and LineBreak as well.

Result Shown as Following:

Freely Download Spire.Doc for .NET

Advertisements

One thought on “How to Insert Page Break in Word with C#/VB.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s