In this article we describe how to use the PDFTron Mac Catalyst PDF SDK for adding a PDF and MS Office document viewer to your iOS and Mac app using Mac Catalyst, Apple's cross-platform technology announced at WWDC 2019.

Catalyst viewer with menus

linkCatalyst

Apple introduced Catalyst as an easy and convenient way to develop cross-platform apps for iOS and macOS, and we're pleased to bring the PDFTron SDK to even more customers using this exciting technology. The PDFTron SDK for Catalyst provides all the capabilities of our powerful iOS SDK with some additional features for a native Mac experience, including support for DOCX, XLSX, PPTX, DOC, XLS, and PPT (no MS Office licenses or software required).

linkGetting started with our Catalyst PDF SDK

Integrating the PDFTron SDK for a Catalyst project is just as easy as integrating our iOS SDK with some small differences, notably with the use of XCFrameworks to package the library. Apple introduced XCFrameworks alongside Catalyst at WWDC 2019 as a new way to bundle libraries for various different target platforms and architectures.

To get started, please download the SDK to try it out.

The PDFTron Catalyst SDK includes XCFrameworks for the PDFNet and Tools modules and it's easy to add them to your Catalyst project. You can find them in the downloaded package here:

Lib/Framework-dynamic/PDFNet.xcframework

Lib/Tools/Tools.xcframework

To integrate these into your project, you can simply add them to your Xcode project following these steps:

  1. Select your project in the left pane
  2. Select your target and go to the General tab
  3. Scroll down to the Frameworks, Libraries, and Embedded Content section and add the XCFrameworks by dragging them from the Finder window. You can also click + and select Add Other… from the dialog which appears.

And that's it!

Catalyst integration

Adding PDFNet and Tools XCFrameworks to your project.

linkDisplay PDF & MS Office documents in your Catalyst app

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

var documentViewController: PTDocumentViewController?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // Load the Getting Started.pdf file from app bundle.
        let fileURL = Bundle.main.url(forResource: "Getting Started", withExtension: "pdf")
        
        // Initialize the PTDocumentViewController
        documentViewController = PTDocumentViewController()
        guard let documentViewController = documentViewController else {
            return
        }
        documentController.openDocument(with: fileURL!)
        
        // Show PTDocumentViewController in UINavigationController.
        let navigationController = UINavigationController(rootViewController: documentController)
        navigationController.modalPresentationStyle = .fullScreen
        self.present(navigationController, animated: true, completion: nil)
    }

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

Now you have a document viewer in your Catalyst app!

Catalyst viewer

Viewing a document in your Catalyst app.

linkAdding a Viewer Toolbar and Mac Menus

So far we've integrated the PDFTron SDK and displayed a PDF viewer in the app but at this point it still just looks like an iOS app running on a Mac. There are a few things we can do to make the app feel more like a native macOS app, starting with a toolbar at the top of the window. The PTDocumentViewController in our Catalyst SDK provides a ready-to-use Mac toolbar which we can add to our app. The toolbar is associated to the PTDocumentViewController so we recommend having your view controller conform to the PTDocumentViewControllerDelegate protocol and show the toolbar when the documentViewControllerDidOpenDocument: delegate method is called:

func documentViewControllerDidOpenDocument(_ documentViewController: PTDocumentViewController) {
    guard let window = documentViewController.view.window,
          let windowScene = window.windowScene,
          let titlebar = windowScene.titlebar else {return}
    titlebar.toolbar = documentViewController.macToolbar
}
PTDocumentViewControllerDelegate
Don't forget to set your view controller to be the PTDocumentViewController's delegate object!
documentController.delegate = self

Catalyst viewer with toolbar

Above: Using the pre-built Mac toolbar available in the SDK.

The PDFTron SDK for Mac Catalyst also comes with some convenient prebuilt action menus that you can easily integrate into your app. These provide easy and quick access to many common actions such as navigating and annotating documents, as well as changing the view mode.

These can be added using the UIMenuBuilder protocol in UIKit. To use these menus you should ensure that you can access your view controller's PTDocumentViewController instance from your AppDelegate class, where you can override the buildMenuWithBuilder: function and customize the app's menus.

override func buildMenu(with builder: UIMenuBuilder) {
    guard builder.system == .main else { return }

    // access the PTDocumentViewController instance
    guard let viewController:ViewController = UIApplication.shared.windows.first?.rootViewController as? ViewController,
          let documentViewController = viewController.documentViewController
          else {return}

    builder.replace(menu: .standardEdit, with: documentViewController.editMenu)
    builder.insertChild(documentViewController.viewModesMenu, atStartOfMenu: .view)
    builder.insertChild(documentViewController.navigationListsMenu, atStartOfMenu: .view)
    builder.insertChild(documentViewController.additionalViewMenu, atStartOfMenu: .view)
    builder.insertSibling(documentViewController.annotateMenu, afterMenu: .view)
    builder.insertSibling(documentViewController.navigateDocMenu, afterMenu: .view)
}

A table of available menus and their actions, as well as more information on the SDK is available in our Catalyst guide.

The full sample project for this article is available here.

For an even more fully-featured sample app, please see the MacReader sample app in the SDK download.

linkConclusion

In this article we showed how to add a PDF and MS Office Document viewer to a Catalyst app with PDFTron and add useful features like a toolbar and menu bars for an even better experience on macOS. We're excited by the potential of Catalyst and look forward to bringing our powerful PDF SDK to even more users with this technology.

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