Word Insert Break in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to insert break.

private void button1_Click(object sender, EventArgs e)
{
    //Create word document
    Document document = new Document();

    InsertBreak(document);

    //Save doc file.
    document.SaveToFile("Sample.doc",FileFormat.Doc);

    //Launching the MS Word file.
    WordDocViewer("Sample.doc");


}

private void InsertBreak(Document document)
{
    Section section = document.AddSection();
    Paragraph paragraph = section.AddParagraph();
    paragraph.AppendText("The sample demonstrates how to insert break.");
    paragraph.ApplyStyle(BuiltinStyle.Heading2);

    section.AddParagraph();
    paragraph = section.AddParagraph();
    paragraph.AppendText("Insert new page.");
    paragraph.ApplyStyle(BuiltinStyle.Heading4);

    section = document.AddSection();
    section.BreakCode = SectionBreakType.NewPage;

    section.AddParagraph();
    paragraph = section.AddParagraph();
    paragraph.AppendText("Next page.");
    paragraph.ApplyStyle(BuiltinStyle.Heading4);

}

private void WordDocViewer(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch { }
}

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
	'Create word document
	Dim document_Renamed As New Document()

	InsertBreak(document_Renamed)

	'Save doc file.
	document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)

	'Launching the MS Word file.
	WordDocViewer("Sample.doc")


End Sub

Private Sub InsertBreak(ByVal document_Renamed As Document)
	Dim section_Renamed As Section = document_Renamed.AddSection()
	Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()
	paragraph_Renamed.AppendText("The sample demonstrates how to insert break.")
	paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)

	section_Renamed.AddParagraph()
	paragraph_Renamed = section_Renamed.AddParagraph()
	paragraph_Renamed.AppendText("Insert new page.")
	paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading4)

	section_Renamed = document_Renamed.AddSection()
	section_Renamed.BreakCode = SectionBreakType.NewPage

	section_Renamed.AddParagraph()
	paragraph_Renamed = section_Renamed.AddParagraph()
	paragraph_Renamed.AppendText("Next page.")
	paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading4)

End Sub

Private Sub WordDocViewer(ByVal fileName As String)
	Try
		Process.Start(fileName)
	Catch
	End Try
End Sub