How to control page display to PDF loaded in UIWebView for iOS (go to page)

前端 未结 5 828

I\'m developing an app which displays a PDF embedded in a WebView this is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *urlremoto = [NS         


        
相关标签:
5条回答
  • 2020-12-30 16:16

    just as RFG said,using webview's scrollview is perfect,and the key step is to find out the height of pdf page,the follow code will get the whole height of pdf file and page num of pdf file .and then we got it!

    First Way:

    //get the total height
    CGFloat pageHeight = self.webView.scrollView.contentSize.height;
    //get page nums
    -(NSInteger)getTotalPDFPages:(NSString *)strPDFFilePath
    {
        NSURL *pdfUrl = [NSURL fileURLWithPath:strPDFFilePath];
        CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
        size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
        return pageCount;
    }
    

    Second Way:

    //get the content info 
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        [self.okBtn setEnabled:YES];
        [self.signPdfBtn setEnabled:YES];
        NSArray* a = [self.webView.scrollView subviews];
        for (UIView* view in a) {
            if ([view isKindOfClass:NSClassFromString(@"UIWebPDFView")]) {
                NSArray* b = view.subviews;
                self.content_hetght = view.bounds.size.height;
                self.content_num = b.count;
                }
        }
    }
    //then you can jump any page,like the last page
    [self.webView.scrollView setContentOffset:CGPointMake(0, (self.content_num-1)*(self.content_hetght*1.0 /self.content_num)) animated:YES];
    
    0 讨论(0)
  • 2020-12-30 16:21

    At this moment I think you can use two approaches:

    1. Use scrollView to 'navigate, through PDF:

      [[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES];

      // For example, jumping to page 5 in a PDF document with 1000 px page height:

      int selectedPag = 5; // i.e. Go to page 5
      
      float pageHeight = 1000.0; // i.e. Height of PDF page = 1000 px;
      
      float y = pageHeight * selectedPag;
      
      [[webView scrollView] setContentOffset:CGPointMake(0,y) animated:YES];
      
    2. Split PDF individual pages.

    0 讨论(0)
  • 2020-12-30 16:21

    Its late but may be helpful,

    For iOS less than 11.0

    func webViewDidFinishLoad(_ webView: UIWebView)
        {
    
            var pdfPageHeight:CGFloat = 0
    
            let a = webView.scrollView.subviews
    
            for var view in a
            {
                if view.isKind(of: NSClassFromString("UIWebPDFView")!)
                {
                    let b = view.subviews
    
                    for var iview in b
                    {
                        if iview.isKind(of: NSClassFromString("UIPDFPageView")!)
                        {
                            pdfPageHeight = iview.bounds.size.height
    
                            break
                        }
                    }
                }
            }
    
            webView.scrollView.setContentOffset(CGPoint(x: 0, y: CGFloat((pagenumber - 1)   + 1 )*pdfPageHeight), animated: true)
    
        }
    

    For iOS 11 and greater

    var pdfdocumentURL: URL?
    
        @IBOutlet weak var titleLbl: UILabel!
        @IBOutlet weak var pdfview: PDFView!
        var pagenumber = 1
    
    
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
            self.titleLbl.text = titleStr
    
           let pdfdocument = PDFDocument(url: pdfdocumentURL!)
    
            pdfview.document = pdfdocument
            pdfview.displayMode = PDFDisplayMode.singlePageContinuous
            pdfview.autoScales = true
    
            pagenumber = pagenumber - 1
            if let page = pdfdocument?.page(at: pagenumber) {
                pdfview.go(to: page)
            }
    
    
        }
    
    0 讨论(0)
  • 2020-12-30 16:25

    As far as I know, Safari Kit does not support named destinations (RFC 3778). In other words, if you try this:

    <a href="http://www.domain.com/file.pdf#page=3">Link text</a>
    

    in Safari, it will not work.

    The only chance for you to jump to a PDF page, as far as I can see, is using a framework like Reader, or other equivalent.

    0 讨论(0)
  • 2020-12-30 16:34

    Swift 3 version:

        let selectedPag: CGFloat = 5; // i.e. Go to page 5
    
        let pageHeight: CGFloat = 1000.0; // i.e. Height of PDF page = 1000 px;
    
        let y: CGFloat = pageHeight * selectedPag;
    
        let pageOffset = CGPoint(x: 0, y: y)
        WebViewMore.scrollView.setContentOffset(pageOffset, animated: true)
    
    0 讨论(0)
提交回复
热议问题