Customize the Document Viewer UI

Customize the Document Viewer UI in Xamarin.iOS

The following snippets assume you are using a PTDocumentController called documentController:

C#

1PTDocumentController documentController = new PTDocumentController();

Hide the Toolbar Switcher

C#

1documentController.ToolGroupIndicatorView.Hidden = true;

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

1NSMutableArray<PTToolGroup> mutableGroups = new NSMutableArray<PTToolGroup>(documentController.ToolGroupManager.Groups);
2
3mutableGroups.RemoveObject((nint)mutableGroups.IndexOf(documentController.ToolGroupManager.DrawItemGroup));
4mutableGroups.RemoveObject((nint)mutableGroups.IndexOf(documentController.ToolGroupManager.PensItemGroup));
5
6documentController.ToolGroupManager.Groups = NSArray.FromArray<PTToolGroup>(mutableGroups);

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#

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

Add a tool button to a toolbar

C#

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

Create a new toolbar

The code below creates a new toolbar that contains a free hand, cloudy and image stamp tool.

C#

1UIImage image = new UIImage("square.and.pencil");
2
3// the tools it will contain
4UIBarButtonItem freeHandItem = documentController.ToolGroupManager.CreateItemForToolClass(new ObjCRuntime.Class(typeof(PTFreeHandCreate)));
5UIBarButtonItem cloudyItem = documentController.ToolGroupManager.CreateItemForToolClass(new ObjCRuntime.Class(typeof(PTCloudCreate)));
6UIBarButtonItem stampItem = documentController.ToolGroupManager.CreateItemForToolClass(new ObjCRuntime.Class(typeof(PTImageStampCreate)));
7
8UIBarButtonItem[] tools = { freeHandItem, cloudyItem, stampItem };
9
10// the name of the custom group, its image, and its tool items
11PTToolGroup customGroup = PTToolGroup.GroupWithTitle("MyApps Group", image, tools);
12
13NSMutableArray<PTToolGroup> groups = new NSMutableArray<PTToolGroup>(documentController.ToolGroupManager.Groups);
14groupss.Add(customGroup);
15
16// add the tool group
17documentController.ToolGroupManager.Groups = NSArray.FromArray<PTToolGroup>(groups);

Add a button with fully custom behavior

Your app may need a button that does not invoke one of the built in annotation tools. The following code will add a button that calls a selector.

C#

1PTSelectableBarButtonItem selectableItem = new PTSelectableBarButtonItem();
2selectableItem.Title = "Custom Tool";
3selectableItem.Image = UIImage("square.and.pencil");
4selectableItem.Style = UIBarButtonItemStyle.Plain;
5
6NSMutableArray<UIBarButtonItem> availableTools = new NSMutableArray<UIBarButtonItem>(documentController.ToolGroupManager.AnnotateItemGroup.BarButtonItems);
7
8availableTools.Add(items);
9
10documentController.ToolGroupManager.AnnotateItemGroup.BarButtonItems = NSArray.FromArray<UIBarButtonItem>(availableTools);

If you want to toggle the items selection, flip its selected property:

C#

1selectableItem.Clicked += (button, e) => {
2 button.isSelected = !button.isSelected
3}

Modify UINavigationBar Items

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

C#

1UIBarButtonItem myItem = new UIBarButtonItem(UIImage("square.and.pencil"), UIBarButtonItemStyle.Plain, null);
2
3NSMutableArray<UIBarButtonItem> navigationItems = new NSMutableArray<UIBarButtonItem>(documentController.NavigationItem.RightBarButtonItems);
4navigationItems.Add(myItem);
5
6documentController.NavigationItem.RightBarButtonItems = NSArray.FromArray<UIBarButtonItem>(navigationItems);

Tools can also be added:

C#

1UIBarButtonItem freeHand = documentController.ToolGroupManager.CreateItemForToolClass(new ObjCRuntime.Class(typeof(PTFreeHandCreate)));
2NSMutableArray<UIBarButtonItem> navigationItems = new NSMutableArray<UIBarButtonItem>(documentController.NavigationItem.RightBarButtonItems);
3navigationItems.Add(freeHand);
4
5documentController.NavigationItem.RightBarButtonItems = NSArray.FromArray<UIBarButtonItem>(navigationItems);

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.

Add and Remove Toolbar Items (iPhone UI)

These are the buttons that appear at the bottom of the screen.

C#

1// new button
2UIBarButtonItem myItem = new UIBarButtonItem(UIImage("square.and.pencil"), UIBarButtonItemStyle.Plain, null, null);
3
4// spacer to keep evenly spaced buttons
5UIBarButtonItem spacer = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null, null);
6
7// new array
8NSMutableArray<UIBarButtonItem> toolbarItems = new NSMutableArray<UIBarButtonItem>(documentController.ToolbarItems);
9toolbarItems.Add(spacer);
10toolbarItems.Add(myItem);
11
12// set the toolbarItems to the new items
13documentController.ToolbarItems = NSArray.FromArray<UIBarButtonItem>(toolbarItems);

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales