Instant JSON

Instant JSON is our approach to bringing annotations into a sane format while keeping all important properties to make the Instant JSON spec work with PDF. It is fully documented and supports long-term storage. The PSPDFJSONSerialization protocol has been deprecated in favor of the new API.

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.

The Format

We use Flow type declarations to specify the format of Instant JSON:

Copy
1
2
3
4
5
6
7
8
9
10
declare type InstantJSON = {
  format: 'https://pspdfkit.com/instant-json/v1',
  pdfId?: {
    permanent: string,
    changing: string,
  },
  skippedPdfObjectIds?: number[],
  annotations?: Object[],
  formFieldValues?: Object[],
}
  • format

    This is a literal string that includes the version information.

  • pdfId

    This optional key contains an object of a permanent and a changing PDF ID. According to the PDF spec, a PDF document must contain those 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 Instant Annotation JSON format. 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.

  • formFieldValues

    This is a list of modified form field values. Objects follow the Instant Form Field Value JSON format. This list will not be preset when no form field values have been modified.

Instant Annotation JSON API

Annotation JSON is Instant’s representation of a single annotation. This can be generated by using the methods available in PSPDFAnnotation. For generation, use -[PSPDFAnnotation generateInstantJSONWithError:]:

Copy
1
2
3
4
let annotation = ...
let instantJSONData = try? annotation.generateInstantJSON()

// The annotation's Instant JSON data can be saved to an external file.
Copy
1
2
3
4
5
6
7
PSPDFAnnotation *annotation = ...
NSData *instantJSONData = [annotation generateInstantJSONWithError:&instantJSONGenerationError];
if (!instantJSON || instantJSONGenerationError) {
    // handle error
}

// The annotation's Instant JSON data can be saved to an external file.

For creating an annotation from the JSON, use -[PSPDFAnnotation annotationFromInstantJSON:documentProvider:error:], like so:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
// Load the document.
let document = ...
let loadedInstantJSONData = ... // The Instant JSON data can be loaded from an external file.

guard let documentProvider = document.documentProviders.first,
    // Create annotation from Instant JSON data
    let annotation = try? PSPDFAnnotation(fromInstantJSON: data, documentProvider: documentProvider) else {
        // Handle error.
}

// Add the the newly created annotation to the document.
document.add([annotation])
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
// Load the document.
PSPDFDocument *document = ...
NSData *loadedInstantJSONData = ... // The Instant JSON data can be loaded from an external file.

// Create annotation from Instant JSON data.
NSError *annotationCreationError;
PSPDFAnnotation *annotation = [PSPDFAnnotation annotationFromInstantJSON:loadedInstantJSONData documentProvider:document.documentProviders.firstObject error:&annotationCreationError];
if (!annotation || annotationCreationError) {
    // Handle error.
}

// Add the the newly created annotation to the document.
[document addAnnotations:@[annotation] options:nil];

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 Instant Annotation JSON format specification article and the “Instant JSON — Annotation” example from InstantJSON.swift in PSPDFCatalog.

Instant Document JSON API

Document JSON is a serializable representation of the current changes to a document, i.e. a diff between the PSPDFDocument’s saved and unsaved changes. This can be used to transfer a set of changes across devices without having to send the entire PDF, which could potentially be very large. PSPDFKit for Web uses this in standalone deployment to reduce bandwidth usage. Currently, the generated JSON only contains changes to annotations.

To generate Instant Document JSON, simply call -[PSPDFDocument generateInstantJSONFromDocumentProvider:error:] on the document from which you wish to retrieve currently unsaved changes in JSON form. Note that this method will return nil if there are no unsaved changes in the document:

1
let data = try? document.generateInstantJSON(from: document.documentProviders.first)
Copy
1
2
3
4
5
NSError *error;
NSData *data = [document generateInstantJSONFromDocumentProvider:document.documentProviders.firstObject error:&error];
if (!data) {
  // Handle Error.
}

This generated JSON can then be applied to a document using -[PSPDFDocument applyInstantJSONFromDataProvider:toDocumentProvider:error:]. If you have an NSData object containing the Document JSON data, create a PSPDFDataContainerProvider and pass that as the dataProvider argument to the method, like this:

Copy
1
2
3
4
5
6
7
let document = ...
do {
    let jsonContainer = PSPDFDataContainerProvider(data:data!)
    try document.applyInstantJSON(fromDataProvider: jsonContainer, to: document.documentProviders.first)
} catch {
    // Handle error
}
Copy
1
2
3
4
5
6
7
8
PSPDFDocument *document = ...
PSPDFDataContainerProvider *jsonContainer = [[PSPDFDataContainerProvider alloc] initWithData:data];
NSError *error;
BOOL success = [document applyInstantJSONFromDataProvider:jsonContainer toDocumentProvider:document.documentProviders.firstObject error:&error];
if (!success) {
    // Handle error
}

For more details, see the “Instant JSON — Document” example from InstantJSON.swift in PSPDFCatalog.