How to Insert Word Page Break
Microsoft Word provides so many great features which can be very useful when we handle tons of documents. For example, sometimes in a long content Word document we may want to insert page break or section break before the application does it automatically. Insert page break will make a new page but because in the same section it keeps the same general formatting as the page above it. Here in this post you will know a solution of how to use C# to insert Word page break.

This solution will use Spire.Doc, a powerful all-in-one .NET/Silverlight Word component. Download Spire.Doc here. Make sure Spire.Doc and Visual Studio are correctly installed on system. Follow the simple steps below to insert Word page break.

Step 1 Create Project
Create a C# windows form project in visual studio. Add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.

using System;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;

namespace pagebreak
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
        }
    }
}  

Step 2 Load Word Document
Load word document which we need insert page break in.

            Document document = new Document();
            document.LoadFromFile(@"D:\Word Nested Mail Merge.doc");

Step 3 Insert Word Page Break
Spire.Doc presents an easy solution to insert Word page break. The sample code below is showing you how to insert Word page break according to paragraphs.

        private void InsertBreak(Document document)
        {
            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[2];
            paragraph.AppendBreak(BreakType.PageBreak);
        }

Step 4 Save and Preview

        //Save doc file.
        document.SaveToFile("Sample.doc", FileFormat.Doc);
        private void WordDocViewer(string fileName)

        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch { }
        }

Start the project and check out the page break effect. 

More about Spire.Doc:

Spire.Doc is a MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more… 
 


Comments




Leave a Reply