> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/annotation/customize/customize-serialization.md).

# Customize Annotation Serialization

Customize Annotation Serialization to tailor XFDF serialization in WebViewer. Learn to serialize custom properties and read them back with setCustomSerializeHandler and setCustomDeserializeHandler API

Annotation classes understand how to serialize their properties into XFDF according to the XFDF specification. This can be customized in WebViewer to serialize custom properties and read those properties back in. This is especially useful when storing the XFDF externally, as custom attributes/elements outside the specification will be ignored by WebViewer when used to save to the document.

{% hint style="info" %}
The APIs in this guide are only available in WebViewer 7.1+.
{% endhint %}

## Customizing serialization

To change the serialization behavior for an annotation class, you can use the [setCustomSerializeHandler](https://sdk.apryse.com/api/web/Core.Annotations.html#.setCustomSerializeHandler) API.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
Annotations.setCustomSerializeHandler(Annotations.RectangleAnnotation, function(element, pageMatrix, options) {
  const annot = options.annotation;
  options.originalSerialize(element, pageMatrix)
  if (annot.Width > 100) {
    element.setAttribute('myAttr', annot.myProperty);
  }
  return element;
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Annotations.setCustomSerializeHandler](https://sdk.apryse.com/api/web/Core.Annotations.html#.setCustomSerializeHandler)

{% tabs %}
{% tab title="Shell" %}
{% code lineNumbers="true" %}

```sh
<square page="0" rect="170.090,587.300,337.210,693.110" color="#E44234" flags="print" name="fa7f93a0-4049-e95d-3179-d0820470ade5" title="Guest" subject="Rectangle" date="D:20221016023516-07'00'" creationdate="D:20221016023516-07'00'" myAttr="1"/>
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Customizing deserialization

To change an annotation class to read custom attributes/elements in the XFDF, you can use the [setCustomDeserializeHandler](https://sdk.apryse.com/api/web/Core.Annotations.html#.setCustomDeserializeHandler) API to change that behavior.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
Annotations.setCustomDeserializeHandler(Annotations.RectangleAnnotation, function(element, pageMatrix, options) {
  const annot = options.annotation;
  options.originalDeserialize(element, pageMatrix)
  if (annot.Width > 100) {
    annot.myProperty = element.getAttribute('myAttr');
  }
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Annotations.setCustomDeserializeHandler](https://sdk.apryse.com/api/web/Core.Annotations.html#.setCustomDeserializeHandler)

## Handler function

Both APIs, [setCustomSerializeHandler](https://sdk.apryse.com/api/web/Core.Annotations.html#.setCustomSerializeHandler) and [setCustomDeserializeHandler](https://sdk.apryse.com/api/web/Core.Annotations.html#.setCustomDeserializeHandler), require new, custom function handlers to be provided. They are similar to the `serialize` and `deserialize` functions on annotations except with an additional `options` parameter.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
// Serialize handler signature
function(element, pageMatrix, options) {
  ...
  return element;
}

// Deserialize handler signature
function(element, pageMatrix, options) {
  ...
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

[CustomAnnotationSerializeHandler ](https://sdk.apryse.com/api/web/Core.Annotations.html#.CustomAnnotationSerializeHandler)[CustomAnnotationDeserializeHandler](https://sdk.apryse.com/api/web/Core.Annotations.html#.CustomAnnotationDeserializeHandler)

The `options` parameter contains the annotation being serialized/deserialized. It also includes the original serialize and deserialize functions, `originalSerialize` and `originalDeserialize`, so that you can perform regular operations and then building on top of existing functionality.

## Restoring original behaviors

When customizing annotation serialization and/or deserialization, the original respective function is preserved. Calling an API to change the behavior again will not overwrite the original, preserved function. This allows the original behavior of the annotation to be restored when you want to restore the original behavior.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
// Restore serialization behavior
Annotations.restoreSerialize(Annotations.RectangleAnnotation);

// Restore deserialization behavior
Annotations.restoreDeserialize(Annotations.RectangleAnnotation);
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Annotations.restoreSerialize](https://sdk.apryse.com/api/web/Core.Annotations.html#.restoreSerialize) [Annotations.restoreDeserialize](https://sdk.apryse.com/api/web/Core.Annotations.html#.restoreDeserialize)

## Next steps

Change how annotations [render](/web/annotation/customize/customize-rendering.md) using your custom properties.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/annotation/customize/customize-serialization.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
