Resize PDF pages

Resize PDF pages

This sample demonstrates how to change the size of pages in the PDF file. For resizing the pages, a new page is created with the specified width and height and all the shapes of the original page are simply put on the new resized page which is then written to the disk as a PDF file.

const double scale = 0.5;

using (FileStream inputStream = new FileStream(@"..\..\../inputdocuments/PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read))
{
    // open the source document
    Document documentIn = new Document(inputStream);

    // create the target document
    Document documentOut = new Document();

    // enumerate the pages in the source document
    foreach (Page originalPage in documentIn.Pages)
    {
        // calculate a new size of the page
        double scaledWidth = originalPage.Width * scale;
        double scaledHeight = originalPage.Height * scale;

        // append a scaled version of the original page to the target document
        Page scaledPage = new Page(scaledWidth, scaledHeight);
        documentOut.Pages.Add(scaledPage);

        PageShape pageShape = new PageShape(originalPage, 0, 0, scaledPage.Width, scaledPage.Height);
        scaledPage.VisualOverlay.Add(pageShape);
    }

    // write the target document to disk
    using (FileStream outFile = new FileStream(@"..\..\output.pdf", FileMode.Create, FileAccess.Write))
    {
        documentOut.Write(outFile);
    }
}
const double scale = 0.5;

using (FileStream inputStream = new FileStream(@"..\..\../inputdocuments/PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read))
{
    // open the source document
    Document documentIn = new Document(inputStream);

    // create the target document
    Document documentOut = new Document();

    // enumerate the pages in the source document
    foreach (Page originalPage in documentIn.Pages)
    {
        // calculate a new size of the page
        double scaledWidth = originalPage.Width * scale;
        double scaledHeight = originalPage.Height * scale;

        // append a scaled version of the original page to the target document
        Page scaledPage = new Page(scaledWidth, scaledHeight);
        documentOut.Pages.Add(scaledPage);

        PageShape pageShape = new PageShape(originalPage, 0, 0, scaledPage.Width, scaledPage.Height);
        scaledPage.VisualOverlay.Add(pageShape);
    }

    // write the target document to disk
    using (FileStream outFile = new FileStream(@"..\..\output.pdf", FileMode.Create, FileAccess.Write))
    {
        documentOut.Write(outFile);
    }
}

The size of the original PDF page:

Original Pdf Size

The size of the resized PDF page:

Resized Pdf Page