【问题标题】:PDF size in a UIWebViewUIWebView 中的 PDF 大小
【发布时间】:2012-11-07 22:57:38
【问题描述】:

我只是想知道在 UIWebView 中加载 pdf 后是否可以知道它的大小? 当我们使用这样的代码时:

[self.siteWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.test.fr/test.pdf"]]];

【问题讨论】:

    标签: ios pdf uiwebview


    【解决方案1】:

    是的。绝对有可能在 uiwebview 中获得 pdf 的高度(1 页高度或任何内容)。我以前做过。下面的代码展示了如何在 uiwebview 中获得 1 个页面高度。

    -(void)getPDFInfo    
    {
    NSURL* pdfFileUrl = self.fileLocation;
    
    
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
    CGPDFPageRef page;
    
    // CGRect aRect = CGRectMake(0, 0, 100, 200); // thumbnail size
    
    
    NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
    numberOfPage= totalNum;
    for(int i = 0; i < totalNum; i++ ) {
        CGPDFPageRef myPageRef=CGPDFDocumentGetPage(pdf, i+1);
        //aRect=CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
    
        CGRect cropBox = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
        pdfWidth= cropBox.size.width;
        pdfHeight=cropBox.size.height;
    
        NSLog(@"cropbox size is %@",NSStringFromCGRect(cropBox));
    
        int pageRotation = CGPDFPageGetRotationAngle(myPageRef);
    
        CGSize pageVisibleSize = CGSizeMake(cropBox.size.width, cropBox.size.height);
        if ((pageRotation == 90) || (pageRotation == 270) ||(pageRotation == -90)) {
            pageVisibleSize = CGSizeMake(cropBox.size.height, cropBox.size.width);
        }
    
    
    
    }
    }
    

    调用此函数后,调用此行以获取 uiwebview 中的 pdf 文件大小。

    documentHeightInWebview = pdfHeight * (documentWidthInWebview/pdfWidth);
    

    其中 documentWidthInWebview 是您的真实 uiwebview 宽度,它将显示您的 pdf 文件。

    【讨论】:

      【解决方案2】:

      您可以使用 javascript 来获取信息。把它放在 webview 中确实加载了。比如下面会获取页面的高度。

      -(void) webViewDidFinishLoad:(UIWebView *)webView {
      
      //get the height of the chunk, and store it.
      CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.height;"] floatValue];
      

      【讨论】:

      • 您是否在 Web 视图完成加载后通过评估来调用字符串。在有内容之前它将为 0。不过,我从未尝试过仅使用 pdf 格式。
      • 我在 webViewDidFinishLoad 方法中执行了这段代码,所以我认为 web 视图已经完全加载了,不是吗?
      • 是的,应该在那个阶段。我不是 100% 确定你怎么能做到这一点。我唯一的其他建议是将pdf下载到设备,将其存储在本地,使用核心图形方法获取文档的高度(获取第一页,获取页面高度等),然后在您的网络视图。
      猜你喜欢
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      相关资源
      最近更新 更多