display specific pdf page in the UIWebview ios

孤街浪徒 提交于 2019-12-01 10:03:44

问题


I am currently working on a project and I have ios need to display a pdf file. However i want choose the page to display. For example see page 10 of 37 in a UIWebView. I have not found a way to cleanly separate the pages of a pfd.

thank you for your help.


回答1:


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,




回答2:


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 .....



来源:https://stackoverflow.com/questions/19154484/display-specific-pdf-page-in-the-uiwebview-ios

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