简体   繁体   中英

Get local PDF page height - iOS

I have a pdf stored locally in an iphone. I want to get the height of that pdf having the path. I've tried:

NSURL *pdfUrl = [NSURL fileURLWithPath:self.documentPath];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(CFBridgingRetain(pdfUrl));
float width = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(document, 0), kCGPDFMediaBox).size.width;
float height = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(document, 0), kCGPDFMediaBox).size.height;

But the document is null, and I don't know any other way of doing it.

Try with this, I hope it helps you :)

// fileName is the PDF path
NSData *pdfData = [NSData dataWithContentsOfFile: @"__PDF_PATH"];

// Convert NSData to CFDataRef
CFDataRef thePDFdata = (__bridge CFDataRef)pdfData;

// Create CGDataProviderRef object from data
CGDataProviderRef providerObj = CGDataProviderCreateWithCFData(thePDFdata);

// Create the PDF using provider object
CGPDFDocumentRef pdfDoc = CGPDFDocumentCreateWithProvider(providerObj);

// Get the first page of the PDF
CGPDFPageRef pdfPageRef = CGPDFDocumentGetPage(pdfDoc, 1);

// Get the rect object from page 1
CGRect pdfPageRect = CGPDFPageGetBoxRect(pdfPageRef, kCGPDFMediaBox);

float width = pdfPageRect.size.width;
float height = pdfPageRect.size.height;

NSLog(@"WIDTH: %f HEIGHT: %f", width, height);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM