mtacki
mtacki

Reputation: 27

swift scroll position of pdf.document

I have a pdf document inside a pdfView using PDFKit

    var urldocs = getDocumentsDirectoryURL()

    urldocs.appendPathComponent("test.pdf")


    let pdfDocument = PDFDocument(url: urldocs)

    pdfView.displayMode = .singlePageContinuous
    pdfView.autoScales = true

    pdfView.document = pdfDocument

How can I set the pdfView to show a specific area of the embedded document !? In a ScrollView, one could go ScrollView.contentOffset = CGPoint... I can not figure out how to do this in a pdfView

Thanks !

Upvotes: 1

Views: 3482

Answers (2)

PaulMest
PaulMest

Reputation: 15075

This will go to the 100x100 sized region at 10, 10 on page 2. Since it is a 0-based index, Page 2 is represented as index 1.

pdfView.go(to:CGRect(x: 10, y: 10, width: 100, height: 100), on:pdfView.document.page(at: 1))

Upvotes: 1

Fabian
Fabian

Reputation: 5358

You can go to a selection you found via text search in the document:

let selections = pdfView.document!.findString("have a pdf document", withOptions: [NSString.CompareOptions.literal])
guard let newSelection = selections.first else {
    fatalError("selection not found.")
}

update(pdfView) {
    $0.setCurrentSelection(newSelection, animate: true)
    $0.scrollSelectionToVisible(nil)
}

Or you can go to a cgRect on a page:

let pdfPage = pdfView.document!.page(at: 0)!
pdfView.go(to: cgRect, on: pdfPage)

Upvotes: 0

Related Questions