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 Dec 06, 2023 1:41 pm

Hello,

Is there a way to scale an existing pdf document or add scaling when loading the pdf document stream?

Such as. here on XPS with Windows:
Code: Select all
            System.Windows.Documents.FixedPage page = new FixedPage();
            page.RenderTransform = new ScaleTransform(0.96, 0.96, page.Width, page.Height);


I need scaling without printing.

Spire.PDF 8.8.6
C#
Windows
.Net Framework 4.8

Thanks.

chd
 
Posts: 7
Joined: Mon Jul 16, 2018 9:22 am

Thu Dec 07, 2023 1:32 am

Hello,

Thank you for your inquiry.
To fulfill your requirement of scaling a PDF document, you can use the following code snippet:
Code: Select all
//Load the document from disk
PdfDocument originalDoc = new PdfDocument();
originalDoc.LoadFromFile("input.pdf");
//Set the margins
PdfMargins margins = new PdfMargins(0);

//Create a new pdf document
using (PdfDocument newDoc = new PdfDocument())
{
    float scale = 0.96f;
    for (int i = 0; i < originalDoc.Pages.Count; i++)
    {
        PdfPageBase page = originalDoc.Pages[i];

        //Use same scale to set width and height
        float width = page.Size.Width * scale;
        float height = page.Size.Height * scale;

        //Add new page with expected width and height
        PdfPageBase newPage = newDoc.Pages.Add(new SizeF(width, height), margins);
        newPage.Canvas.ScaleTransform(scale, scale);

        //Copy content of original page into new page
        newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF.Empty);
    }

    //save the document
    //newDoc.SaveToFile("result.pdf", FileFormat.PDF);
    MemoryStream stream = new MemoryStream();
    newDoc.SaveToStream(stream, FileFormat.PDF);
}

If you have any further questions or need additional assistance, please feel free to let me know.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Dec 07, 2023 10:04 am

Hello,

Thanks.
But your code snippet creates a new document.
I want scale an existing document, because i dont want copy all meta data.
For example a password or print settings.
In addition, the performance of a 100 page document is very poor.

So maybe you have a better solution?

chd
 
Posts: 7
Joined: Mon Jul 16, 2018 9:22 am

Fri Dec 08, 2023 3:21 am

Hello,

Thank you for your feedback.
If I understand correctly, you are looking to scale the pages of a PDF document when displaying it. If that is the case, please refer to the following code snippet as an example:
Code: Select all
PdfDocument pdfDocument = new PdfDocument();
pdfDocument.LoadFromFile("input.pdf");
PdfPageBase page = pdfDocument.Pages[0];
PdfDestination dest = new PdfDestination(page);
dest.Mode = PdfDestinationMode.Location;
dest.Location = new PointF(-4f, -4f);
dest.Zoom = 0.96f;
PdfGoToAction gotoAction = new PdfGoToAction(dest);
pdfDocument.AfterOpenAction = gotoAction;
pdfDocument.SaveToFile("result.pdf", FileFormat.PDF);

However, if you are looking to dynamically resize the PDF document pages themselves after loading, I apologize, this functionality is not currently supported by our software.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Dec 08, 2023 8:09 am

Hello,

I am looking to dynamically resize the PDF document pages themselves after loading or
when there are loaded in the PdfDocument() class.

Can you record this as a feature request and let us know if anything has changed here?
Thanks for help.

chd
 
Posts: 7
Joined: Mon Jul 16, 2018 9:22 am

Fri Dec 08, 2023 8:58 am

Hello,

Thank you for your feedback.
I've added your request as a new feature with the ticket SPIREPDF-6433 to our upgrade list. Once it is available, I will let you know immediately.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF