Customize a document view controller in Xamarin

This document refers to the PTDocumentController

For the legacy PTDocumentViewController class, please see this guide .

This article explains how to customize the document viewer classes PTDocumentController and PTTabbedDocumentViewController.

Because the document viewer classes are part of the open source Tools UI framework, it is possible to achieve virtually any required modification. That said, it is usually faster and more convenient to configure the viewers via APIs, which this guide describes.

PTDocumentController

Apryse Docs Image

The above image (items 1–5) indicates areas that are controllable via the PTDocumentController's API. Information on customizing these is available directly below.

Apryse Docs Image

The above image (items 7–12) indicates a number of default buttons that create and present new controls. Information on where to look to customize these presented controls can be found in the component controls table.

On iPad and other regular horizontal size class devices, a number of default UI controls are accessible from the navigation bar, whereas on iPhones and compact size classes some of those controls appear in the toolbar at the bottom of the view.

Image Number

Button

Functionality

Location on iPad

Location on iPhone

7

searchButtonItem

PTTextSearchViewController

navigationItem.rightBarButtonItems

navigationItem.rightBarButtonItems

8

moreItemsButtonItem

PTMoreItemsViewController

navigationItem.rightBarButtonItems

navigationItem.rightBarButtonItems

10

navigationListsButtonItem

PTNavigationListsViewController

navigationItem.leftBarButtonItems

toolbar

11

thumbnailsButtonItem

PTThumbnailsViewController

navigationItem.leftBarButtonItems

toolbar

12

readerModeButtonItem

PTReflowViewController

navigationItem.rightBarButtonItems

toolbar

1: Toolbar switcher

Hide the toolbar switcher

The toolbar switcher can be hidden:

C#

1documentController.ToolGroupIndicatorView.Hidden = true;

Remove toolbars from the switcher

Toolbars can be removed by removing them from the toolGroupManager's groups array. The following code removes the "Draw" and "Pens" toolbars:

C#

1var mutableGroups = documentController.ToolGroupManager.Groups;
2var groupsToRemove = new List<PTToolGroup> { documentController.ToolGroupManager.DrawItemGroup, documentController.ToolGroupManager.PensItemGroup};
3documentController.ToolGroupManager.Groups = mutableGroups.Except(groupsToRemove).ToArray();

2: Navigation bar items

The buttons which are contained in the leftBarButtonItems and rightBarButtonItems arrays, are completely customizable. It is possible to

Add buttons

You can modify the navigation bar items. Here is an example of adding a new button (for the current size class):

C#

1var myItem = new UIBarButtonItem(UIImage.GetSystemImage("square.and.pencil"), UIBarButtonItemStyle.Plain, (sender, e) =>
2{
3 Console.WriteLine("button pressed:" + sender);
4});
5
6var currentItems = documentController.NavigationItem.RightBarButtonItems;
7var itemsArray = new List<UIBarButtonItem>(currentItems);
8itemsArray.Add(myItem);
9
10documentController.NavigationItem.RightBarButtonItems = itemsArray.ToArray();

Tools can also be added:

C#

1var freeHand = documentController.ToolGroupManager.CreateItemForToolClass(new ObjCRuntime.Class(typeof(PTFreeHandCreate)));
2
3var currentItems = documentController.NavigationItem.RightBarButtonItems;
4var itemsArray = new List<UIBarButtonItem>(currentItems);
5itemsArray.Add(freeHand);
6
7documentController.NavigationItem.RightBarButtonItems = itemsArray.ToArray();

Note that in the example above, de-selecting the tool button item needs to be implemented by the app, by listening to the Tool Did Change notification.

Remove buttons

To remove any of the default buttons see the instructions here: Hide default buttons

Move buttons

The default buttons are all accessible via properties, making it easy to rearrange or move them. The following code swaps the position of the search button and navigation lists button:

C#

1var rightItems = documentViewController.NavigationItem.RightBarButtonItems;
2var rightItemsList = new List<UIBarButtonItem>(rightItems);
3rightItemsList.Remove(documentViewController.SearchButtonItem);
4rightItemsList.Add(documentViewController.NavigationListsButtonItem);
5
6var bottomRightItems = documentViewController.ThumbnailSliderController.TrailingToolbarItems;
7var bottomRightItemsList = new List<UIBarButtonItem>(bottomRightItems);
8bottomRightItemsList.Remove(documentViewController.NavigationListsButtonItem);
9bottomRightItemsList.Add(documentViewController.SearchButtonItem);
10
11documentViewController.NavigationItem.RightBarButtonItems = rightItemsList.ToArray();
12documentViewController.ThumbnailSliderController.TrailingToolbarItems = bottomRightItemsList.ToArray();

Change icons

The icons of existing buttons may be changed by creating new UIBarButtonItems that have the same target and action as an existing item, and replacing the existing item with the new item:

C#

1// new search UIBarButtonItem
2var newSearchItem = new UIBarButtonItem(UIBarButtonSystemItem.Search, documentController.SearchButtonItem.Target, documentController.SearchButtonItem.Action);
3index = rightItemsList.IndexOf(documentController.SearchButtonItem);
4rightItemsList.Remove(documentController.SearchButtonItem);
5rightItemsList.Insert(index, newSearchItem);
6
7// update the icons
8documentController.NavigationItem.RightBarButtonItems = rightItemsList.ToArray();

3: Annotation toolbar

The currently-visible annotation toolbar is selected with the toolbar switcher.

Hide (and show) the annotation toolbar

The toolbar can be programmatically hidden by setting the mode to view group, which is a special group and the only group where the toolbar is hidden:

C#

1documentController.ToolGroupManager.SelectedGroup = documentController.ToolGroupManager.ViewItemGroup;

The toolbar can be shown again by changing the group to any group other than view:

C#

1documentController.ToolGroupManager.SelectedGroup = documentController.ToolGroupManager.DrawItemGroup;

Remove buttons from a toolbar

The example below shows how to remove the text highlight and text underline button from a toolbar.

Disabling a tool type entirely

If you want to disable a tool entirely, from all toolbars and the long press menu, please use the annotations permissions system.

C#

1var annotateGroup = documentController.ToolGroupManager.AnnotateItemGroup;
2
3// tool buttons that exist currently
4var defaultAnnotateGroupTools = annotateGroup.BarButtonItems;
5
6// new set of tools to replace current ones
7var newAnnotateGroupTools = new List<UIBarButtonItem>();
8
9// add all currently existing tools except for the ones we don't want
10foreach (UIBarButtonItem defaultToolItem in defaultAnnotateGroupTools)
11{
12 if (defaultToolItem.IsKindOfClass(new ObjCRuntime.Class(typeof(PTToolBarButtonItem)))) {
13 var toolBarButton = (PTToolBarButtonItem)defaultToolItem;
14 if ( toolBarButton.ToolClass.Equals(new ObjCRuntime.Class(typeof(PTTextHighlightCreate))) ||
15 toolBarButton.ToolClass.Equals(new ObjCRuntime.Class(typeof(PTTextUnderlineCreate))) )
16 {
17 // do not add this tool
18 continue;
19 }
20 else
21 {
22 newAnnotateGroupTools.Add(toolBarButton);
23 }
24 }
25 else
26 {
27 newAnnotateGroupTools.Add(defaultToolItem);
28 }
29}
30// assign tools to new array
31documentController.ToolGroupManager.AnnotateItemGroup.BarButtonItems = newAnnotateGroupTools.ToArray();

Add a tool button to a toolbar

C#

1// create a mutable array of the current items in the annotation toolbar group
2var availableTools = new List<UIBarButtonItem>(documentController.ToolGroupManager.AnnotateItemGroup.BarButtonItems);
3
4// create a new toolbar item for freehand annotations
5var freeHandItem = documentController.ToolGroupManager.CreateItemForToolClass(new ObjCRuntime.Class(typeof(PTFreeHandCreate)));
6
7// add the freehand annotation item to the front of the list
8availableTools.Insert(0, freeHandItem);
9
10// assign the array back to the annotation toolbar group.
11documentController.ToolGroupManager.AnnotateItemGroup.BarButtonItems = availableTools.ToArray();

4: Page number indicator

The page indicator can be enabled/disabled via the pageIndicatorEnabled property.

5: PDFViewCtrl

The PTPDFViewCtrl is a UIView that displays the PDF. It is customizable via is properties/methods and delegate methods.

For an overview see the PTPDFViewCtrl Guide , or the detailed API documentation.

Note that all PDF "interaction" (annotations, form filling, text selection, link following, etc.) is supplementary to the PDFViewCtrl, and is implemented in the open source tools.framework.

6. Toolbar (iPhone only)

These are the buttons that appear at the bottom of the screen on iPhones and in Compact horizontal size classes. Buttons can be added, removed, or rearranged with convenient APIs.

Add buttons to toolbar

Remove buttons from toolbar

To remove any of the default buttons see the instructions here: Hide default buttons

7-12: Controls Presented by a PTDocumentController

To customize the controls that are presented by the PTDocumentController's default buttons, please see the corresponding guide or API:

Hide default buttons

Default buttons can be removed ("hidden") using built-in properties.

For example to hide the search and more items buttons:

C#

1documentController.SearchButtonHidden = true;
2documentController.MoreItemsButtonHidden = true;

PTTabbedDocumentViewController

The tabbed document view controller displays a collection of document controllers in tabs.

Tab Settings

Tabs can be disabled using the TabsEnabled property, and the maximum number of allowed tabs can be set using MaximumTabCount.

Access to child PTDocumentControllers

The current document controller can be accessed via SelectedViewController, and others via documentViewControllerAtIndex

To configure a document controller before it is displayed, conform to and implement the PTTabbedDocumentViewControllerDelegate method willAddDocumentViewController. Note that it is permissible to assign the internal PTDocumentViewController's delegate to an external object.

C#

1tabbedDocumentViewController.WillAddDocumentViewController += (sender, e) =>
2{
3 // do something with e.DocumentViewController
4};

Summary

API

Functionality

TabsEnabled

Enables/disables tabs.

MaximumTabCount

Controls the maximum number of concurrent tabs.

SelectedViewController

The current PTDocumentViewController.

documentViewControllerAtIndex

The PTDocumentViewController at the given index.

willAddDocumentViewController

Access to a PTDocumentViewController that is about to be displayed.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales