How to Insert Page Break in a Word Document with Silverlight

Page Break is used to start with contents in new page. Generally speaking, page break will be inserted automatically when one page are filled with text. Also, people can insert page break manually. For example, if we want to present title and subtitle in the first page and present body in the second body, we should insert a page break after typing the title.

In this post, I want to introduce the method about how to insert page break in Word with Silverlight. I prepare a document and want to insert a page break after paragraph 1. Also, the component, Spire.Doc for Silverlight is used in this example. Therefore, if you want to use the following code, please DOWNLOAD and install this component. Then, add its dll file as reference in your project.

Step 1. Design UserControl

Rename MainPage.xaml as InsertPageBreak.xaml. Double click it to design UserControl. Set background as an image. And then, add a label. Change label contents and set contents format. Finally, add a button to run.

Step 2. Declare SaveFileDialog

Declare a SaveFileDialog at the beginning to save document. Then, set a filter for this SaveFileDialog. The filter is used for users to choose the most appropriate document format. In this example, I just set filter format as .docx.

Step 3. Load Document

Right click project name to add existed item which is the document I want to insert page break. After adding, change the document Build Action as Embedded Resource.

Then, double click run button to write code. Declare a new document at first. Then, use foreach sentence to get document name from assembly. If the document name is the same as name of embedded resource, load this document.

Step 4. Insert Page Break

Because I want to insert page break after paragraph 1, so I need to get this paragraph in document section. Then, use paragraph.AppendBreak() method to insert break. The parameter passed to this method is break type. In this example, the type is PageBreak.

Note: because title is also taken as one paragraph, so the paragraph 1 in document becomes paragraph 2 when coding.

Step 5. Save Document

Judge if the SaveFileDialog which is declared in first step can pop up. If the result is true, save the document which we have inserted page break by using it.

Full InsertPageBreak.xaml.cs

using System;

using System.Windows;

using System.Windows.Controls;

using System.Reflection;

using System.IO;

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace PageBreak

{

    public partial class MainPage : UserControl

    {

        //Declare SaveFileDialog

        private SaveFileDialog saveFileDialog= new SaveFileDialog();

        public MainPage()

        {

            InitializeComponent();

            this.saveFileDialog.Filter = “Word Document (*.docx)|*.docx”;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            //Load Document

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;

            foreach (String name in assembly.GetManifestResourceNames())

            {

                if (name.EndsWith(“Blues Introduction.docx”))

                {

                    using (Stream docStream = assembly.GetManifestResourceStream(name))

                    {

                        document = new Document(docStream, FileFormat.Docx);

                    }

                }

            }

 

            //Insert Page Break

            Section section = document.Sections[0];

            Paragraph paragraph = section.Paragraphs[1];

            paragraph.AppendBreak(BreakType.PageBreak);

 

            //Save Document

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream stream = this.saveFileDialog.OpenFile())

                {

                    document.SaveToStream(stream, FileFormat.Docx);

                }

            }

        }

    }

}

Result

Original Document

Document with Page Break

Learn More about Silverlight Word Component, Spire.Doc for Silverlight.

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

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