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

假装没事ソ 提交于 2019-11-30 07:49:43

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.

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)
        }


    }

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.

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

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