Daily using/supporting

Get Firefox browser!
Get Thunderbird!
Get Opera browser!
Get The Gimp!
Get Inkscape!
Get LibreOffice!
Get Videolan!
Get Linux!
Get Mandriva!
Get Joomla!
Hacker Emblem

Archives

Which topics would you like us to cover more?

Latest comments

Home Architecture, security and coding Scale a PDF using PdfSharp
Scale a PDF using PdfSharp
Written by Division by Zero   
Tuesday, 17 May 2011 10:33

In Februari I posted some code with an example of how to remove images from a PDF. If you just want to scale a PDF to another page format (this will not make the actual file smaller in size!), here's some code to show you how to do that.

// Open the PDF as a graphical object
XPdfForm form = XPdfForm.FromFile("example.PDF");
// Create a new document
PdfDocument doc2 = new PdfDocument();
doc2.Options.CompressContentStreams = true;
doc2.Options.ColorMode = PdfColorMode.Rgb;
// Loop through every page
for (int pagenumber=1;pagenumber < (form.PageCount + 1); pagenumber++)
{
// Add a new page to the new document with the right size
PdfPage page = doc2.Pages.Add();
page.Size = PageSize.A5;
// Get the graphics from the new page
// And get a rectangle exactly the size of the page
XGraphics graph = XGraphics.FromPdfPage(page);
// Go to the next page in the original document
form.PageNumber = pagenumber;
XRect rect = new XRect(0, 0, page.Width, page.Height);
// Draw the original page on the new page. This will scale
// the original to the size of the new page
// Of course you can use graph.ScaleTransform(0.5), but this is
// relevant to the size of the new page. 0.5 means that only half of
// the page is used.
graph.DrawImage(form, rect);
}
 
// Save the Pdf as another file
doc2.Save("result.pdf");
 

Add comment


Security code
Refresh

I think, therefore I am. - R. Descartes


© 2009 - 2013, Division by Zero

Template based on the empire template by joomlashack 

 Creative Commons License
This work by Division by Zero is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.