> 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/android/annotation/sync.md).

# Annotation syncing in Android

Learn how to sync annotations between clients for collaboration with Apryse. Follow instructions for importing and exporting annotation changes on the client side. Ensure permanent unique identifiers

Apryse supports annotation syncing between different clients to allow for collaboration. All the required locking, change tracking, merging and view updating are handled internally. Below are instructions on how to handle the import and export of annotation changes on the client side. While the server component is not described here, that implementation would be straightforward. The server would receive XFDF XML data, which it would then pass onto all other active clients that are part of the collaboration session. The clients take care of the updating. The server can store any XFDF data as it likes, since the data is just an XML string.

## Requirements

Here are a few requirements for syncing to work as expected:

* A **permanent and unique** string identifier is needed for the **userId**. UserId needs to be permanent and cannot be changed later on. For example, if `Alice` was used for the userId, however, was later changed to `AliceB`, the viewer will **no longer** treat it as the same user. Therefore, we recommend that you generate a **UUID** for each user.
* Similar to userId, each annotation will also need its own UUID, aka **annotId**. This UUID also needs to be **permanent and unique**.
* Annotation changes are stored as XFDF command strings, which PDFNet generates when changes are made. However, any custom modification to the XFDF command string could lead to unexpected results, thus, modification to the XFDF command string are not recommended.
* Existing annotations that do not have a unique identifier will not work, it is recommended that you pre-process all annotations to make sure they all have unique identifiers.
* Undo and redo will be automatically enabled upon using annotation syncing.

## Initialize and sending annotation changes

Add the following after [`ToolManager`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/tools/ToolManager.html) is initialized:

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

```java
// supply a permanent and unique userId here
mToolManager.enableAnnotManager("12345-67890-ABCD-EFGH", new AnnotManager.AnnotationSyncingListener() {
    @Override
    public void onLocalChange(String action, String xfdfCommand, String xfdfJSON) {
        // a local annotation change event has happened,
        // now is the time to send the XFDF command string to your remote service
        // action is one of {@link AnnotManager.AnnotationAction#ADD}
        //                  {@link AnnotManager.AnnotationAction#MODIFY}
        //                  {@link AnnotManager.AnnotationAction#DELETE}
        //                  {@link AnnotManager.AnnotationAction#UNDO}
        //                  {@link AnnotManager.AnnotationAction#REDO}
        // xfdfCommand is the XFDF command string, modification to this string is not recommended
    }
});
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
// supply a permanent and unique userId here
mToolManager.enableAnnotManager("12345-67890-ABCD-EFGH", object : AnnotManager.AnnotationSyncingListener() {
    fun onLocalChange(action: String, xfdfCommand: String, xfdfJSON: String) {
        // a local annotation change event has happened,
        // now is the time to send the XFDF command string to your remote service
        // action is one of {@link AnnotManager.AnnotationAction#ADD}
        //                  {@link AnnotManager.AnnotationAction#MODIFY}
        //                  {@link AnnotManager.AnnotationAction#DELETE}
        //                  {@link AnnotManager.AnnotationAction#UNDO}
        //                  {@link AnnotManager.AnnotationAction#REDO}
        // xfdfCommand is the XFDF command string, modification to this string is not recommended
    }
})
```

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

PDFNet will generate a unique identifier for every annotation created through the Apryse SDK. However, if you would like to generate your own annotation identifier, you are able to do so as follows:

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

```java
mToolManager.setExternalAnnotationManagerListener(
    new ToolManager.ExternalAnnotationManagerListener() {
        @Override
        public String onGenerateKey() {
            // generate your own permanent and unique annotId here
            return UUID.randomUUID().toString();
        }
    });
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
mToolManager.setExternalAnnotationManagerListener(
  object : ToolManager.ExternalAnnotationManagerListener() {
      fun onGenerateKey(): String {
          // generate your own permanent and unique annotId here
          return UUID.randomUUID().toString()
      }
  })
```

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

## Receive annotation changes

When an annotation change event is received from a remote service, add the following to notify the viewer about the change:

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

```java
public void receivedAnnotationEvents(String xfdfCommand) {
    if (mToolManager.getAnnotManager() != null) {
        mToolManager.getAnnotManager().onRemoteChange(xfdfCommand);
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
fun receivedAnnotationEvents(xfdfCommand: String) {
    mToolManager.annotManager?.onRemoteChange(xfdfCommand)
}
```

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

## Jump to annotation

To jump to an annotation by id:

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

```java
public void jumpToAnnotation(String annotId) {
    if (mToolManager.getAnnotManager() != null) {
        mToolManager.getAnnotManager().jumpToAnnot(annotId);
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
fun jumpToAnnotation(annotId: String) {
    mToolManager.annotManager?.jumpToAnnot(annotId)
}
```

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


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/android/annotation/sync.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
