Some test text!

menu
search

Annotation and PDF interaction customization

PDFTron SDK provides various customization options for annotations interacting with documents.

Annotation and PDF interaction Customization

This tutorial only applies to Xamarin.iOS. See Xamarin.Android equivalent here.

Annotation and PDF interaction is implemented within the open-source Tools.framework. This includes annotation creation & selection, text selection, form filling and link following.

The behavior is implemented via a collection of "Tools": classes that derive from the abstract base class PTTool and that are coordinated by a PTToolManager. The PTToolManager provides the ability to customize tool behavior, and enable/disable the ability for users to create or edit annotations (both in general and on an annotation type by annotation type basis).

linkCreate and edit annotations

To disable (or re-enable) the ability to create or edit annotations, options can be altered as shown below. When disabled, the relevant annotation tool is removed from the annotation toolbar, and from the UIMenuController.

// disables all text annotation types (highlights, underlines, etc.)
documentViewController.ToolManager.HighlightAnnotationOptions.CanCreate = false;
documentViewController.ToolManager.HighlightAnnotationOptions.CanEdit = false;
documentViewController.ToolManager.UnderlineAnnotationOptions.CanCreate = false;
documentViewController.ToolManager.UnderlineAnnotationOptions.CanEdit = false;
documentViewController.ToolManager.StrikeOutAnnotationOptions.CanCreate = false;
documentViewController.ToolManager.StrikeOutAnnotationOptions.CanEdit = false;
documentViewController.ToolManager.SquigglyAnnotationOptions.CanCreate = false;
documentViewController.ToolManager.SquigglyAnnotationOptions.CanEdit = false;

linkAnnotation interaction and UIMenuController

Further control over annotation interaction behaviour via the PTToolManagerDelegate protocol.

There are several methods that allow you to be notified of, and if desired modify or prevent, default behavior from occurring. For all methods, if false is returned, then the default behavior will not occur.

  1. PTToolManager.ShouldSelectAnnotation

    This method is called just before an annotation is selected. The annotation object itself is available for the decision making process, and for further action if needed.

  2. PTToolManager.ShouldShowMenu

    This method is called just before the selection popup menu is shown. The UIMenuController object is passed so that menu items can be added or removed as required.

  3. PTToolManager.ShouldHandleLinkAnnotation

    This method is called just before a link is followed. The link is provided so that alternate action can be taken if required.

  4. PTToolManager.ShouldSwitchToTool

    This method is called just before a tool is activated. The tool is available for the decision making process.

linkExample

The sample code uses -toolManager:shouldShowMenu:forAnnotation:onPageNumber: to restrict use of the UIMenuController to copying and defining text found in the PDF. This is done by stopping the UIMenuController from popping up in any case other than selecting text, and by removing options to highlight/underline etc. from the selected text UIMenuController popup.

mToolManager.ShouldShowMenu = (sender, menuController, annotation, pageNumber) =>
{
    // remove all items except signature and free text
    if (mToolManager.Tool is pdftron.PDF.Tools.PanTool)
    {
        menuController.MenuItems = removeQuickMenuItem(menuController.MenuItems);
    }
    return true;
};

UIMenuItem[] removeQuickMenuItem(UIMenuItem[] items) {
    var itemsArray = new List<UIMenuItem>(items);
    var itemsToRemove = new List<String> {
        "Note",
        "Ink",
        "Arrow",
        "Line",
        "Rectangle",
        "Ellipse"
    };

    for (int i = 0; i < items.Length; i++) {
        var itemStr = PDFViewCtrlToolsUtil.ToolsBundle.GetLocalizedString(items[i].Title, null);
        if (itemsToRemove.Contains(itemStr)) {
            itemsArray.Remove(items[i]);
        }
    }
    return itemsArray.ToArray();
}
Default Menu OptionsCustomized Menu Options
Default Text MenuCustom Text Menu
Default Rect MenuCustom Rect Menu

linkOverride classes

Tools.framework frequently creates new instances of objects defined within the framework (e.g. a new instance of PTAnnotEditTool is created when a user taps on an annotation). In order to customize certain aspects of the default behavior, it is required to create and use a subclass of the built-in tool. To enable this without requiring source code modification, Tools.framework includes a system to "inject" an externally defined subclass that will be used by the Tools.framework during its normal operation. This is done via the PTOverrides class as follows:

// EGAnnotEditTool is an externally defined class that derives from PTAnnotEditTool
var cls = new Class(typeof(PTAnnotEditTool));
var own = new Class(typeof(EGAnnotEditTool));
PTOverrides.OverrideClass(cls, own);

Note that in order to be compatible with the external override system, the base class must conform to the PTOverridable protocol.

linkCustomize by modifying the source code

Because Tools.framework is open source, any and all changes are possible by editing the source code directly. The project is found in /Lib/Tools/src.

If you customize the tools source code, you will likely want to use a universal framework (one that contains architectures for both simulators and devices), which is not done automatically by Xcode. The Tools project includes a custom script step that automatically creates a universal framework, located in a folder next to the default build location, with the name {Debug,Release}-universal.

(Building a universal framework also be done manually by first building for device, then simulator, and using the terminal command xcrun -sdk iphoneos lipo -create to merge the binaries (Tools.framework/Tools) into a universal binary.)

Then create the Xamarin binding as described here: Create Tools package from PDFViewCtrlTools Objective-C source code.

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