display specific pdf page in the UIWebview ios

坚强是说给别人听的谎言 提交于 2019-12-01 12:39:55

You can use setContentOffset property of webview to show that page,

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

where pageheight=your page height, 10 is your page no,

Use UIWebView's delegate method to do this:

- (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];
   }
}

Now add below method to traverse to your page. Note need valid PDF file path and provide your valid specific page no you want traverse in PDF file.

-(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];
   }
}

For calculation of total PDF pages

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

Enjoy coding .....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!