Roval
Roval

Reputation: 597

Open PDF file on certain page iOS

Is it possible to open PDF file on certain page with UIDocumentInteractionController?

I'm trying to append #page=3 at the end of the file address string but it is not working when I try to do:

NSString* pdf = [fileName stringByAppendingString:@"#page=3"];

NSURL *URL = [[NSURL alloc] initFileURLWithPath:pdf];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];

Maybe there is another way to do this?

Upvotes: 2

Views: 2283

Answers (2)

Roval
Roval

Reputation: 597

Now, with Swift 4 and PDFKit we can do the following:

var pdfView: PDFView!

override func viewDidLoad() {
    super.viewDidLoad()

    pdfView = PDFView()
    // You might want to constraint pdfView here

    // Load pdf from bundle or url
    let url = Bundle.main.url(forResource: "test", withExtension: "pdf")!
    let document = PDFDocument(url: url)
    pdfView.document = document

    // Grab 10th page, returns optional if out of bounds
    if let page10 = document?.page(at: 10) {
        pdfView.go(to: page10)
    }

}

Upvotes: 7

terrafirma9
terrafirma9

Reputation: 362

It doesn't look possible to do this. Don't see any hooks into the document interaction controller's scrollview, so you can't change the offset programmatically. You probably need to:

  1. Show the PDF on your own, in a UIWebView or something, so you can control the offset
  2. Show a toolbar above it with filename and an "open-in" button

Basically make your own preview.

Upvotes: 0

Related Questions