获取本地PDF页面高度 - iOS

时间:2018-02-28 11:18:14

标签: ios objective-c pdf

我在iPhone上本地存储了一个pdf。我想得到那个有路径的pdf的高度。我试过了:

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;

但该文件无效,我也不知道其他任何方式。

1 个答案:

答案 0 :(得分:1)

试试这个,我希望它可以帮助你:)

// 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);