How to Set Word Pages (Page Setup) with C#/VB.NET

Word Page Setup is often used to make the appearance be the most appropriate with papers after printing. Generally speaking, page setup includes margins, orientation, paper and layout. In this post, I will introduce a method to work with page setup in Word by using C#/VB.NET.

When setting pages, we need to pay much attention on margin settings according to document contents, especially there are header and footer in document. Also, we can set page orientation if necessary.

In my example, I will set page size, margins (left, right, top and bottom) and page orientation. And I use a .NET Word component, Spire.Doc for .NET to help me complete it more quickly. Therefore, if you want to use the following code, please remember to add Spire.Doc as reference in your project.

Detailed Steps Shown as Following:           

  1. Load document which I want to set page from my computer.
  2. Set page, including page size, orientation and margins.
  3. Save and launch file.

Main Coding:

C#

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

namespace PageSetup
{
    class Setup
    {
        static void Main(string[] args)
        {

            //Load Document
            Document document = new Document();
            document.LoadFromFile(@”D:\work\Documents\welcome.docx”);
            Section section = document.Sections[0];

            //Page Setup
            section.PageSetup.PageSize = PageSize.A4;
            section.PageSetup.Orientation = PageOrientation.Landscape;
            section.PageSetup.Margins.Top = 72f;
            section.PageSetup.Margins.Bottom = 72f;
            section.PageSetup.Margins.Left = 89.85f;
            section.PageSetup.Margins.Right = 89.85f;

            //Save and Launch
            document.SaveToFile(“PageSetup.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“PageSetup.docx”);
        }
    }
}

VB.NET:

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace PageSetup
    Friend Class Setup
        Shared Sub Main(ByVal args() As String)

            ‘Load Document
            Dim document As New Document()
            document.LoadFromFile(“D:\work\Documents\welcome.docx”)
            Dim section As Section = document.Sections(0)

            ‘Page Setup
            section.PageSetup.PageSize = PageSize.A4
            section.PageSetup.Orientation = PageOrientation.Landscape
            section.PageSetup.Margins.Top = 72.0F
            section.PageSetup.Margins.Bottom = 72.0F
            section.PageSetup.Margins.Left = 89.85F
            section.PageSetup.Margins.Right = 89.85F

            ‘Save and Launch
            document.SaveToFile(“PageSetup.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“PageSetup.docx”)

        End Sub
    End Class
End Namespace

Result Shown as Following:

Download Spire.Doc for .NET Here

Spire.Office for .NET, component used to operate Word, Excel and PDF documents and export data, also can be used to realize the above function with the same function.

Advertisements

2 thoughts on “How to Set Word Pages (Page Setup) with C#/VB.NET

  1. rockgirlstories says:

    Hey I was wondering how I can change the timeline on my wordpress page. I’m writing about my experiences in the music business and I want it to go from young to old and it backwards. Can you help me out?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s