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.
This is how you can change the page size, orientation, and margins in a DOCX document using C# and .NET with Sautinsoft.
Complete code
using SautinSoft.Document;
using System.IO;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Get your free 100-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/page-setup.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 });
}
}
}
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 });
}
}
}
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: