Hello, I am trying to draw a pdf page onto a pdf context and then save it to disk. I cannot seem to figure out what is wrong. Can someone give me a few pointers. Thanks.
- (void)testQuartz:(NSData *)pdfDocumentData
{
//Create the pdf document reference
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
CGDataProviderRelease(dataProvider); //Release the data provider
//Create the pdf context
CGPDFPageRef page = CGPDFDocumentGetPage(document, 0);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);
if (CGPDFDocumentGetNumberOfPages(document) > 0)
{
//Draw the page onto the new context
page = CGPDFDocumentGetPage(document, 0);
CGContextDrawPDFPage(pdfContext, page);
}
else
{
NSLog(@"Failed to create the document");
}
//Write to disk
[(NSMutableData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES];
//Clean up
CGDataConsumerRelease(dataConsumer);
CFRelease(mutableData);
CGPDFDocumentRelease(document);
}