Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

pdf

uiwebview

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

like image 938
Jesús Ayala Avatar asked Oct 07 '13 17:10

Jesús Ayala


3 Answers

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


    }
like image 186
Dharmendra Chaudhary Avatar answered Nov 14 '22 23:11

Dharmendra Chaudhary


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.

like image 24
RFG Avatar answered Nov 14 '22 23:11

RFG


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.

like image 32
sergio Avatar answered Nov 15 '22 00:11

sergio