> 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/xamarin/guides/ios/basics/open.md).

# Open a document (iOS)

Learn how to open a document in Xamarin.iOS using a document view controller or tabbed document view controller. Explore the features of the PTDocumentController for PDF viewing and annotation. Get st

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

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

## Document view controller in Xamarin.iOS

The [`PTDocumentController`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.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/xamarinios/tools/api/pdftron.PDF.Controls.PTTabbedDocumentViewController.html).

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%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/xamarinios/tools/api/pdftron.PDF.Controls.PTDocumentController.html) is a subclass of the [`PTDocumentBaseViewController`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.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*](/xamarin/ui-customization/setup.md)*.*

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

{% code lineNumbers="true" %}

```csharp
PTDocumentController documentController = new PTDocumentController();
UINavigationController navigationController = new UINavigationController(documentController);
NSUrl fileURL = NSBundle.MainBundle.GetUrlForResource("sample", "pdf");

documentController.OpenDocumentWithURL(fileURL);

documentController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, e) => {
    documentController.DismissViewController(true, null);
});
this.PresentViewController(navigationController, true, null);
```

{% endcode %}

## 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 in Xamarin.iOS

The [`PTTabbedDocumentViewController`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.PTTabbedDocumentViewController.html) class is a container view controller that hosts multiple [`PTDocumentController`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.PTDocumentController.html)[s](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.PTDocumentController.html) with a tabbed interface. For more information about the `PTDocumentController` class, please see [this guide](/xamarin/guides/ios/basics/open.md).

![](https://653871032-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgjsBtuYmcKhWOdM9VCg4%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](/xamarin/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`:

{% code lineNumbers="true" %}

```csharp
PTTabbedDocumentViewController tabbedController = new PTTabbedDocumentViewController();
UINavigationController navigationController = new UINavigationController(tabbedController);
NSUrl fileURL = NSBundle.MainBundle.GetUrlForResource("sample", "pdf");

tabbedController.OpenDocumentWithURL(fileURL);

this.PresentViewController(navigationController, true, null);
```

{% endcode %}

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:

{% code lineNumbers="true" %}

```csharp
// Add the fileURL to the end of the tab bar, without changing the selected tab.
NSError error = null;
bool success = tabbedController.AddTabWithURL(fileURL, false, out error);
if (!success)
{
    Console.WriteLine("add tab failed...");
}
// Insert and select the otherFileURL at the start of the tab bar.
NSError error = null;
bool success = tabbedController.InsertTabWithURL(otherFileURL, 0, true, out error);
if (!success)
{
    Console.WriteLine("insert and select tab failed...");
}
```

{% endcode %}

## 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/xamarinios/tools/api/pdftron.PDF.Controls.PTTabbedDocumentViewControllerDelegate.html) method [`willAddDocumentViewController`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.Controls.PTTabbedDocumentViewControllerDelegate.html). Note that it **is** permissible to assign the internal `PTDocumentController`'s delegate to an external object.

{% code lineNumbers="true" %}

```csharp
tabbedDocumentViewController.WillAddDocumentViewController += (sender, e) =>
{
    // do something with e.DocumentViewController
};
```

{% endcode %}

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:

{% code lineNumbers="true" %}

```csharp
tabbedDocumentViewController.WillRemoveTabAtIndex += (sender, e) => {
    if (((PTTabbedDocumentViewController)sender).TabURLs.Length > 1)
    {
        return;
    }
    ((PTTabbedDocumentViewController)sender).DismissViewController(true, null);
};
```

{% endcode %}
{% endtab %}

{% tab title="View" %}

## PTPDFViewCtrl in Xamarin.iOS

{% hint style="info" %}
**This tutorial only applies to Xamarin.iOS. See Xamarin.Android equivalent here .**
{% endhint %}

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

The easiest way to display a PDF is using a [`PTDocumentController`](/xamarin/guides/ios/basics/open.md). The [`PTPDFViewCtrl`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.PDFViewCtrl.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/xamarinios/tools/api/pdftron.PDF.PDFViewCtrl.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`](/xamarin/guides/ios/basics/open.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](/xamarin/get-started/faq/add-license.md).
2. Add a PDF to your project:

* Right click on `Resources`
* Click on `Add` > `Add Files`
* Choose a file (in this tutorial, let's say we added a `sample.pdf`) and confirm add
* Make sure the `Build Action` of the file is set to `BundleResource`

1. Instantiate a new [`PDFDoc`](https://sdk.apryse.com/api/xamarinios/pdfnet/api/pdftron.PDF.PDFDoc.html):

{% code lineNumbers="true" %}

```csharp
var pdfPath = "sample.pdf";
var docToOpen = new pdftron.PDF.PDFDoc(pdfPath);
```

{% endcode %}

1. Create a new [`PDFViewCtrl`](https://sdk.apryse.com/api/xamarinios/tools/api/pdftron.PDF.PDFViewCtrl.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 package ](/xamarin/annotation/namespaces.md#ios). To add support for annotations and text selection you need to [incorporate the tools package ](/xamarin/annotation/config-annot-tools.md#ios).

{% code lineNumbers="true" %}

```csharp
// Create a new PDFViewCtrl
var pdfViewCtrl = new pdftron.PDF.PDFViewCtrl();

// Set the document to display
pdfViewCtrl.Doc = TypeConvertHelper.ConvPDFDocToNative(docToOpen);

// Add the PDFViewCtrl to the current view controller's root view.
View.AddSubview(pdfViewCtrl);

// Auto Layout
pdfViewCtrl.TranslatesAutoresizingMaskIntoConstraints = false;
NSLayoutConstraint.ActivateConstraints(new NSLayoutConstraint[] {
    pdfViewCtrl.LeadingAnchor.ConstraintEqualTo(this.View.LeadingAnchor),
    pdfViewCtrl.WidthAnchor.ConstraintEqualTo(this.View.WidthAnchor),
    pdfViewCtrl.TopAnchor.ConstraintEqualTo(this.View.LayoutMarginsGuide.TopAnchor),
    pdfViewCtrl.BottomAnchor.ConstraintEqualTo(this.View.BottomAnchor)
});
```

{% endcode %}
{% 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/xamarin/guides/ios/basics/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.
