C#/VB.NET: Change PDF Page Size

There may be a situation where you need to change the page size of a PDF file. For instance, you need to print a PDF file, but its page size is different from the paper size that your printer uses. In this article, we will demonstrate how to change the page size of a PDF file in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

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

PM> Install-Package Spire.PDF

Change PDF Page Size to a Standard Paper Size in C# and VB.NET

The way to change the page size of a PDF file is to create a new PDF file and add pages of the desired size to it, then create templates based on the pages in the original PDF file and draw the templates onto the pages in the new PDF file. This process will preserve text, images, and other elements present in the original PDF file.

Spire.PDF for .NET supports a variety of standard paper sizes like letter, legal, A0, A1, A2, A3, A4, B0, B1, B2, B3, B4 and many more. The following steps show you how to change the page size of a PDF file to a standard paper size:

  • Initialize a PdfDocument instance and load the original PDF file using PdfDocument.LoadFromFile() method.
  • Initialize another PdfDocument instance to create a new PDF file.
  • Loop through the pages in the original PDF.
  • Add pages of the desired size to the new PDF file using PdfDocument.Pages.Add() method.
  • Initialize a PdfTextLayout instance and set the text layout as one page through PdfTextLayout.Layout property.
  • Create templates based on the pages in the original PDF using PdfPageBase.CreateTemplate() method.
  • Draw the templates onto the pages in the new PDF file with the specified text layout using PdfTemplate.Draw() method.
  • Save the result file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace ChangePageSizeToStandardPaperSize
{
    class Program
    {
        static void Main(string[] args)
        {            
            //Create a PdfDocument instance
            PdfDocument originPdf = new PdfDocument();
            //Load the original PDF document
            originPdf.LoadFromFile("Sample.pdf");

            //Create a new PDF document
            PdfDocument newPdf = new PdfDocument();

            //Loop through the pages in the original PDF
            foreach(PdfPageBase page in originPdf.Pages)
            { 
                //Add pages of size A1 to the new PDF
                PdfPageBase newPage = newPdf.Pages.Add(PdfPageSize.A1, new PdfMargins(0));
                //Create a PdfTextLayout instance
                PdfTextLayout layout = new PdfTextLayout();
                //Set text layout as one page (if not set the content will not scale to fit page size)
                layout.Layout = PdfLayoutType.OnePage;
                //Create templates based on the pages in the original PDF
                PdfTemplate template = page.CreateTemplate();
                //Draw the templates onto the pages in the new PDF
                template.Draw(newPage, new PointF(0, 0), layout);
            }

            //Save the result document
            newPdf.SaveToFile("ChangePageSizeToA1.pdf");
        }
    }
}

C#/VB.NET: Change PDF Page Size

Change PDF Page Size to a Custom Paper Size in C# and VB.NET

Spire.PDF for .NET uses point (1/72 of an inch) as the unit of measure. If you need to change the page size of a PDF to a custom paper size in other units of measure like inches or millimeters, you can use the PdfUnitConvertor class to convert them to points.

The following steps show you how to change the page size of a PDF file to a custom paper size in inches:

  • Initialize a PdfDocument instance and load the original PDF file using PdfDocument.LoadFromFile() method.
  • Initialize another PdfDocument instance to create a new PDF file.
  • Initialize a PdfUnitConvertor instance, then convert the custom size in inches to points using PdfUnitConvertor.ConvertUnits() method.
  • Initialize a SizeF instance from the custom size.
  • Loop through the pages in the original PDF.
  • Add pages of the custom size to the new PDF file using PdfDocument.Pages.Add() method.
  • Initialize a PdfTextLayout instance and set the text layout as one page through PdfTextLayout.Layout property.
  • Create templates from the pages in the original PDF using PdfPageBase.CreateTemplate() method.
  • Draw the templates onto the pages in the new PDF file with the specified text layout using PdfTemplate.Draw() method.
  • Save the result file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace ChangePageSizeToCustomPaperSize
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument originPdf = new PdfDocument();
            //Load the original PDF document
            originPdf.LoadFromFile("Sample.pdf");

            //Create a new PDF document
            PdfDocument newPdf = new PdfDocument();

            //Create a PdfUnitConvertor instance
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            //Convert the custom size in inches to points
            float width = unitCvtr.ConvertUnits(6.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
            float height = unitCvtr.ConvertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
            //Create a new SizeF instance from the custom size, then it will be used as the page size of the new PDF
            SizeF size = new SizeF(width, height);

            //Loop through the pages in the original PDF
            foreach (PdfPageBase page in originPdf.Pages)
            {
                //Add pages of the custom size (6.5*8.5 inches) to the new PDF
                PdfPageBase newPage = newPdf.Pages.Add(size, new PdfMargins(0));
                //Create a PdfTextLayout instance
                PdfTextLayout layout = new PdfTextLayout();
                //Set text layout as one page (if not set the content will not scale to fit page size)
                layout.Layout = PdfLayoutType.OnePage;
                //Create templates based on the pages in the original PDF
                PdfTemplate template = page.CreateTemplate();
                //Draw the templates onto the pages in the new PDF
                template.Draw(newPage, new PointF(0, 0), layout);
            }

            //Save the result document
            newPdf.SaveToFile("ChangePageSizeToCustomSize.pdf");
        }
    }
}

C#/VB.NET: Change PDF Page Size

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.