簡體   English   中英

在UIWebview ios中顯示特定的pdf頁面

[英]display specific pdf page in the UIWebview ios

我目前正在研究一個項目,我要讓ios顯示一個pdf文件。 但是我想選擇要顯示的頁面。 例如,請參見UIWebView中的第10頁,共37頁。 我還沒有找到一種方法來干凈地分隔pfd的頁面。

謝謝您的幫助。

使用UIWebView's delegate方法可以做到這一點:

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

享受編碼.....

您可以使用webview的setContentOffset屬性顯示該頁面,

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

其中pageheight =您的頁面高度,10是您的頁面編號,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM