How do I Programmatically Add Annotations to the Saved Annotations list?

Q: How do I Programmatically Add Annotations to the Saved Annotations list?

Custom Saved Annotations

A: You need to create an AnnotationSet and update the SavedAnnotationsViewController’s sharedAnnotationStore’s annotationSets property, like so:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// New annotations.
let annotations = ...

// The default shared annotations store.
let sharedAnnotationStore = SavedAnnotationsViewController.shared

// Get the current annotation sets.
var annotationSets = sharedAnnotationStore.annotationSets;

// Append the annotations.
annotationSets.append(AnnotationSet(annotations: annotations, copyAnnotations: true))

// Update the shared annotation store's annotation sets.
sharedAnnotationStore.annotationSets = annotationSets
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// New annotations.
NSArray<PSPDFAnnotation *> *annotations = [document annotationsForPageAtIndex:0 type:PSPDFAnnotationTypeAll];

// The default shared annotations store.
id<PSPDFAnnotationSetStore> sharedAnnotationStore = PSPDFSavedAnnotationsViewController.sharedAnnotationStore;

// Get the current annotation sets.
NSMutableArray<PSPDFAnnotationSet *> *annotationSets = sharedAnnotationStore.annotationSets.mutableCopy;

// Append the annotations.
[annotationSets addObject:[[PSPDFAnnotationSet alloc] initWithAnnotations:annotations copyAnnotations:YES]];

// Update the shared annotation store's annotation sets.
sharedAnnotationStore.annotationSets = annotationSets;