Storing Custom Data in Annotations on Android

When adding an annotation to a document, PSPDFKit allows storing of additional data that you can specify for each annotation. This data is persistently stored along with the annotation if the annotation is stored in one of the following formats:

  1. Embedded in the PDF

  2. Instant JSON

  3. XFDF

PSPDFKit automatically handles the serialization of the custom data attached to an annotation for these formats.

Storing Custom Data

PSPDFKit 5.3 for Android added the setCustomData() method to Annotation. This allows you to store arbitrary data in an annotation in the form of a JSONObject, like so:

val restaurantInfo = JSONObject()
    .put("name", "Grill House")
    .put("locations", JSONArray(listOf("Vienna", "Paris", "New York")))
    .put("rating", 5)
    .put("verified", true)

// The `customData` property is available on all subclasses of `Annotation`.
annotation.customData = restaurantInfo
JSONObject restaurantInfo = new JSONObject()
    .put("name", "Grill House")
    .put("locations", new JSONArray(Arrays.asList("Vienna", "Paris", "New York")))
    .put("rating", 5)
    .put("verified", true);

// The `customData` property is available on all subclasses of `Annotation`.

This JSON object is saved with the annotation when the document is saved. PSPDFKit does not use this property internally for any reason; you have complete control over its contents.