【问题标题】:display specific pdf page in the UIWebview ios在 UIWebview ios 中显示特定的 pdf 页面
【发布时间】:2013-10-09 20:57:56
【问题描述】:

我目前正在做一个项目,我有 ios 需要显示一个 pdf 文件。 但是我想选择要显示的页面。 例如,请参阅 UIWebView 中的第 10 页,共 37 页。 我还没有找到一种方法来干净地分离 pfd 的页面。

感谢您的帮助。

【问题讨论】:

标签: ios objective-c file pdf uiwebview


【解决方案1】:

使用UIWebView'sdelegate方法来做到这一点:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   //Check if file still loading
   if(!webView.isLoading)
   { 
     //now traverse to specific page
     [self performSelector:@selector(traverseInWebViewWithPage) withObject:nil afterDelay:0.1];
   }
}

现在添加以下方法以遍历您的页面。 注意需要有效的 PDF 文件路径并提供您想要在 PDF 文件中遍历的有效特定页面。

-(void)traverseInWebViewWithPage
{
   //Get total pages in PDF File ----------- PDF File name here ---------------
   NSString *strPDFFilePath = [[NSBundle mainBundle] pathForResource:@"yourPDFFileNameHere" ofType:@"pdf"];
   NSInteger totalPDFPages = [self getTotalPDFPages:strPDFFilePath];

   //Get total PDF pages height in webView
   CGFloat totalPDFHeight = yourWebViewPDF.scrollView.contentSize.height;
   NSLog ( @"total pdf height: %f", totalPDFHeight);

   //Calculate page height of single PDF page in webView
   NSInteger horizontalPaddingBetweenPages = 10*(totalPDFPages+1);
   CGFloat pageHeight = (totalPDFHeight-horizontalPaddingBetweenPages)/(CGFloat)totalPDFPages;
   NSLog ( @"pdf page height: %f", pageHeight);

   //scroll to specific page --------------- here your page number -----------
   NSInteger specificPageNo = 2;
   if(specificPageNo <= totalPDFPages)
   {
      //calculate offset point in webView
      CGPoint offsetPoint = CGPointMake(0, (10*(specificPageNo-1))+(pageHeight*(specificPageNo-1)));
      //set offset in webView
      [yourWebViewPDF.scrollView setContentOffset:offsetPoint];
   }
}

用于计算 PDF 总页数

-(NSInteger)getTotalPDFPages:(NSString *)strPDFFilePath
{
   NSURL *pdfUrl = [NSURL fileURLWithPath:strPDFFilePath];
   CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
   size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
   return pageCount;
}

享受编码.....

【讨论】:

    【解决方案2】:

    您可以使用 webview 的 setContentOffset 属性来显示该页面,

    [[webView scrollView] setContentOffset:CGPointMake(0,10*pageheight) animated:YES];
    

    其中pageheight=你的页面高度,10是你的页面编号,

    【讨论】:

    • 是的,这种方法效果很好,但我有其他页面的边框,我可以滚动。谢谢你的回答。
    • @Viruss mca:你从哪里得到“页面高度”?
    • @AlexR: CGFloat pageHeight = webView.scrollView.contentSize.height;NSLog ( @"Client height: %@", [webview stringByEvaluatingJavaScriptFromString: @"document.body.clientHeight"] );
    • @Virussmca, [webview stringByEvaluatingJavaScriptFromString: @"document.body.clientHeight"] 这对我不起作用
    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 2016-08-08
    • 2011-12-12
    • 2020-10-23
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多