How to change PDF page size in C#

When your PDF pages are with different sizes, it is very inconvenient for you to edit and view it. Sometimes, we need to change the page size of the PDF files if the characters in it are too big or too small. With the help of Spire.PDF, we can change the PDF page size from code. By changing the page size, all the characters and contents in the PDF file will be changed. Check the steps as below:

Make sure Spire.PDF for .NET (version 3.2.165 or above) has been installed correctly. Add Spire.PDF.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.PDF\Bin\NET4.0\ Spire.PDF.dll".

Step 1: Create a PDF document and load from file.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");

Step 2: Create a New PDF page with the same contents as the original PDF file and set its page size at A1.

//Create a new PDF document
PdfDocument newPdf = new PdfDocument();
//Traverse every PDF page
foreach (PdfPageBase page in pdf.Pages)
{
//Create a new PDF page with Page size =A1 and PDFMargins at 0.
PdfPageBase newPage = newPdf.Pages.Add(PdfPageSize.A1, new PdfMargins(0));
   
//Create an instance of PdfTextlayout.
 PdfTextLayout loLayout = new PdfTextLayout();
//Set the Layout property as OnePage.
loLayout.Layout = PdfLayoutType.OnePage;
//Copy the contents of page into the new PDF page at the PointF(0,0)
page.CreateTemplate().Draw(newPage, new PointF(0, 0), loLayout);
}

Step 3: Save the new PDF document to file.

newPdf.SaveToFile("Result.pdf");

Effective screenshots with the same scaling for the original PDF file and the new PDF file.

Original PDF file at 50% scaling:

How to change PDF page size in C#

New PDF file at 50% scaling:

How to change PDF page size in C#

Full codes:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace ChangePDFpagesize
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("Sample.pdf");

            PdfDocument newPdf = new PdfDocument();
            foreach (PdfPageBase page in pdf.Pages)
            {
                PdfPageBase newPage = newPdf.Pages.Add(PdfPageSize.A1, new PdfMargins(0));
                PdfTextLayout loLayout = new PdfTextLayout();
                loLayout.Layout = PdfLayoutType.OnePage;
                page.CreateTemplate().Draw(newPage, new PointF(0, 0), loLayout);
            }
            newPdf.SaveToFile("Result.pdf");
        }
    }
}