5

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *urlremoto = [NSURL URLWithString:@"https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/TransitionGuide.pdf"];

    request = [NSURLRequest requestWithURL:urlremoto];

    [self.webView loadRequest:request];
    [self.webView setScalesPageToFit:YES];
}

-(void)webViewDidStartLoad:(UIWebView *)webView{

    [self.activityIndicator startAnimating];
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    //TODO: describir el error
    [self.activityIndicator stopAnimating];        
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    [self.activityIndicator stopAnimating];
}

Now I want to touch a button and change my loaded PDF to certain page, I'm looking for something to achieve this but still got nothing working

thanks in advance for the support

Jesús Ayala
  • 2,743
  • 3
  • 32
  • 48
  • possible duplicate of [iPhone UIWebView PDF Page Jump](http://stackoverflow.com/questions/1974304/iphone-uiwebview-pdf-page-jump) – bummi Feb 19 '15 at 08:50

5 Answers5

4

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


    }
2

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.

RFG
  • 2,880
  • 3
  • 28
  • 44
  • I tried with your line in UIWebViewDelegate method: -(void)webViewDidFinishLoad:(UIWebView *)webView; this but didn't work like this: [[self.webView scrollView] setContentOffset:CGPointMake(0,-800) animated:YES]; – Jesús Ayala Oct 07 '13 at 22:48
  • it worked!!! thanks for sharing your expertise =D final question, is there a way to get the pageHeight dinamically that could be great for building a method, I'll search that anyways but if you know that'd be great – Jesús Ayala Oct 08 '13 at 15:58
  • 1
    I've found this link that coulb be useful for your purposes: http://stackoverflow.com/questions/3045587/how-to-get-actual-pdf-page-size-in-ipad – RFG Oct 08 '13 at 17:23
  • Great code snippet! But how do you get the PDF document's page height? – AlexR Jan 02 '14 at 18:22
  • it didn't work for me. i loaded the request then scrolled, then add to superview. [webView loadRequest:request]; [[webView scrollView] setContentOffset:CGPointMake(0,200) animated:YES]; [self.view addSubview:webView]; – iosMentalist Apr 01 '14 at 13:32
1

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.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
1

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];
shutup
  • 241
  • 1
  • 7
-1

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)
M. Black
  • 353
  • 1
  • 4
  • 20
  • The tricky part of the question is finding out the correct value for pageHeight, not the how-to-scroll part. – Baxissimo Dec 11 '18 at 02:38