PDFsharp - Change page size and scale content

Viewed 8806

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?

2 Answers

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);

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

Related