Some test text!

Search
Hamburger Icon

Xamarin / Guides / Custom event handlers

Annotation handling

Apryse SDK allows you to intercept annotation events and add custom code for when they are triggered.

Customize annotation handling for Xamarin.Android

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

By default, the Tools library will respond to user actions such as a click event on a link or form widget, or a property change event on a selected annotation. It is possible to customize such behavior before it is executed within the Tools library, as well as getting notified of an action after it has happened.

Intercept annotation events

If you would like to customize the behavior executed in response to user actions prior to it being handled by the Tools library, it is possible to do so with the BasicAnnotationListener.onInterceptAnnotationHandling(Annot, Bundle, int) API. If this method returns true, then it will intercept executing the default logic.

In `BasicAnnotationListener.onInterceptAnnotationHandling(Annot, Bundle, int)`, the second parameter, `Bundle`, contains the name of the action intercepted by this function, as well as some extra information. You can get the name of the intercepted action by calling `bundle.getString(Tool.METHOD_FROM)`:

To get extra information, first obtain the information keys by calling bundle.getStringArray(Tool.KEYS):

Then, you can get the information values by looping on each key obtained above:

Common use cases

Links

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

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

Form widgets

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

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

Full sample code

For example, the following code demonstrates several scenarios and how you can get notified before each: clicking on a radio button, changing the annotation opacity, and clicking on a link. See the comments in the code for details.

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

Get notified after an action

If you simply want to get notified after the action has happened, you can do so via the AnnotationModificationListener.onAnnotationsModified(Map<Annot, Integer>) API. This event callback is raised when an annotation change has occurred. For example, in order to get notified when the value of a checkbox has been changed and when annotation opacity has been changed, do the following:

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

We are always interested in expanding the customization options through APIs, and will be adding more options in the future. If you would like to suggest changes, please don't hesitate to get in touch.

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales