DEV Community

Jeremy K.
Jeremy K.

Posted on

Resize PDF Page Sizes in C#

Resizing PDF pages is a frequent requirement in document processing workflows. Common use cases include scaling A4 PDFs to fit A3 print media, creating narrow custom layouts for mobile viewing, and standardizing page dimensions across multi-source PDF files. This tutorial demonstrates how to change PDF page sizes programmatically with C# using the free .NET library - Free Spire.PDF for .NET.


I. Install the Free .NET Library

The NuGet Package Manager offers the most straightforward installation path:

  • In Visual Studio, right-click your project → select Manage NuGet Packages → search for Free Spire.PDF → complete the installation.
  • Alternatively, run this command in the NuGet Package Manager Console:
Install-Package FreeSpire.PDF
Enter fullscreen mode Exit fullscreen mode

Note: Free Spire.PDF for .NET imposes a 10-page processing limit, making it ideal for small-to-medium PDF documents only.


II. Core Approach to Change PDF Page Sizes

This guide uses a "new PDF document + original page template rendering" strategy to adjust page sizes. Its key advantage is leveraging the OnePage setting of PdfTextLayout, which automatically scales content to fit the new page dimensions and eliminates content cropping or misalignment caused by direct page size modification.

PDF pages use points (pt) as the standard unit, with common conversions as follows:

  • 1 inch = 72 points
  • A4 (210mm × 297mm) = 595.28 pt × 841.89 pt
  • A3 (297mm × 420mm) = 841.89 pt × 1190.55 pt

Free Spire.PDF simplifies unit conversion with the built-in PdfUnitConvertor class, supporting seamless translation between inches, millimeters, centimeters, and points.

Scenario 1: Convert PDF to Standard Preset Page Sizes (e.g., A1)

This method is designed for batch-resizing PDFs to industry-standard formats (A1–A4, Letter, Legal, etc.). Use the predefined PdfPageSize enumeration to set dimensions directly, no manual unit conversion required.

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

namespace ChangePageSizeToStandardPaperSize
{
    class Program
    {
        static void Main(string[] args)
        {            
            // 1. Load the source PDF document
            PdfDocument originPdf = new PdfDocument();
            originPdf.LoadFromFile("Sample.pdf"); // Replace with your source file path

            // 2. Initialize a new PDF for resized output
            PdfDocument newPdf = new PdfDocument();

            // 3. Iterate through all pages and resize individually
            foreach(PdfPageBase page in originPdf.Pages)
            { 
                // 3.1 Add an A1-sized page with zero margins
                PdfPageBase newPage = newPdf.Pages.Add(PdfPageSize.A1, new PdfMargins(0));

                // 3.2 Enable one-page fitting to scale content automatically
                PdfTextLayout layout = new PdfTextLayout();
                layout.Layout = PdfLayoutType.OnePage;

                // 3.3 Create a template from the original page and draw to the new page
                PdfTemplate template = page.CreateTemplate();
                template.Draw(newPage, new PointF(0, 0), layout);
            }

            // 4. Save the resized PDF
            newPdf.SaveToFile("Standard_Size.pdf");

            // 5. Release file resources
            originPdf.Close();
            newPdf.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Breakdown:

  • PdfDocument: Separate instances for source and output files prevent accidental data loss from direct edits.
  • PdfPageSize.A1: Predefined constant for standard paper sizes, supporting A0–A4, Letter, Legal, and more.
  • PdfTextLayout: Setting Layout = PdfLayoutType.OnePage is critical—it dynamically scales original content to fit the new page perfectly.
  • PdfTemplate: Encapsulates the original page content as a reusable template, enabling clean, distortion-free rendering to the new dimensions.

Scenario 2: Set Custom Page Sizes to a PDF

Use this workflow for non-standard dimensions (e.g., 6.5 × 8.5 inches). The PdfUnitConvertor class automates inch-to-point conversion, removing manual calculation errors.

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

namespace ChangePageSizeToCustomPaperSize
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. Load the source PDF
            PdfDocument originPdf = new PdfDocument();
            originPdf.LoadFromFile("Sample.pdf"); // Replace with your file path

            // 2. Create a new PDF for custom-sized output
            PdfDocument newPdf = new PdfDocument();

            // 3. Initialize unit converter for inch-to-point translation
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            // 3.1 Convert custom dimensions to points
            float width = unitCvtr.ConvertUnits(6.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
            float height = unitCvtr.ConvertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
            // 3.2 Package custom size into a SizeF object
            SizeF customSize = new SizeF(width, height);

            // 4. Render each source page to a custom-sized new page
            foreach (PdfPageBase page in originPdf.Pages)
            {
                // 4.1 Add a custom-sized page with zero margins
                PdfPageBase newPage = newPdf.Pages.Add(customSize, new PdfMargins(0));

                // 4.2 Enable full-page content scaling
                PdfTextLayout layout = new PdfTextLayout();
                layout.Layout = PdfLayoutType.OnePage;

                // 4.3 Draw the original page template to the new page
                PdfTemplate template = page.CreateTemplate();
                template.Draw(newPage, new PointF(0, 0), layout);
            }

            // 5. Save the custom-sized PDF
            newPdf.SaveToFile("Custom_Size.pdf");

            // 6. Clean up resources
            originPdf.Close();
            newPdf.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Breakdown:

  • PdfUnitConvertor: Built-in utility supporting conversions between Inch, Millimeter, Point, and Centimeter.
  • ConvertUnits: Accepts three parameters (value, source unit, target unit) to eliminate manual math and reduce human error.
  • SizeF: Wraps custom width/height values for direct use in Pages.Add, supporting any non-standard page dimension.

III. Key Considerations

  1. Unit Consistency: Always use points for width/height values to avoid sizing discrepancies from mixed units.
  2. Content Scaling: PdfLayoutType.OnePage maintains native aspect ratio during auto-scaling. For manual scaling control, combine with the ScaleTransform method.
  3. Compatibility: Generated PDFs work with major readers (Adobe Acrobat, Chrome, Edge, etc.). Validate layout integrity for complex files with forms or annotations post-conversion.
  4. Resource Management: Always call Close() on PdfDocument instances to release file locks and prevent save failures.

This code-only solution requires no external tools and integrates seamlessly into .NET document-processing systems. It delivers reliable, high-quality PDF resizing for small-to-medium workloads, balancing simplicity and performance for production use cases.

Top comments (0)