Document Interactions


After loading a PDF document you can programmatically interact with it allowing you to scroll to different pages or zoom in and out of a document. All of the interaction APIs are available on the PSPDFViewController which is the core PDF viewer.

Changing the current page

While the user can manually change the current page by doing a swipe gesture on the device you can also programmatically change pages using setPageIndex: and setPageIndex:animated:. Using pageIndex you can fetch the current page.

Copy
1
2
3
4
5
6
7
8
9
10
11
let startPage = pdfController.pageIndex

// Change to page thirteen (zero based page numbers).
pdfController.pageIndex = 12

// Go to the last page, using a transitioning animation.
let lastPage = document.pageCount - 1
pdfController.setPageIndex(lastPage, animated: true)

// Go back to the page where we started off.
pdfController.pageIndex = startPage
Copy
1
2
3
4
5
6
7
8
9
10
11
NSUInteger startPage = pdfController.pageIndex;

// Change to page thirteen (zero based page numbers).
pdfController.pageIndex = 12;

// Go to the last page, using a transitioning animation.
NSUInteger lastPage = document.pageCount - 1;
[pdfController setPageIndex:lastPage animated:YES];

// Go back to the page where we started off.
pdfController.pageIndex = startPage;

Zooming into the page

You can programmatically zoom into the current page using setZoomScale:animated: which zooms to a specific scale. Furthermore, you can use zoomToRect:pageIndex:animated: to zoom onto a CGRect of any page (e.g. zoom onto a sentence, or annotation).

For more information on zooming have a look at our zooming guide.

Copy
1
2
3
4
5
let annotationPosition = annotation.boundingBox
let page = annotation.absolutePageIndex

// Zoom to the annotation.
pdfController.zoom(to: annotationPosition, pageIndex: page, animated: true)
Copy
1
2
3
4
5
CGRect annotationPosition = annotation.boundingBox;
NSUInteger page = annotation.absolutePageIndex;

// Zoom to the annotation.
[pdfViewController zoomToRect:annotationPosition pageIndex:page animated:YES];

Tip: zoomToRect:pageIndex:animated: will automatically go to the given page if some other page is currently viewed. No need to switch the page manually!

Was this page helpful? We're happy to answer any questions.