New: Create and edit Word documents with DOCX Editor in WebViewer

How to Add a PDF & MS Office Viewer to a Mac Catalyst App

By Jamie Dassoulas | 2023 Jun 07

Sanity Image
Read time

4 min

In this article, we describe how to use the Apryse 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.

mac catalyst viewer with menus

Catalyst

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 Apryse SDK to even more customers using this exciting technology. The Apryse 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).

Getting Started with Our Catalyst PDF SDK

Integrating the Apryse 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, download the SDK to try it out.

The Apryse 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 by 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!

mac catalyst integration in project folder

Adding PDFNet and Tools XCFrameworks to your project.

Display PDF & MS Office documents in your Catalyst app

The Apryse 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!

mac catalyst PDF Viewer

Viewing a document in your Catalyst app.

Adding a Viewer Toolbar and Mac Menus

So far we've integrated the Apryse 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
}

Note: Don't forget to set your view controller to be the PTDocumentViewController's delegate object!
documentController.delegate = self

mac catalyst PDF viewer toolbar

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

The Apryse 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.

Conclusion

In this article, we showed how to add a PDF and MS Office Document viewer to a Catalyst app with Apryse 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 Apryse into your project, please feel free to contact us, and we'll be more than happy to help!

Sanity Image

Jamie Dassoulas

Share this post

email
linkedIn
twitter