【问题标题】:Get local PDF page height - iOS获取本地 PDF 页面高度 - iOS
【发布时间】:2018-02-28 11:18:13
【问题描述】:

我有一个 pdf 本地存储在 iPhone 中。我想获得具有路径的 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;

但是文档是空的,我不知道有什么其他的方法。

【问题讨论】:

    标签: ios objective-c pdf


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-25
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多