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
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)
}
}
At this moment I think you can use two approaches:
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];
Split PDF individual pages.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With