Change PDF Page Size with C#
Contents
[
Hide
]
Change PDF Page Size
Aspose.PDF for .NET lets you change PDF page size with simple lines of code in your .NET applications. This topic explains how to update/change the page dimensions (size) of an existing PDF file.
The following code snippet also work with Aspose.PDF.Drawing library.
The Page class contains the SetPageSize(…) method which lets you set the page size. The code snippet below updates page dimensions in a few easy steps:
- Load the source PDF file.
- Get the pages into the PageCollection object.
- Get a given page.
- Call the SetPageSize(..) method to update its dimensions.
- Call the Document class’ Save(..) method to generate the PDF file with updated page dimensions.
Please note that the height and width properties use points as basic unit, where 1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points.
The following code snippet shows how to change the PDF page dimensions to A4 size.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ChangePdfPageSize()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "UpdateDimensions.pdf"))
{
// Get page collection
var pageCollection = document.Pages;
// Get particular page
var pdfPage = pageCollection[1];
// Set the page size as A4 (11.7 x 8.3 in) and in Aspose.Pdf, 1 inch = 72 points
// So A4 dimensions in points will be (842.4, 597.6)
pdfPage.SetPageSize(597.6, 842.4);
// Save PDF document
document.Save(dataDir + "UpdateDimensions_out.pdf");
}
}
Get PDF Page Size
You can read PDF page size of an existing PDF file using Aspose.PDF for .NET. The following code sample shows how to read PDF page dimensions using C#.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetPdfPageSize()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "UpdateDimensions.pdf"))
{
// Adds a blank page to pdf document
Page page = document.Pages.Count > 0 ? document.Pages[1] : document.Pages.Add();
// Get page height and width information
Console.WriteLine(page.GetPageRect(true).Width.ToString() + ":" + page.GetPageRect(true).Height);
// Rotate page at 90 degree angle
page.Rotate = Rotation.on90;
// Get page height and width information
Console.WriteLine(page.GetPageRect(true).Width.ToString() + ":" + page.GetPageRect(true).Height);
}
}