C#/VB.NET: Adjust the Page Size of a Word Document

2023-06-05 03:05:00 Written by  support iceblue
Rate this item
(0 votes)

In certain cases, you may need to adjust the page size of a Word document to ensure that it fits your specific needs or preferences. For instance, if you are creating a document that will be printed on a small paper size, such as a brochure or flyer, you may want to decrease the page size of the document to avoid any cropping or scaling issues during printing. In this article, we will explain how to adjust the page size of a Word document in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Adjust the Page Size of a Word Document to a Standard Page Size in C# and VB.NET

With Spire.Doc for .NET, you can easily adjust the page sizes of Word documents to a variety of standard page sizes, such as A3, A4, A5, A6, B4, B5, B6, letter, legal, and tabloid. The following steps explain how to change the page size of a Word document to a standard page size using Spire.Doc for .NET:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document.LoadFromFile() method.
  • Iterate through the sections in the document.
  • Adjust the page size of each section to a standard page size by setting the value of the Section.PageSetup.PageSize property to a constant value of the PageSize enum.
  • Save the result document using the Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace ChangePageSizeToStandardSize
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("Input.docx");

            //Iterate through the sections in the document
            foreach (Section section in doc.Sections)
            {
                //Change the page size of each section to A4
                section.PageSetup.PageSize = PageSize.A4;
            }            

            //Save the result document
            doc.SaveToFile("StandardSize.docx", FileFormat.Docx2016);
        }
    }
}

C#/VB.NET: Adjust the Page Size of a Word Document

Adjust the Page Size of a Word Document to a Custom Page Size in C# and VB.NET

If you plan to print your document on paper with dimensions that don't match any standard paper size, you can change the page size of your document to a custom page size that matches the exact dimensions of the paper. The following steps explain how to change the page size of a Word document to a custom page size using Spire.Doc for .NET:

  • Initialize an instance of the Document class.
  • Load a Word document using the Document.LoadFromFile() method.
  • Initialize an instance of the SizeF structure from specified dimensions.
  • Iterate through the sections in the document.
  • Adjust the page size of each section to the specified dimensions by assigning the SizeF instance to the Section.PageSetup.PageSize property.
  • Save the result document using the Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing;

namespace ChangePageSizeToCustomSize
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("Input.docx");

            //Initialize an instance of the SizeF structure from specified dimensions
            SizeF customSize = new SizeF(600, 800);

            //Iterate through the sections in the document
            foreach (Section section in doc.Sections)
            {
                //Change the page size of each section to the specified dimensions
                section.PageSetup.PageSize = customSize;
            }

            //Save the result document
            doc.SaveToFile("CustomSize.docx", FileFormat.Docx2016);
        }
    }
}

C#/VB.NET: Adjust the Page Size of a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Monday, 05 June 2023 02:20