Document Interactions

After loading a PDF document, you can programmatically interact with it, which allows you to scroll to different pages or zoom in and out. 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:. You can fetch the current page using pageIndex:

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

// Change to page 13 (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 you started off.
pdfController.pageIndex = startPage
Copy
1
2
3
4
5
6
7
8
9
10
11
NSUInteger startPage = pdfController.pageIndex;

// Change to page 13 (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 In To the Page

You can zoom in to a page by calling zoomToPDFRect:forPageAtIndex:animated: on PSPDFDocumentViewController. The method will make sure to switch to the correct page if necessary before applying the zoom.

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

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

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

// Zoom to the annotation.
[documentViewController zoomToPDFRect:annotationPosition forPageAtIndex:pageIndex animated:YES];

Tip: zoomToPDFRect:forPageAtIndex:animated: will automatically go to the given page if some other page is currently being viewed. There is no need to switch the page manually!