Intercept annotation handling in Tools

In this document
chevron_rightIn Xamarin.Android
chevron_rightIn Xamarin.iOS

In Xamarin.Android

ToolManager events in Xamarin.Android can be done the same way they are done in Android. See tutorial here.

For example, the equivalent of the example provided in the Android guide will be:

To get notified before an action has happened:

mToolManager.InterceptAnnotationHandling += (sender, e) =>
{   
    var annot = TypeConvertHelper.ConvAnnotToManaged(e.Annot);
    var extra = e.Extra;

    /*
        For example, when a radio button is clicked on, this code will print:
        calling method: handleWidget
        property: OTHER
    */
    
    if (extra != null && extra.ContainsKey(Tool.MethodFrom))
    {
        string methodCalling = extra.GetString(Tool.MethodFrom);
        Console.WriteLine("InterceptAnnot calling method " + methodCalling);
        var property = pdftron.PDF.Model.AnnotationProperty.GetProperty(methodCalling);
        Console.WriteLine("InterceptAnnot property: " + property);
    }

    /*
        For example, when annotation opacity is to be changed to 50%, this code will print:
        calling method: editOpacity
        property: OPACITY
        key: opacity
        value: 0.5
    */
    if (extra != null && extra.ContainsKey(Tool.Keys))
    {
        String[] paramKeys = extra.GetStringArray(Tool.Keys);
        if (paramKeys != null)
        {
            foreach (string key in paramKeys)
            {
                Object param = extra.Get(key);
                Console.WriteLine("InterceptAnnot key: " + key);
                Console.WriteLine("InterceptAnnot value: " + param.ToString());
            }
        }
    }

    try
    {
        // Intercept radio button from clicking by return true
        if (annot.GetType() == Annot.Type.e_Widget)
        {
            var w = new pdftron.PDF.Annots.Widget(annot);
            Field field = w.GetField();
            if (field != null && field.IsValid() && field.GetType() == Field.Type.e_radio)
            {
                e.Handled = true;
                return;
            }
        }

        // Intercept clicking link annotation by return true
        if (annot.GetType() == Annot.Type.e_Link)
        {
            Console.WriteLine("InterceptAnnot handling link annotation");
        }
    }
    catch
    {
        // handle exception
    }
    // set handled to false so the rest events can continue executing
    e.Handled = false;
};

To get notified after an action has happened:

mToolManager.AnnotationsModified += (sender, e) =>
{
    var extra = e.Extra;
    /*
        For example, when checkbox value has been changed, this code will print:
        calling method: handleWidget
        property: OTHER
    */
    if (extra != null && extra.ContainsKey(Tool.MethodFrom))
    {
        String methodCalling = extra.GetString(Tool.MethodFrom);
        Console.WriteLine("AnnotationsModified calling method " + methodCalling);
        var property = pdftron.PDF.Model.AnnotationProperty.GetProperty(methodCalling);
        Console.WriteLine("AnnotationsModified property: " + property);
    }

    /*
        For example, when changed annotation opacity to 50%
        calling method: editOpacity
        property: OPACITY
        key: opacity
        value: 0.5
    */
    if (extra != null && extra.ContainsKey(Tool.Keys))
    {
        String[] paramKeys = extra.GetStringArray(Tool.Keys);
        if (paramKeys != null)
        {
            foreach (String key in paramKeys)
            {
                Object param = extra.Get(key);
                Console.WriteLine("AnnotationsModified key: " + key);
                Console.WriteLine("AnnotationsModified value: " + param.ToString());
            }
        }
    }

    // Do something with the annots
    foreach (var item in e.Annots)
    {
        var nativeAnnot = item.Key;
        var annot = TypeConvertHelper.ConvAnnotToManaged(nativeAnnot);
        if (annot != null && annot.IsValid())
        {
            Annot.Type type = annot.GetType();
            Console.WriteLine("AnnotationsModified: type: " + type);
        }
    }
};

In Xamarin.iOS

ToolManager events in Xamarin.iOS can be done the same way they are done in iOS. See tutorial here.

For example, the equivalent of the example provided in the iOS guide will be:

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();
}