Instant JSON

Instant JSON is our approach to bringing annotations into a modern format while keeping all important properties to make the Instant JSON spec work with PDF. It is fully documented and supports long-term storage.

Instant JSON stores PDF changes like annotations in a separate JSON file. This means that a PDF document will only need to be transferred once and all changes will be added as an overlay to the existing PDF. This approach significantly reduces the bandwidth since you only need to transfer this JSON instead of the complete PDF.

Conceptually, Instant JSON defines a list of skippedPdfObjectIds. These point to the PDF’s internal object IDs for annotations. Whenever an object ID is marked as skipped, it will no longer be loaded from the original PDF. Instead, it could be defined inside the annotations array with the same pdfObjectId. If this is the case, the PDF viewer will display the new annotation, which signals an update to the original one. If an object ID is marked as skipped but the annotations array does not contain an annotation with the same pdfObjectId, it will be interpreted as a deleted annotation. An annotation that is inside the annotations array without the pdfObjectId property is interpreted as a newly created annotation.

All annotations in the annotations array have a unique id field. For updated annotations that were already in the original PDF, this field will be the stringified pdfObjectId. Newly created annotations will get a newly generated ULID.

An “empty” Instant JSON contains neither skippedPdfObjectIds nor annotations, which means the original PDF is untouched. All annotations in the initial PDF are still shown.

format

This is a literal string that includes the version information.

pdfId

This optional key contains an object of a permanent PDF ID and a changing PDF ID. According to the PDF spec, a PDF document must contain these IDs. We use the permanent ID to verify that the PDF you’ve opened together with this Instant JSON is indeed the correct one. The changing PDF ID will be updated whenever the PDF file is updated (for example, when saved with different annotations). Since Instant JSON only works with an immutable PDF, the state will be invalid when used with a changed PDF.

Not every PDF will have a valid permanent or changing ID. As such, this field might not be set. We recommend you take care to always use the same PDF.

skippedPdfObjectIds

This is an array of PDF object IDs that will be ignored when importing annotations from the original PDF document. If this array is empty, the key should not be set.

annotations

This is a list of new or updated annotations. Annotations follow the format for Instant JSON for annotations. When an annotation contains a pdfObjectId, it is considered to be an update to one of the annotations of the original PDF. For newly created annotations, this key will not be set.

const{InkAnnotation,toSerializableObject}=PSPDFKit.Annotations;constannotation=newInkAnnotation();constserializedObject=toSerializableObject(annotation);constjson=JSON.stringify(serializedObject);// The annotation's Instant JSON data can be saved to an external file.

varannotation=newPSPDFKit.Annotations.InkAnnotation();varserializedObject=PSPDFKit.Annotations.toSerializableObject(annotation);varjson=JSON.stringify(serializedObject);// The annotation's Instant JSON data can be saved to an external file.

const{fromSerializableObject}=PSPDFKit.Annotations;constjson="{...}";constserializedObject=JSON.parse(json);constannotation=fromSerializableObject(serializedObject);// Add the newly created annotation to the document.instance.createAnnotation(annotation);

varjson="{...}";varserializedObject=JSON.parse(json);varannotation=PSPDFKit.Annotations.fromSerializableObject(serializedObject);// Add the newly created annotation to the document.instance.createAnnotation(annotation);

There are some limitations with Instant JSON, in that not all annotation types are currently supported, and only the properties that can be handled correctly across all of PSPDFKit’s supported platforms (iOS, Android, and Web) are serialized. For more information, check out the detailed JSON Format guide article.

Note for standalone users: Instead of saving annotations to the backend, a save operation will persist the annotations in memory until they are either exported via Instance#exportInstantJSON or written to the PDF document when you export it using
Instance#exportPDF. Unsaved annotations will not be exported to reflect the server behavior: