Importing and exporting annotations in Instant JSON

Annotations can be imported and exported to a document using Nutrient’s Instant JSON format, which keeps all the important properties of annotations and supports long-term storage. Refer to the Instant JSON documentation for more details about the format.

A single annotation JSON can be generated by calling the ToJson() method of our public API:

var annotation = _annotationManager.AnnotationFactory.CreateAnnotation<Line>();
annotation.StartPoint = new System.Drawing.PointF(10, 10);
annotation.EndPoint = new System.Drawing.PointF(100, 100);
annotation.BoundingBox = new Sdk.Models.Geometry.Rectangle()
 {
    Left = 0,
    Top = 0,
    Width = 100,
    Height = 100
};
var annotationJson = annotation.ToJson();
// Annotation Instant JSON.
{
	"startPoint": [10, 10],
	"endPoint": [100, 100],
	"strokeColor": "#2293FB",
	"strokeWidth": 5,
	"blendMode": "normal",
	"bbox": [0, 0, 100, 100],
	"createdAt": "2024-02-09T10:21:17.1203525+00:00",
	"opacity": 1,
	"pageIndex": 0,
	"updatedAt": "2024-02-09T10:21:17.1203525+00:00",
	"v": 2,
	"type": "pspdfkit/shape/line"
}

The annotation JSON that’s obtained can later be deserialized using the counterpart to CreateAnnotation(), PSPDFKit.Api.Annotation.IAnnotationFactory.CreateAnnotation():

var annotation = _annotationFactory.CreateAnnotation(JObject.Parse(annotationJson));

Importing annotations in Instant JSON

If the Document Editor component is included in the license, ImportInstantJsonAsync can be used to import Instant JSON via IAnnotationManager.ImportInstantJsonAsync():

await _annotationManager.ImportInstantJsonAsync(
  JObject.Parse(@"{
    bbox: [100, 150, 200, 75],
    blendMode: ""normal"",
    createdAt: ""1970-01-01T00:00:00Z"",
    id: ""01F73GJ4RPENTCMFSCJ5CSFT5G"",
    name: ""01F73GJ4RPENTCMFSCJ5CSFT5G"",
    opacity: 1,
    pageIndex: 0,
    strokeColor: ""#2293FB"",
    strokeWidth: 5,
    type: ""pspdfkit/shape/rectangle"",
    updatedAt: ""1970-01-01T00:00:00Z"",
    v: 1
  }"));

Importing Instant JSON annotations can also be achieved without the Document Editor license component using the IViewerConfiguration.InstantJson:

var config = PSPDFKitController.CreateViewerConfiguration();
config.InstantJson = JObject.Parse(@"{
    bbox: [100, 150, 200, 75],
    blendMode: ""normal"",
    createdAt: ""1970-01-01T00:00:00Z"",
    id: ""01F73GJ4RPENTCMFSCJ5CSFT5G"",
    name: ""01F73GJ4RPENTCMFSCJ5CSFT5G"",
    opacity: 1,
    pageIndex: 0,
    strokeColor: ""#2293FB"",
    strokeWidth: 5,
    type: ""pspdfkit/shape/rectangle"",
    updatedAt: ""1970-01-01T00:00:00Z"",
    v: 1
}");
await PSPDFKitController.LoadDocumentFromAssetsAsync(DemoFile, config);

Exporting annotations in Instant JSON

Annotations that have been saved can be exported in Instant JSON format using the IAnnotationManager.ExportInstantJsonAsync() API method:

const json = await _annotationManager.ExportInstantJsonAsync();