Set Word Page Margins in C#, VB.NET

Word Page Margins can be taken as distance between page edge and body contents. Users often adjust page margins to fit document body to page size for having the best layout. This guide focuses on introducing developers a solution to easily set Word page margin in C#, VB.NET via Spire.Doc for .NET, a professional .NET Word component.

Spire.Doc for .NET provides a PageSetup class which enables users to get page setup of current section in Word. Customize its Margins (top, bottom, left and right) properties in points and orientation (portrait, landscape) to ensure the layout to be comfortable and wonderful. The following screenshot presents result after setting page margins of Word.

Word Page Margins

Download and install Spire.Doc for .NET and follow the steps to set Word margins. Firstly, get the section which you can want to set margins of loaded document. Secondly, set Margins properties of PageSetup, including Top, Bottom, Left and Right. Thirdly, set Orientation property of PageSetup as Landscape. Code as following:

using Spire.Doc;
using Spire.Doc.Documents;

namespace WordMargin
    class Program
        static void Main(string[] args)
            //Load Document
            Document document = new Document();

            //Set Margins
            document.Sections[0].PageSetup.Margins.Top = 17.9f;
            document.Sections[0].PageSetup.Margins.Bottom = 17.9f;
            document.Sections[0].PageSetup.Margins.Left = 17.9f;
            document.Sections[0].PageSetup.Margins.Right = 17.9f;

            //Set Page Orientation
            document.Sections[0].PageSetup.Orientation = PageOrientation.Landscape;

            //Save and Launch
            document.SaveToFile("WordMargin.docx", FileFormat.Docx2010);
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordMargin
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()

            'Set Margins
            document.Sections(0).PageSetup.Margins.Top = 17.9F
            document.Sections(0).PageSetup.Margins.Bottom = 17.9F
            document.Sections(0).PageSetup.Margins.Left = 17.9F
            document.Sections(0).PageSetup.Margins.Right = 17.9F

            'Set Page Orientation
            document.Sections(0).PageSetup.Orientation = PageOrientation.Landscape

            'Save and Launch
            document.SaveToFile("WordMargin.docx", FileFormat.Docx2010)
        End Sub
    End Class
End Namespace

Spire.Doc, an easy-to-use Word component, is very powerful on processing Word documents, including generating, opening, saving, writing and editing for .NET, WPF and Silverlight.