Skip to content

Instantly share code, notes, and snippets.

@andrey-str
Created October 12, 2015 23:15
Show Gist options
  • Save andrey-str/efb44485acba959a8b99 to your computer and use it in GitHub Desktop.
Save andrey-str/efb44485acba959a8b99 to your computer and use it in GitHub Desktop.
Load and render a page from PDF with QuartzCore
#import "PDFDocument.h"
bool PDFDocument::loadDocument(const char *filename){
CFStringRef path;
CFURLRef url;
CGPDFDocumentRef document;
size_t count;
path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path, // 1 kCFURLPOSIXPathStyle, 0);
CFRelease (path);
document = CGPDFDocumentCreateWithURL (url);// 2
CFRelease(url);
count = CGPDFDocumentGetNumberOfPages (document);// 3
if (count == 0) {
printf("`%s' needs at least one page!", filename);
return false;
}
mDocument = document;
return true
}
void PDFDocument::drawPage(CGContextRef myContext, size_t pageNumber) {
assert(mDocument);
CGPDFPageRef page;
page = CGPDFDocumentGetPage (mDocument, pageNumber);
CGContextDrawPDFPage (myContext, page);
}
#pragma once
#import <QuartzCore/QuartzCore.h>
class PDFDocument{
public:
bool loadDocument(const char *filename);
void drawPage(CGContextRef myContext, size_t pageNumber);
private:
CGPDFDocumentRef mDocument;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment