UIWebView中的PDF大小

时间:2012-11-07 22:57:39

标签: ios pdf uiwebview

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

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

2 个答案:

答案 0 :(得分: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文件。

答案 1 :(得分:0)

您可以使用javascript获取信息。把它放在webview中加载。例如,以下内容将获得页面的高度。

-(void) webViewDidFinishLoad:(UIWebView *)webView {

//get the height of the chunk, and store it.
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.height;"] floatValue];
相关问题