Adding a Vector Image Annotation from Instant JSON

This article outlines how to add a vector image annotation that’s a PDF using Instant JSON.

Vector images should be single-page PDFs. These can be embedded in other PDFs as image annotations.

In Instant JSON, the type of these annotations should be pspdfkit/image rather than pspdfkit/stamp. The custom stamp type is for stamps that show custom text but with a predefined style, while images can display any JPEG, PNG, or PDF image. However, for compatibility reasons, our iOS API doesn’t make this distinction: It models both stamps and images using the PSPDFStampAnnotation class.

The contentType should be set to application/pdf.

Here’s how to add an annotation with a PDF attachment using Instant JSON. The source image comes from my-custom-graphic.pdf in the app’s bundle:

PSPDFDocument *document = ...
NSData *instantJSONData = [@"{ \"type\": \"pspdfkit/image\", \"bbox\": [100, 100, 200, 200], \"v\": 1, \"pageIndex\": 0, \"opacity\": 1 }" dataUsingEncoding:NSUTF8StringEncoding];
NSData *attachmentData = [NSData dataWithContentsOfURL:[NSBundle.mainBundle URLForResource:@"my-custom-graphic" withExtension:@"pdf"]];

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

// Create the data container provider using the attachment data.
PSPDFDataContainerProvider *dataContainerProvider = [[PSPDFDataContainerProvider alloc] initWithData:attachmentData];

// Attach the attachment data.
NSError *attachBinaryError;
BOOL success = [imageAnnotation attachBinaryInstantJSONAttachmentFromDataProvider:dataContainerProvider error:&attachBinaryError];
if (!success) {
    // Handle error.
}

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

You can read more about this approach in our Instant JSON guide.