Page Break

This example demonstrates how to set break style and add page breaks to a document.

A Page Break is inserted to tells the printing device where to end the current page and begin the next.

In a word file we add section and when one section is ended we like to tell word that page has ended and new page will begin .

After generate button is clicked it will launch a word with above version of word and showing added section ,paragraph and pagebreak

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using OfficeComponent.Word;

namespace OfficeComponent.Samples
{
    class PageBreakExample : WordExampleBase
    {
        public PageBreakExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public PageBreakExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            WordDocument document = new WordDocument();

            Section section = document.AddSection();

            Paragraph para = section.AddParagraph();
            para.AppendText("OfficeComponent");
            para.ApplyStyle(StyleIdentifier.Heading1);

            para = section.AddParagraph();
            string content;
            using (StreamReader reader = new StreamReader(CommonDataPath + "\\About.txt"))
            {
                content = reader.ReadToEnd();
            }
            TextRange text = para.AppendText(content);
            text.CharacterFormat.FontName = "Calibri";
            text.CharacterFormat.FontSize = 12;

            // Append line breaks.
            para.AppendBreak(BreakType.LineBreak);
            para.AppendBreak(BreakType.LineBreak);
            para.AppendBreak(BreakType.LineBreak);

            para.AppendBreak(BreakType.PageBreak);

            para = section.AddParagraph();
            para.AppendText("OfficeComponent Word");
            para.ApplyStyle(StyleIdentifier.Heading1);

            para = section.AddParagraph();
            using (StreamReader reader = new StreamReader(CommonDataPath + "\\OfficeComponentWord.txt"))
            {
                content = reader.ReadToEnd();
            }
            text = para.AppendText(content);
            text.CharacterFormat.FontName = "Calibri";
            text.CharacterFormat.FontSize = 12;

            string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
            // Save the document.
            document.Save(fileName, SaveAsFormat);

            // Close the document.
            document.Close();

            return fileName;
        }
    }
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text

Imports OfficeComponent.Word

Namespace OfficeComponent.Samples
	Friend Class PageBreakExample
		Inherits WordExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)

		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim document As New WordDocument()

			Dim section As Section = document.AddSection()

			Dim para As Paragraph = section.AddParagraph()
			para.AppendText("OfficeComponent")
			para.ApplyStyle(StyleIdentifier.Heading1)

			para = section.AddParagraph()
			Dim content As String
			Using reader As New StreamReader(CommonDataPath & "\About.txt")
				content = reader.ReadToEnd()
			End Using
			Dim text As TextRange = para.AppendText(content)
			text.CharacterFormat.FontName = "Calibri"
			text.CharacterFormat.FontSize = 12

			' Append line breaks.
			para.AppendBreak(BreakType.LineBreak)
			para.AppendBreak(BreakType.LineBreak)
			para.AppendBreak(BreakType.LineBreak)

			para.AppendBreak(BreakType.PageBreak)

			para = section.AddParagraph()
			para.AppendText("OfficeComponent Word")
			para.ApplyStyle(StyleIdentifier.Heading1)

			para = section.AddParagraph()
			Using reader As New StreamReader(CommonDataPath & "\OfficeComponentWord.txt")
				content = reader.ReadToEnd()
			End Using
			text = para.AppendText(content)
			text.CharacterFormat.FontName = "Calibri"
			text.CharacterFormat.FontSize = 12

			Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
			' Save the document.
			document.Save(fileName, SaveAsFormat)

			' Close the document.
			document.Close()

			Return fileName
		End Function
	End Class
End Namespace