2012-11-07 51 views
2

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

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

回答

0

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

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

//get the height of the chunk, and store it. 
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.height;"] floatValue]; 
+0

它始终返回0:/ – seb

+0

您是否在Web视图完成加载后通过评估来调用字符串。在有内容之前它将为0。我从来没有尝试过,只是一个PDF虽然。 – Bergasms

+0

我在webViewDidFinishLoad方法中执行这段代码,所以我认为web视图是完整的,不是吗? – seb

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文件。