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 PDFViewController, which is the core PDF viewer.

Changing the Current Page

A user can manually change the current page by swiping on a device. However, you can programmatically change pages using pageIndex and setPageIndex(_:animated:). You can also use pageIndex to fetch the current page:

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.
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 you started.
pdfController.pageIndex = startPage;

Zooming In To the Page

You can zoom in to a page by calling zoom(toPDFRect:forPageAt:animated:) on PDFDocumentViewController. 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: zoom(toPDFRect:forPageAt:animated:) will automatically go to the given page if some other page is currently being viewed. There’s no need to switch the page manually!