Skip to content

Instantly share code, notes, and snippets.

@jamesantrobus
Created January 9, 2011 15:38
Show Gist options
  • Save jamesantrobus/771759 to your computer and use it in GitHub Desktop.
Save jamesantrobus/771759 to your computer and use it in GitHub Desktop.
Rough demo to transform a CGPDFPage to a UIImage with a given width
private UIImage TransformToImage(CGPDFPage page, Single width)
{
RectangleF pageRect = page.GetBoxRect(CGPDFBox.Media);;
Single pdfScale = width / pageRect.Size.Width;
pageRect.Size = new SizeF(pageRect.Size.Width*pdfScale, pageRect.Size.Height*pdfScale);
//pageRect.Origin = CGPointZero;
UIGraphics.BeginImageContext(pageRect.Size);
CGContext context = UIGraphics.GetCurrentContext();
//White BG
context.SetRGBFillColor(1.0f,1.0f,1.0f,1.0f);
context.FillRect(pageRect);
context.SaveState();
// Next 3 lines makes the rotations so that the page look in the right direction
context.TranslateCTM(0.0f, pageRect.Size.Height);
context.ScaleCTM(1.0f, -1.0f);
CGAffineTransform transform = page.GetDrawingTransform(CGPDFBox.Media, pageRect, 0, true);
context.ConcatCTM(transform);
context.DrawPDFPage(page);
context.RestoreState();
UIImage img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment