Some test text!

menu
search

Open a document

You have a few options to open a document such as with a document view controller, tabbed document view controller or an embedded view.

Show documents in a tabbed ViewController

The PTTabbedDocumentViewController class is a container view controller that hosts multiple PTDocumentViewControllers with a tabbed interface. For more information about the PTDocumentViewController class, please see this guide.

Tabbed viewer view controller

The tabbed viewer control is part of the Tools library, so make sure you have added the Tools library to your project.

linkShow a tabbed viewer controller

The following sample demonstrates how to show a tabbed viewer controller with a document from another UIViewController:

// Create and wrap a tabbed controller in a navigation controller.
let tabbedController = PTTabbedDocumentViewController()

let navigationController = UINavigationController(rootViewController: tabbedController)

// Open an existing local file URL.
let fileURL: URL! = Bundle.main.url(forResource: "sample", withExtension: "pdf")

tabbedController.openDocument(with: fileURL)

// Show navigation (and tabbed) controller.
self.present(navigationController, animated: true, completion: nil)

The openDocumentWithURL: method will create a new tab if the URL is not already being displayed, otherwise it will switch to the existing tab.

The tabbed viewer controller currently relies on the UINavigationController to supply a navigation bar for the buttons.

linkTab management

For more control over how tabs are added to the tabbed viewer controller, the addTabWithURL:selected:error: and insertTabWithURL:atIndex:selected:error: methods can be used:

// Add the fileURL to the end of the tab bar, without changing the selected tab.
do {
    try tabbedController.addTab(with: fileURL selected:false)
} catch {
    // Failed to add tab.
}

// Insert and select the otherFileURL at the start of the tab bar.
do {
    try tabbedController.insertTab(with: otherFileURL, at: 0, selected:true)
} catch {
    // Failed to insert and select tab.
}

linkConfigure the tab bar

The behavior of the tabbed viewer controller can be customized with several properties. The tabsEnabled and maximumTabCount properties can be used to disable the tab bar and limit the number of tabs, respectively. By default, tabs are enabled in the tabbed viewer controller and there is no limit on the number of tabs.

linkTab bar visibility

The tab bar's visibility is controlled by the tabBarHidden property. To animate the change in visibility, the setTabBarHidden:animated: method can be used.

When the containing navigation controller's navigation bar is hidden, the tabbed viewer also hides its tab bar.

linkTabbed viewer controller delegate

To configure a document view controller before it is displayed, conform to and implement the PTTabbedDocumentViewControllerDelegate method tabbedDocumentViewController(_:willAdd:). Note that it is permissible to assign the internal PTDocumentViewController's delegate to an external object.

func tabbedDocumentViewController(_ tabbedDocumentViewController: PTTabbedDocumentViewController, willAdd documentViewController: PTDocumentViewController) {

        documentViewController.delegate = self
        // customize documentViewController
    }

You can set a delegate to be notified by the tabbed viewer controller when tabs are removed with the PTTabbedDocumentViewControllerDelegate protocol's tabbedDocumentViewController:willRemoveTabAtIndex: method.

The tabbedDocumentViewController:willRemoveTabAtIndex: delegate method can be used to close the tabbed viewer controller when the last tab is closed:

func tabbedDocumentViewController(_ tabbedDocumentViewController: PTTabbedDocumentViewController, willRemoveTabAt index: UInt) {
    // Check if the last tab is being removed.
    if (tabbedDocumentViewController.tabURLs.count > 1) {
        return
    }
        
    // Close tabbed viewer controller.
    // ...
}

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close