PDF Viewing#

Present a Document View#

There are two fundamental ways of presenting a document to the screen. One way is to use the Default UI which includes a user interface. The alternative is to load the document into a dedicated view controller and provide your own Custom UI with delegate methods available for your document control.

Default UI#

The Default UI is an App Kit UI created by Artifex which includes a user-interface for typical document features and actions. It is presented at the top of the document view and accommodates for both tablet and phone layout.

The Default UI aims to deliver a handy way of allowing for document viewing & manipulation without the need to provide your own Custom UI.

Basic Usage#

Assuming that your view controller has a navigation controller then local documents in the iOS main bundle can be presented by pushing an instance of DefaultUIViewController from your dedicated UIViewController as follows:

import mupdfdk
import mupdf_default_ui
...

let documentPath:String = "sample-document.pdf"

DefaultUIViewController.viewController(path: documentPath) { vc in
    if let vc = vc {
        vc.modalPresentationStyle = .fullScreen
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

To load files via a defined URL use the following:

DefaultUIViewController.viewController(url: url) { vc in
    if let vc = vc {
        vc.modalPresentationStyle = .fullScreen
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

Note

Files outside of your App bundle will need to be opened using the advanced method.

Advanced usage#

Alternatively, an application developer can open a document with a session. This enables more control with regard to the file operations and settings for the document.

Opening a document within a session involves the following:

  1. Initialize the MuPDF library:

let mupdfdkLib:MuPDFDKLib = MuPDFDKLib.init(settings: ARDKSettings())
  1. Initialize your own FileState class (ensuring to set the correct file path within it):

let fileState:MyFileState = MyFileState()
  1. Initialize document settings with your required Configuration Options:

let docSettings:ARDKDocumentSettings = ARDKDocumentSettings()
docSettings.enableAll(true)
  1. Initialize the document session, ARDKDocSession, with your file state, MuPDF library and document settings instances, also set your signing delegate as required:

let session:ARDKDocSession = ARDKDocSession(fileState:fileState,
                                          ardkLib:self.mupdfdkLib,
                                      docSettings:docSettings)
session.signingDelegate = ARDKOpenSSLSigningDelegate()
  1. Instantiate the DefaultUIViewController with the session:

let vc:DefaultUIViewController = DefaultUIViewController.viewController(session: session)

The Back Button#

Your dedicated view controller, which pushes the instance of ARDKDocumentViewController, must include one bespoke method to enable a graceful exit back from the Default UI. This method is detailed as follows:

@IBAction func docCloseUnwindAction(_ sender: UIStoryboardSegue) {}

This method is triggered by the user pressing the ‘back’ button in the MuPDF UI.

Note

The implementation can be left empty, but this function must be present in the view controller that should be unwound back to - otherwise nothing will happen when the user taps the back button.

Configuration Options#

When using the Default UI an application developer can optionally set certain configurable features.

The available settings conform to the ARDKDocumentSettings protocol which contains the following key/value pairs:

Key Value type
contentDarkModeEnabled bool
editingEnabled bool
fullScreenModeEnabled bool
insertFromCameraEnabled bool
insertFromPhotosEnabled bool
openInEnabled bool
openUrlEnabled bool
pdfAnnotationsEnabled bool
pdfFormFillingAvailable bool
pdfFormFillingEnabled bool
pdfFormSigningEnabled bool
pdfRedactionAvailable bool
pdfRedactionEnabled bool
pdfSignatureFieldCreationEnabled bool
pdfSecureRedactionAvailable bool
pdfSecureRedactionEnabled bool
pdfFormESigningAvailable bool
pdfFormESigningEnabled bool
pdfFormDigitalSigningAvailable bool
pdfFormDigitalSigningEnabled bool
printingEnabled bool
saveAsEnabled bool
saveButtonEnabled bool
saveToButtonEnabled bool
securePrintingEnabled bool
shareEnabled bool
systemPasteboardEnabled bool

Note

The configuration options should be set before a document is opened

To use MuPDF with configuration options an application developer should use load documents using the document session method.

Custom UI#

Your dedicated view controller should implement the following protocols ARDKBasicDocViewDelegate & ARDKDocumentEventTarget. For more on these protocols see the Document API page.

import mupdfdk

class DocumentViewController: UIViewController,
                              ARDKBasicDocViewDelegate,
                              ARDKDocumentEventTarget

These protocols will allow your UIViewController subclass to respond to events whilst presenting & interacting with the document.

There are several ways your UIViewController subclass can act as a container for an instance of MuPDFDKBasicDocumentViewController, along with UI related views. A layout described via a storyboard is one option, or a programmatic approach, as follows:

let documentViewController:MuPDFDKBasicDocumentViewController =
MuPDFDKBasicDocumentViewController.init(forPath:documentPath)
documentViewController.delegate = self
documentViewController.session.doc.add(self)
self.addChild(documentViewController)
documentViewController.view.frame = self.view.bounds
self.view.addSubview(documentViewController.view)
documentViewController.didMove(toParent:self)

Going to a Page#

Once a document is loaded an application developer can view pages either by scrolling the document view or by using the App Kit API as follows:

// note: page number is zero-indexed, thus this would show page 8 of your document
documentViewController.showPage(7)

In the code sample above documentViewController refers to the instance of your MuPDFDKBasicDocumentViewController. Furthermore this API should only be called after the document has initially loaded and had it’s first render (see Document Listeners - Document completed).

Viewing Full-Screen#

In order to view a document in full-screen, it is up to the application developer to hide any UI which they have present and set the frame of the document view to fill the screen. Once in full-screen, you can use Tap selections to exit and return from the full-screen frame to any previous UI.

Document Page Viewer#

There is a drop-in UI available to enable a page viewer for your document which understands how to display a column of page thumbnails from information obtained from within the document session object. In order to use this UI, instantiate the ARDKPagesViewController class with the document session and add it to your UI as required.

let pagesVC = ARDKPagesViewController(session: session)
pagesVC.view.frame = CGRect
pagesVC.didMove(toParent: self)
pagesVC.delegate = self
pagesVC.selectPage(0)
self.addChild(pagesVC)
view.addSubview(pagesVC.view)

Note

The delegate protocol for ARDKPagesViewController is ARDKPageSelectorDelegate.


This software is provided AS-IS with no warranty, either express or implied. This software is distributed under license and may not be copied, modified or distributed except as expressly authorized under the terms of that license. Refer to licensing information at artifex.com or contact Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, CA 94129, USA, for further information.Discord logo