DEV Community

Leon Davis
Leon Davis

Posted on

Resize PDF Pages in C#: Change PDF Page Size

PDF page size is often overlooked until a document needs to be printed, archived, or processed in bulk. In many business systems, PDF files may come from different sources: scanned documents, exported reports, generated invoices, or legacy templates. As a result, the page dimensions may not be consistent.

In a .NET application, one practical way to resize PDF pages is to create a new PDF document with the desired page size, then draw each original page onto the new page as a template. This keeps the visual content of the source PDF, including text, images, tables, and layout, while producing a document with standardized page dimensions.

This article demonstrates how to resize PDF pages in C# for two common scenarios:

  • Changing PDF pages to a standard paper size such as A4, A3, A1, Letter, or Legal
  • Changing PDF pages to a custom size defined in inches or millimeters

The examples use C# and Spire.PDF for .NET.


Install the PDF Library

Install the package from NuGet:

PM> Install-Package Spire.PDF
Enter fullscreen mode Exit fullscreen mode

Or use the .NET CLI:

dotnet add package Spire.PDF
Enter fullscreen mode Exit fullscreen mode

Then add the required namespaces:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
Enter fullscreen mode Exit fullscreen mode

How PDF Page Resizing Works

A PDF page has a fixed coordinate system. Unlike a Word document or an HTML page, its content does not automatically reflow when the page size changes.

For this reason, resizing a PDF page usually requires a different approach:

  1. Load the original PDF document.
  2. Create a new PDF document.
  3. Add a new page with the target page size.
  4. Convert the original page into a template.
  5. Draw the template onto the new page.
  6. Save the resized PDF file.

The key operation is creating a template from the original page:

PdfTemplate template = sourcePage.CreateTemplate();
template.Draw(newPage, new PointF(0, 0), layout);
Enter fullscreen mode Exit fullscreen mode

This approach preserves the page appearance and scales the original page content into the new page area.


Resize PDF Pages to a Standard Paper Size in C

If the target size is a common paper format, you can use predefined values from PdfPageSize, such as A4, A3, A1, Letter, or Legal.

The following example resizes every page in a PDF document to A1 size.

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

namespace ResizePdfToStandardSize
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument sourcePdf = new PdfDocument();
            sourcePdf.LoadFromFile("Sample.pdf");

            PdfDocument outputPdf = new PdfDocument();

            foreach (PdfPageBase sourcePage in sourcePdf.Pages)
            {
                PdfPageBase newPage = outputPdf.Pages.Add(
                    PdfPageSize.A1,
                    new PdfMargins(0)
                );

                PdfTextLayout layout = new PdfTextLayout();
                layout.Layout = PdfLayoutType.OnePage;

                PdfTemplate template = sourcePage.CreateTemplate();
                template.Draw(newPage, new PointF(0, 0), layout);
            }

            outputPdf.SaveToFile("ResizeToA1.pdf");

            sourcePdf.Close();
            outputPdf.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

To resize the PDF to A4 instead, replace PdfPageSize.A1 with PdfPageSize.A4:

PdfPageBase newPage = outputPdf.Pages.Add(
    PdfPageSize.A4,
    new PdfMargins(0)
);
Enter fullscreen mode Exit fullscreen mode

Other standard page sizes can be used in the same way:

PdfPageSize.A3
PdfPageSize.A4
PdfPageSize.Letter
PdfPageSize.Legal
Enter fullscreen mode Exit fullscreen mode

The second argument controls the page margins:

new PdfMargins(0)
Enter fullscreen mode Exit fullscreen mode

A margin value of 0 allows the original page template to use the full target page area. If you need additional whitespace around the content, you can set a larger margin value:

new PdfMargins(20)
Enter fullscreen mode Exit fullscreen mode

Keep in mind that margins reduce the available drawing area, so the rendered page content may appear smaller.


Why PdfLayoutType.OnePage Is Important

The following setting is important when drawing the original page onto the resized page:

PdfTextLayout layout = new PdfTextLayout();
layout.Layout = PdfLayoutType.OnePage;
Enter fullscreen mode Exit fullscreen mode

PdfLayoutType.OnePage helps fit the source page content into a single target page. Without this setting, the content may not scale as expected, especially when the original page size and the target page size are significantly different.

This is especially relevant when:

  • Resizing large pages to A4 or Letter
  • Enlarging small pages to A3 or A1
  • Processing scanned PDF pages
  • Normalizing PDFs with inconsistent page dimensions

It does not redesign the PDF layout, but it helps preserve the original page as a scaled visual representation.


Resize PDF Pages to a Custom Size in C

In some cases, a standard paper size is not suitable. For example, you may need to generate PDFs for labels, tickets, certificates, forms, or a specific print template.

Spire.PDF uses points as the measurement unit for page dimensions. One inch equals 72 points. Instead of calculating the values manually, you can use PdfUnitConvertor to convert inches, millimeters, or other units to points.

The following example resizes all pages to 6.5 × 8.5 inches.

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

namespace ResizePdfToCustomSize
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument sourcePdf = new PdfDocument();
            sourcePdf.LoadFromFile("Sample.pdf");

            PdfDocument outputPdf = new PdfDocument();

            PdfUnitConvertor convertor = new PdfUnitConvertor();

            float width = convertor.ConvertUnits(
                6.5f,
                PdfGraphicsUnit.Inch,
                PdfGraphicsUnit.Point
            );

            float height = convertor.ConvertUnits(
                8.5f,
                PdfGraphicsUnit.Inch,
                PdfGraphicsUnit.Point
            );

            SizeF customPageSize = new SizeF(width, height);

            foreach (PdfPageBase sourcePage in sourcePdf.Pages)
            {
                PdfPageBase newPage = outputPdf.Pages.Add(
                    customPageSize,
                    new PdfMargins(0)
                );

                PdfTextLayout layout = new PdfTextLayout();
                layout.Layout = PdfLayoutType.OnePage;

                PdfTemplate template = sourcePage.CreateTemplate();
                template.Draw(newPage, new PointF(0, 0), layout);
            }

            outputPdf.SaveToFile("ResizeToCustomSize.pdf");

            sourcePdf.Close();
            outputPdf.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The custom page size is created with SizeF:

SizeF customPageSize = new SizeF(width, height);
Enter fullscreen mode Exit fullscreen mode

Then the custom size is used when adding pages to the output PDF:

PdfPageBase newPage = outputPdf.Pages.Add(
    customPageSize,
    new PdfMargins(0)
);
Enter fullscreen mode Exit fullscreen mode

This method is useful when the target PDF page size comes from a business requirement rather than a standard paper format.


Convert Millimeters to PDF Page Size

Print specifications are often written in millimeters. For example, an A4-sized page is commonly represented as 210mm × 297mm.

You can convert millimeters to points like this:

PdfUnitConvertor convertor = new PdfUnitConvertor();

float width = convertor.ConvertUnits(
    210f,
    PdfGraphicsUnit.Millimeter,
    PdfGraphicsUnit.Point
);

float height = convertor.ConvertUnits(
    297f,
    PdfGraphicsUnit.Millimeter,
    PdfGraphicsUnit.Point
);

SizeF pageSize = new SizeF(width, height);
Enter fullscreen mode Exit fullscreen mode

Then use the converted pageSize when creating the new page:

PdfPageBase newPage = outputPdf.Pages.Add(
    pageSize,
    new PdfMargins(0)
);
Enter fullscreen mode Exit fullscreen mode

Using PdfUnitConvertor keeps the code readable and avoids hardcoded point values that may be difficult to understand later.


Practical Considerations

Check the Aspect Ratio

If the original page and the target page use different aspect ratios, the resized PDF may contain extra whitespace or slightly compressed content. For example, converting a landscape page to portrait A4 can preserve the page content, but the visual result may not be ideal.

Review Scanned PDFs Carefully

Scanned PDFs often include hidden margins, uneven borders, or slightly rotated images. Resizing the page will also preserve these visual details, so the output should be checked manually if the source file comes from scans.

Avoid Overwriting the Original PDF

It is better to save the resized file as a new PDF instead of replacing the original one directly. This makes it easier to compare the output and roll back if the result is not correct.

Be Careful with Mixed Page Sizes

Some PDFs contain pages with different sizes by design. The examples above resize every page to the same target size, which is useful for printing and archiving, but it may not be suitable for every document.


Conclusion

Resizing PDF pages in C# is not just about changing a page property. A more reliable approach is to create a new PDF with the target page size and draw each original page onto the new page as a template.

For standard paper sizes such as A4, A3, Letter, or Legal, predefined page sizes can be used directly. For custom dimensions, the width and height can be converted from inches or millimeters to points before creating the new page.

This method works well when the goal is to preserve the original visual layout while standardizing page dimensions. After generating the resized PDF, it is still worth reviewing a few pages manually, especially when the source file contains scanned pages, mixed page sizes, or unusual aspect ratios.

Top comments (0)