Resize PDF pages

Manipulate PDF, Shapes
4/4/2014

Downloads

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.

C# code sample for resizing PDF pages

1 const double scale = 0.5; 2 3 using (FileStream inputStream = new FileStream(@"..\..\../inputdocuments/PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read)) 4 { 5 // open the source document 6 Document documentIn = new Document(inputStream); 7 8 // create the target document 9 Document documentOut = new Document(); 10 11 // enumerate the pages in the source document 12 foreach (Page originalPage in documentIn.Pages) 13 { 14 // calculate a new size of the page 15 double scaledWidth = originalPage.Width * scale; 16 double scaledHeight = originalPage.Height * scale; 17 18 // append a scaled version of the original page to the target document 19 Page scaledPage = new Page(scaledWidth, scaledHeight); 20 documentOut.Pages.Add(scaledPage); 21 22 PageShape pageShape = new PageShape(originalPage, 0, 0, scaledPage.Width, scaledPage.Height); 23 scaledPage.VisualOverlay.Add(pageShape); 24 } 25 26 // write the target document to disk 27 using (FileStream outFile = new FileStream(@"..\..\output.pdf", FileMode.Create, FileAccess.Write)) 28 { 29 documentOut.Write(outFile); 30 } 31 }

VB.NET code sample for resizing PDF pages

1 const double scale = 0.5; 2 3 using (FileStream inputStream = new FileStream(@"..\..\../inputdocuments/PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read)) 4 { 5 // open the source document 6 Document documentIn = new Document(inputStream); 7 8 // create the target document 9 Document documentOut = new Document(); 10 11 // enumerate the pages in the source document 12 foreach (Page originalPage in documentIn.Pages) 13 { 14 // calculate a new size of the page 15 double scaledWidth = originalPage.Width * scale; 16 double scaledHeight = originalPage.Height * scale; 17 18 // append a scaled version of the original page to the target document 19 Page scaledPage = new Page(scaledWidth, scaledHeight); 20 documentOut.Pages.Add(scaledPage); 21 22 PageShape pageShape = new PageShape(originalPage, 0, 0, scaledPage.Width, scaledPage.Height); 23 scaledPage.VisualOverlay.Add(pageShape); 24 } 25 26 // write the target document to disk 27 using (FileStream outFile = new FileStream(@"..\..\output.pdf", FileMode.Create, FileAccess.Write)) 28 { 29 documentOut.Write(outFile); 30 } 31 }

The size of the original PDF page:

original-pdf-size.png

The size of the resized PDF page:

resized-pdf-page.png