Skip to content

Instantly share code, notes, and snippets.

@madebyjeffrey
Created May 30, 2011 19:56
Show Gist options
  • Save madebyjeffrey/999386 to your computer and use it in GitHub Desktop.
Save madebyjeffrey/999386 to your computer and use it in GitHub Desktop.
Drawing a PDF
- (void) awakeFromNib {
self.opaque = YES;
self.clearsContextBeforeDrawing = YES;
[self setNeedsDisplay];
NSBundle *main = [NSBundle mainBundle];
// Open PDF
CGPDFDocumentRef doc = CGPDFDocumentCreateWithURL((CFURLRef)[main URLForResource: @"NC.pdf" withExtension: nil]);
if (doc == NULL)
@throw @"Resource does not exist.";
// Get First Page
CGPDFPageRef page = CGPDFDocumentGetPage(doc, 1);
if (page == NULL)
@throw @"Resource does not contain assets.";
CGPDFPageRetain(page);
// Get Page Size
CGRect pagesize = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
// Start Drawing Context to Render PDF
UIGraphicsBeginImageContext(pagesize.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(context, pagesize);
CGContextSaveGState(context);
// Flip Context to render PDF correctly
CGContextTranslateCTM(context, 0.0, pagesize.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Scale PDF - ignored at this point (repeat last command with size
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
self.test_NC = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// CGContextRelease(context);
// CGPDFPageRelease(page);
// CGPDFDocumentRelease(doc);
CALayer *image = [CALayer layer];
image.contents = (id)self.test_NC.CGImage;
image.frame = CGRectMake(20, 20, 100, 100);
image.hidden = NO;
[self.layer addSublayer: image];
[image retain];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment