PDFTron is now Apryse. Same great products, new name.

How to Add a PDF Viewer to Your App Using Swift and PDFKit

By David Luco | 2018 Oct 24

Sanity Image
Read time

3 min

In this article, we describe how to add a PDF viewer to your iOS app using a Swift PDF library with PDFKit, Apple's built-in PDF SDK introduced in iOS 11.

PDFKit uses an app to make PDFs. The PDFKit framework has been available in AppKit since macOS 10.4 but was a private framework on iOS used in the iBooks and Mail apps. However, the Core Graphics framework for iOS provides basic PDF handling without any UI. Using the Core Graphics PDF API directly to create a PDF viewer requires a lot of work, so knowing how to select a PDF file in iOS Swift saves a large amount of time.

Step 1 - Import the Module

In your iOS Swift PDF framework project where you want to display a PDF, usually in a UIViewController subclass, import the PDFKit module at the top of the Swift file:

import PDFKit

This will make the PDFKit framework available for use in the Swift file.

Step 2 - Implement the Swift PDF Viewer

To generate or display a PDF document, a PDFView is created and added to the view controller's view hierarchy. In the following sample, the UIViewController.viewDidLoad() method is used to set up the PDFView and load a PDF document from the app bundle:

override func viewDidLoad() {

    // ...

    // Add PDFView to view controller.
    let pdfView = PDFView(frame: self.view.bounds)
    pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    // Fit content in PDFView.
    pdfView.autoScales = true
    // Load Sample.pdf file from app bundle.
    let fileURL = Bundle.main.url(forResource: "Sample", withExtension: "pdf")
    pdfView.document = PDFDocument(url: fileURL!)

In the sample above, the PDFView is set to fill the entire view controller and automatically scale its content to fit the available space. The "Sample.pdf" file is loaded from the app's bundled resources, but it is also possible to use other URL sources or a Data object.

PDFKit Viewer

PDFKit viewer.

As you can see from the preview, the PDF document is displayed but there are no controls to add annotations, search for text, etc.

Apple's PDFKit framework is an easy way to add a basic PDF viewer, but if you need more robust functionality, like annotation creation and editing, real-time collaboration, form filling, text search, page manipulation, or others, you would have to implement those features yourself. Also beware that PDFKit's rendering engine only works well if your documents are simple and you can afford to have some rendering inconsistencies or failures.

For more robust functionality and reliable rendering, Apryse has an iOS PDF SDK that includes some of the following features:

Apryse for iOS not only supports PDF documents but also other formats including .docx, .doc, .pptx, .xlsx, Markdown, and various image formats.

Adding a PDF Viewer with Apryse

To show a PDF with our iOS PDF Library, simply select an integration or framework to get your iOS application started.

The Apryse PTDocumentViewController is a full-featured PDF viewer that can be added to your app with a few lines of code. In the following sample, a PTDocumentViewController is shown from a UIViewController subclass's viewDidAppear(_:) method:

override func viewDidAppear(_ animated: Bool) {
    // ...
    // Create PTDocumentViewController.
    let documentController = PTDocumentViewController()
    // Load Sample.pdf file from app bundle.
    let fileURL = Bundle.main.url(forResource: "Sample", withExtension: "pdf")

    documentController.openDocument(with: fileURL!)

    // Show PTDocumentViewController in UINavigationController.
    let navigationController = UINavigationController(rootViewController: documentController)
    self.present(navigationController, animated: true, completion: nil)

In the sample above, a PTDocumentViewController is created and added to a UINavigationController that supplies the navigation bar. The sample document is loaded from the app bundle, but the PTDocumentViewController class can also open files from other locations.

PDFKit Viewer

Apryse viewer

View the API documentation for the PTDocumentViewController class.


Using Apple's PDFKit framework to add a Swift PDF reader to your app is simple, but if you want more advanced features and need rock-solid rendering and support, then PDFKit's simplicity becomes a liability. Apryse has hundreds of features, supports 30+ file formats, has a proven built-in rendering engine, and is just as simple to integrate.

You can view and download the full source code for adding a Swift PDF viewer.

If you have any questions about integrating Apryse into your project, please feel free to contact us and we'll be more than happy to help!

Sanity Image

David Luco

Share this post