Thursday, 10 January 2013 03:11

Set Word Page Margins in C#, VB.NET

Written by  support iceblue
Rate this item
(0 votes)

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:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordMargin
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Valentine.docx");

            //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);
            System.Diagnostics.Process.Start("WordMargin.docx");
        }
    }
}
[VB.NET]
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()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Valentine.docx")

            '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)
            System.Diagnostics.Process.Start("WordMargin.docx")
        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.

Additional Info

  • tutorial_title: Set Word Page Margins
Last modified on Monday, 14 July 2014 06:05