Change Page Size and Margins in DOCX document using C# and .NET

To change the page size, orientation, and margins in a DOCX document using C# and .NET, we can use the Sautinsoft.Document library, which allows us to programmatically manipulate DOCX files.

  1. Add SautinSoft.Document from Nuget.
  2. Create or load a document.
  3. Add a new section.
  4. Set new the page size, orientation, and margins.
  5. Save the document in the desired format.

This is how you can change the page size, orientation, and margins in a DOCX document using C# and .NET with Sautinsoft.

Change Page Size and Margins

Complete code

using SautinSoft.Document;
using System.IO;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:
            // https://sautinsoft.com/start-for-free/

            ChangePageProperties();
        }

        /// <summary>
        /// How to adjust a document page properties.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/change-page-size-and-margins-in-docx-csharp-net.php
        /// </remarks>
        public static void ChangePageProperties()
        {
            var inpFile = Path.GetFullPath(@"..\..\..\example.docx");
            var outFile = Path.GetFullPath("Result.docx");
            DocumentCore dc = DocumentCore.Load(inpFile);

            // Apply page size, orientation and margins.
            foreach (var sect in dc.Sections)
            {
                sect.PageSetup.PaperType = PaperType.A3;
                sect.PageSetup.Orientation = Orientation.Landscape;
                sect.PageSetup.PageMargins.Left = LengthUnitConverter.Convert(5.0, LengthUnit.Centimeter, LengthUnit.Point);
            }

            dc.Save(outFile);

            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

using SautinSoft.Document;
using System.IO;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:
            // https://sautinsoft.com/start-for-free/

            ChangePageProperties();
        }

        /// <summary>
        /// How to adjust a document page properties.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/change-page-size-and-margins-in-docx-csharp-net.php
        /// </remarks>
        public static void ChangePageProperties()
        {
            var inpFile = Path.GetFullPath(@"..\..\..\example.docx");
            var outFile = Path.GetFullPath("Result.docx");
            DocumentCore dc = DocumentCore.Load(inpFile);

            // Apply page size, orientation and margins.
            foreach (var sect in dc.Sections)
            {
                sect.PageSetup.PaperType = PaperType.A3;
                sect.PageSetup.Orientation = Orientation.Landscape;
                sect.PageSetup.PageMargins.Left = LengthUnitConverter.Convert(5.0, LengthUnit.Centimeter, LengthUnit.Point);
            }

            dc.Save(outFile);

            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.