Search code examples
iosswiftpdfzoomingpdfkit

Zoom in to specific coordinates in PDFKit using swift


I am using pdfkit to open pdf file in my app.

I want to zoom in to specific coordinates.

Let say Following is my pdf page and I want to zoom in to specific coordinates when user will click on pencil button.

enter image description here

After clicking on pencil button it should be look like following image.

enter image description here

I've searched a lot but couldn't find any proper solution for that. I don't want to use third party libraries to do that.


Solution

  • I solved this problem by using following code.

    let documents = pdfView.document
        if let page = documents?.page(at: 0) {
            pdfView.scaleFactor = pdfView.scaleFactorForSizeToFit * 2
            pdfView.go(to: CGRect(x: 0, y: 0, width: 300, height: 300), on: page)
        }
    

    This is the zoom in code to specific coordinates.

    There is one more thing that needs attention. You need to turn off pagination pdfView.usePageViewController(false).