Set Page Setup in Word Documents

The following example shows how to set page setup properties like orientation, margins, and paper type in a Word document using the GemBox.Document library in C# and VB.NET.

using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        document.Sections.Add(
            new Section(document,
                new Paragraph(document, "First section's page.")));

        document.Sections.Add(
            new Section(document,
                new Paragraph(document, "Second section's page, first paragraph."),
                new Paragraph(document, "Second section's page, second paragraph."),
                new Paragraph(document, "Second section's page, third paragraph.")));

        var pageSetup1 = document.Sections[0].PageSetup;
        var pageSetup2 = document.Sections[1].PageSetup;

        // Set page orientation.
        pageSetup1.Orientation = Orientation.Landscape;

        // Set page margins.
        pageSetup1.PageMargins.Top = 10;
        pageSetup1.PageMargins.Bottom = 10;

        // Set paper type.
        pageSetup2.PaperType = PaperType.A5;

        // Set line numbering.
        pageSetup2.LineNumberRestartSetting = LineNumberRestartSetting.NewPage;

        document.Save("Page Setup.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        document.Sections.Add(
            New Section(document,
                New Paragraph(document, "First section's page.")))

        document.Sections.Add(
            New Section(document,
                New Paragraph(document, "Second section's page, first paragraph."),
                New Paragraph(document, "Second section's page, second paragraph."),
                New Paragraph(document, "Second section's page, third paragraph.")))

        Dim pageSetup1 = document.Sections(0).PageSetup
        Dim pageSetup2 = document.Sections(1).PageSetup

        ' Set page orientation.
        pageSetup1.Orientation = Orientation.Landscape

        ' Set page margins.
        pageSetup1.PageMargins.Top = 10
        pageSetup1.PageMargins.Bottom = 10

        ' Set paper type.
        pageSetup2.PaperType = PaperType.A5

        ' Set line numbering.
        pageSetup2.LineNumberRestartSetting = LineNumberRestartSetting.NewPage

        document.Save("Page Setup.%OutputFileType%")

    End Sub
End Module
Word document with set page options like size and margin
Screenshot of Word file with specified page options

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy