Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Feb 19, 2025 9:24 pm

Hello,

I am using your instructions entitled "C#/VB.NET: Change PDF Page Size" to resize PDF’s to letter size paper:

That works great, but our problem is that the source PDF’s will be in various sizes (any size, unknown). We need to maintain the aspect ratio of the original PDF and resize it to fit on letter-size paper. Are you able to provide an example showing how to accomplish that?

This is in a .NET 8 class library project using Spire.PDF version 11.2.4

Thank you!

bwhite_mlhc
 
Posts: 3
Joined: Tue May 18, 2021 7:26 pm

Wed Feb 19, 2025 10:05 pm

I have attached a sample that has the issue

bwhite_mlhc
 
Posts: 3
Joined: Tue May 18, 2021 7:26 pm

Thu Feb 20, 2025 6:48 am

Hi,

Thanks for your inquiry.
Please refer to the following code to meet your requirement. If it doesn't help, please provide more information to help us have a better understanding.
Code: Select all
//Load the original PDF document
PdfDocument originPdf = new PdfDocument();
originPdf.LoadFromFile("download.pdf");

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

//Loop through the pages in the original PDF
foreach (PdfPageBase page in originPdf.Pages)
{
    SizeF size = PdfPageSize.Letter;
    PdfPageBase newPage;
    if (page.Size.Width > page.Size.Height)
    {
        // Landscape
        newPage = newPdf.Pages.Add(new SizeF(size.Height, size.Width), new PdfMargins(0));
    }
    else {
        // Portrait 
        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);
}

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Fri Feb 21, 2025 8:13 pm

That seems to work great, thank you Nina!

bwhite_mlhc
 
Posts: 3
Joined: Tue May 18, 2021 7:26 pm

Mon Feb 24, 2025 1:18 am

Hi,

You're welcome.
If you need assistance in the future, please feel free to write back.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF