Building onto this question, I have tried to delve into CGPDF to get PDF size data without loading it into memory.
This part of iOS is very poorly documented, and searching the web only gives me some bad results about writing to pdfs.. Here is what i've tried, this doesn't result in anything else than a EXC_BAD_ACCESS on the noted line.
NSURL *imageFileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"a1" ofType:@"pdf"]];
CGPDFDocumentRef pdfSource = CGPDFDocumentCreateWithURL((__bridge CFURLRef)imageFileURL);
CGPDFDictionaryRef pdfProps = CGPDFDocumentGetInfo(pdfSource);
CGPDFStringRef cfValue;
CGPDFDictionaryGetString(pdfProps, kCGPDFMediaBox, &cfValue); //Crash, EXC_BAD_ACCESS
NSString *value = CFBridgingRelease(CGPDFStringCopyTextString(cfValue));
CGRect rect = CGRectFromString(value);
NSLog(@"#rekt: %@",NSStringFromCGRect(rect));
Where am I stepping wrong here?
The CGPDFDocumentGetInfo()
dictionary does not contain information about the sizes because
all pages in a PDF document can have different dimensions (media box, crop box, ...).
The following shows how to get the information for the first page:
CGPDFDocumentRef doc = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfURL);
CGPDFPageRef page = CGPDFDocumentGetPage(doc, 1);
CGRect mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
NSLog(@"#mediaBox: %@",NSStringFromCGRect(mediaBox));
CGPDFDocumentRelease(doc);
For documents with multiple pages you would have to determine the maximum over all pages in the document.