> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/ios/basic-operations/open.md).

# Open a document with Apryse iOS SDK

Learn how to open a document using a document view controller, tabbed document view controller, or an embedded view. Explore the features of a document viewer and how to respond to opening events. Enh

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

{% tabs %}
{% tab title="Document viewer" %}

## Document view controller

The [`PTDocumentController`](https://sdk.apryse.com/api/ios/Classes/PTDocumentController.html) shows a PDF viewer and annotator complete with controls such as an annotation toolbar, page layout controls, bookmarks, thumbnail viewer, etc. All of its component pieces are part of the Tools framework, and this class packages them into one easy to use view controller.

For a version of `PTDocumentController` that supports multiple documents, see [`PTTabbedDocumentViewController`](https://sdk.apryse.com/api/ios/Classes/PTTabbedDocumentViewController.html).

![](https://4149080208-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgY6xtY9ZQd9XMpvWlGM0%2Fuploads%2Fgit-blob-efcb719055c893f05756c361db34f29a7c9b6832%2F4b994036c8b8b340ce6e6886be3e57ab8d1166d3-1200x739.png?alt=media)

The document viewer includes a convenient document interaction and annotation toolbar at the top as well as convenient access to a number of powerful built-in components.

The [`PTDocumentController`](https://sdk.apryse.com/api/ios/Classes/PTDocumentController.html) is a subclass of the [`PTDocumentBaseViewController`](https://sdk.apryse.com/api/ios/Classes/PTDocumentBaseViewController.html) from which it inherits much of its functionality.

## Show a document

*The document viewer is part of the Tools library, so make sure you have* [*added the Tools library to your project*](/ios/ui-customization/setup.md)*.*

The following shows how to create a document viewer, set the document to display, and present it on screen.

{% tabs %}
{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// Create a PTDocumentController
let documentController = PTDocumentController()

// The PTDocumentController must be in a navigation controller before a document can be opened
let navigationController = UINavigationController(rootViewController: documentController)

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

documentController.openDocument(with: fileURL)

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

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// Create a PTDocumentController
PTDocumentController *documentController = [[PTDocumentController alloc] init];

// The PTDocumentController must be in a navigation controller before a document can be opened
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:documentController];

// Open an existing local file URL.
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Sample" withExtension:@"pdf"];

[documentController openDocumentWithURL:fileURL];

// Show navigation (and document) controller.
[self presentViewController:navigationController animated:YES completion:nil];
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Respond to Opening Events

`openDocumentWithURL:` is an asynchronous method. To respond to errors or other events that occur during opening, assign an object that conforms to the `PTDocumentControllerDelegate` to the `PTDocumentController`'s `delegate` property and implement the following optional methods:

* `documentControllerDidOpenDocument:` to respond to successfully opening a document.
* `documentController:didFailToOpenDocumentWithError:` to respond to an error during document opening.
* `documentController:destinationURLForDocumentAtURL:` to control where remote or converted documents are saved.
  {% endtab %}

{% tab title="Tabbed document viewer" %}

## Show Documents in a Tabbed ViewController

The [`PTTabbedDocumentViewController`](https://sdk.apryse.com/api/ios/Classes/PTTabbedDocumentViewController.html) class is a container view controller that hosts multiple [`PTDocumentController`](https://sdk.apryse.com/api/ios/Classes/PTDocumentController.html)[s](https://sdk.apryse.com/api/ios/Classes/PTDocumentController.html) with a tabbed interface. For more information about the `PTDocumentController` class, please see [this guide](/ios/basic-operations/open/doc-view.md).

![](https://4149080208-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgY6xtY9ZQd9XMpvWlGM0%2Fuploads%2Fgit-blob-88e1b1f499ccd3ccf7b025be33e3ed027d4232b4%2F53e038b02f332f9d803397f455b64f6b16065c40-1200x739.png?alt=media)

On iPads or other devices with a Regular [horizontal size class](https://developer.apple.com/uikit/uitraitcollection/) then the tabs will be shown in a bar along the top of the view, for Compact size classes the tabs will be accessible in the UIToolbar at the bottom of the view.

*The tabbed viewer control is part of the Tools library, so make sure you have* [*added the Tools library to your project*](/ios/ui-customization/setup.md)*.*

## Show a tabbed viewer controller

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

{% tabs %}
{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// 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)
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// Create and wrap a tabbed controller in a navigation controller.
PTTabbedDocumentViewController *tabbedController = [[PTTabbedDocumentViewController alloc] init];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:tabbedController];

// Open an existing local file URL.
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Sample" withExtension:@"pdf"];

[tabbedController openDocumentWithURL:fileURL];

// Show navigation (and tabbed) controller. 
[self presentViewController:navigationController animated:YES completion:nil];
```

{% endcode %}
{% endtab %}
{% endtabs %}

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.

## Tab 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:

{% tabs %}
{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// 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.
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// Add the fileURL to the end of the tab bar, without changing the selected tab.
NSError *error = nil;
BOOL success = [tabbedController addTabWithURL:fileURL selected:NO error:&error];
if (!success) {
	// Failed to add tab.
}

// Insert and select the otherFileURL at the start of the tab bar.
error = nil;
success = [tabbedController insertTabWithURL:otherFileURL atIndex:0 selected:YES error:&error];
if (!success) {
	// Failed to insert and select tab.
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Configure 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.

### Tab 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.

## Tabbed viewer controller delegate

To configure a document view controller before it is displayed, conform to and implement the [`PTTabbedDocumentViewControllerDelegate`](https://sdk.apryse.com/api/ios/Protocols/PTTabbedDocumentViewControllerDelegate.html#/c:objc\(pl\)PTTabbedDocumentViewControllerDelegate\(im\)tabbedDocumentViewController:willAddDocumentViewController:) method [`tabbedDocumentViewController:willAddDocumentViewController:`](https://sdk.apryse.com/api/ios/Protocols/PTTabbedDocumentViewControllerDelegate.html#/c:objc\(pl\)PTTabbedDocumentViewControllerDelegate\(im\)tabbedDocumentViewController:willAddDocumentViewController:). Note that it **is** permissible to assign the internal `PTDocumentController`'s delegate to an external object.

{% tabs %}
{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
func tabbedDocumentViewController(_ tabbedDocumentViewController: PTTabbedDocumentViewController, willAdd documentViewController: PTDocumentController) {

        documentViewController.delegate = self
        // customize documentViewController
    }
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
-(void)tabbedDocumentViewController:(PTTabbedDocumentViewController *)tabbedDocumentViewController willAddDocumentViewController:(PTDocumentController *)documentViewController
{
    documentViewController.delegate = self;
    // customize documentViewController
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

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:

{% tabs %}
{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
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.
    // ...
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
- (void)tabbedDocumentViewController:(PTTabbedDocumentViewController *)tabbedDocumentViewController willRemoveTabAtIndex:(NSUInteger)index
{
    // Check if the last tab is being removed.
    if (tabbedDocumentViewController.tabURLs.count > 1) {
        return;
    }
    
    // Close tabbed viewer controller.
    // ...
}
```

{% endcode %}
{% endtab %}
{% endtabs %}
{% endtab %}

{% tab title="View" %}

## PTPDFViewCtrl

{% hint style="info" %}
**Displaying a PDF**

The easiest way to display a PDF is using a [`PTDocumentController`](/ios/basic-operations/open/doc-view.md). The [`PTPDFViewCtrl`](https://sdk.apryse.com/api/ios/Classes/PTPDFViewCtrl.html) is a low-level control that may be used when a higher level of customization is required.
{% endhint %}

## About PTPDFViewCtrl

The [`PTPDFViewCtrl`](https://sdk.apryse.com/api/ios/Classes/PTPDFViewCtrl.html) is a `UIView` that displays a PDF.

If your app is displaying a PDF, a `PTPDFViewCtrl` will be used in one of two ways:

* Directly, by adding it as a subview to another view in your app.
* As a component piece of a [`PTDocumentController`](/ios/basic-operations/open/doc-view.md) or `PTTabbedDocumentViewController`. (It is accessible via the `pdfViewCtrl` property.)

## Use PTPDFViewCtrl as a stand-alone component

1. These instructions assume that you've [initialized Apryse](/ios/learn-more/initialize.md).
2. Add a PDF to your project, by dragging it into the project's left side panel. Ensure that it will be copied into the bundle's resources by checking that it is listed under the Copy Bundle Resources section of the project's Build Phases. (If it is not present here, add it using the '+' button.)The file `sample.pdf` has been added to the project, and will be copied into the bundle.

![](https://4149080208-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgY6xtY9ZQd9XMpvWlGM0%2Fuploads%2Fgit-blob-58c47e49ee48983b0fca41aaf61fb87554b96361%2Fbede1a65778f653e8d6d048f0434b6119eda5af8-1920x1308.png?alt=media)

1. Instantiate a new [`PTPDFDoc`](https://sdk.apryse.com/api/ios/Classes/PTPDFDoc.html):

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// Get the path to document in the app bundle.
NSString* pdfPath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
    
// Instantiate a new PDFDoc with the path to the file.
PTPDFDoc* docToOpen = [[PTPDFDoc alloc] initWithFilepath:pdfPath];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// Get the path to document in the app bundle.
let pdfPath: String? = Bundle.main.path(forResource: "sample", ofType: "pdf")
    
// Instantiate a new PDFDoc with the path to the file.
let docToOpen = PTPDFDoc(filepath: pdfPath)
```

{% endcode %}
{% endtab %}
{% endtabs %}

1. Create a new [`PTPDFViewCtrl`](https://sdk.apryse.com/api/ios/Classes/PTPDFViewCtrl.html) and add it as a child to the current view:Running the app will now display the PDF `sample.pdf`. However, it will not support annotation creation, editing, text selection, or any other UI aspect that is handled by [Tools.framework ](/ios/annotation/overview.md). To add support for annotations and text selection you need to [incorporate the tools framework ](/ios/ui-customization/setup.md).

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// Create a new PDFViewCtrl
PTPDFViewCtrl* pdfViewCtrl = [[PTPDFViewCtrl alloc] init];

// Set the document to display
[pdfViewCtrl SetDoc:docToOpen];

// Add the PDFViewCtrl to the current view controller's root view.
[self.view addSubview:pdfViewCtrl];
  
// Set the size of the PDFViewCtrl to that of its superview.
// (Auto Layout is fully supported as well)
pdfViewCtrl.frame = self.view.bounds;
pdfViewCtrl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// Create a new PDFViewCtrl.
let pdfViewCtrl = PTPDFViewCtrl()

// Set the document to display.
pdfViewCtrl.setDoc(docToOpen)

// Add the PDFViewCtrl to the current view controller's root view.
self.view.addSubview(pdfViewCtrl)

// Set the size of the PDFViewCtrl to that of its superview.
// (Auto Layout is fully supported as well)
pdfViewCtrl.frame = self.view.bounds
pdfViewCtrl.autoresizingMask = [.flexibleWidth, .flexibleHeight]
```

{% endcode %}
{% endtab %}
{% endtabs %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/ios/basic-operations/open.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
