PDFsharp - Change page size and scale content
Asked Answered
G

2

7

Is it possible to scale a page from e.g. A2 to A1 with PDFsharp? I can set the size of the page via Size, Width and Height. But how can I scale the content of the page?

Gravitate answered 27/11, 2017 at 14:48 Comment(0)
L
5

You can use DrawImage() to draw an existing PDF page on a new PDF page. You can specify the destination rectangle and thus you can scale the page as needed.

Use the XPdfForm class to access the existing PDF file.

See the Two Pages on One sample for details:
http://www.pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx

Less answered 27/11, 2017 at 14:58 Comment(0)
B
10

based on the comment from Vive and the link provided there, here an example for resizing to A4 with C#:

you must include:

    using PdfSharp.Pdf;
    using PdfSharp.Drawing;
    using PdfSharp;

then:

    // resize this  file from A3 to A4
    string filename = @"C:\temp\A3.pdf";

    // Create the new output document (A4)
    PdfDocument outputDocument = new PdfDocument();
    outputDocument.PageLayout = PdfPageLayout.SinglePage;

    XGraphics gfx;
    XRect box;
    // Open the file to resize
    XPdfForm form = XPdfForm.FromFile(filename);

    // Add a new page to the output document
    PdfPage page = outputDocument.AddPage();

    if (form.PixelWidth > form.PixelHeight)
         page.Orientation = PageOrientation.Landscape;
    else
         page.Orientation = PageOrientation.Portrait;

     double width = page.Width;
     double height = page.Height;

     gfx = XGraphics.FromPdfPage(page);
     box = new XRect(0, 0, width, height);
     gfx.DrawImage(form, box);

     // Save the document...
     string newfilename = @"c:\temp\resized.pdf";
     outputDocument.Save(newfilename);
Benetta answered 1/8, 2019 at 7:44 Comment(0)
L
5

You can use DrawImage() to draw an existing PDF page on a new PDF page. You can specify the destination rectangle and thus you can scale the page as needed.

Use the XPdfForm class to access the existing PDF file.

See the Two Pages on One sample for details:
http://www.pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx

Less answered 27/11, 2017 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.