Detecting if Annotations Have Changed


PSPDFKit allows you to edit and create annotations, if your license includes this feature. You can detect changes to the underlying data models by listening to the following notifications:

Copy
1
2
3
4
5
6
7
/// Sent when new annotations are added to/removed from the default `PSPDFFileAnnotationProvider`.
public let PSPDFAnnotationsAddedNotification: String // object = array of new `PSPDFAnnotation(s)`.
public let PSPDFAnnotationsRemovedNotification: String // object = array of removed `PSPDFAnnotation(s)`.

/// Internal events to notify the annotation providers when annotations are being changed.
/// @warning Only send from main thread! Don't call save during a change notification.
public let PSPDFAnnotationChangedNotification: String // object = new `PSPDFAnnotation`.
Copy
1
2
3
4
5
6
7
/// Sent when new annotations are added to/removed from the default `PSPDFFileAnnotationProvider`.
PSPDF_EXPORT NSString *const PSPDFAnnotationsAddedNotification;    // object = array of new `PSPDFAnnotation(s)`.
PSPDF_EXPORT NSString *const PSPDFAnnotationsRemovedNotification;  // object = array of removed `PSPDFAnnotation(s)`.

/// Internal events to notify the annotation providers when annotations are being changed.
/// @warning Only send from main thread! Don't call save during a change notification.
PSPDF_EXPORT NSString *const PSPDFAnnotationChangedNotification;   // object = new `PSPDFAnnotation`.

Here's an example of listening to changes of the contents in the note annotation controller:

Copy
1
2
3
4
5
6
7
8
9
func annotationChangedNotification(notification: Notification) {
    // Check if we need to re-render ourselves.
    guard let changedAnnotation = notification.object as? PSPDFAnnotation else { return }
    if annotation == changedAnnotation, let keyPaths = notification.userInfo?[PSPDFAnnotationChangedNotificationKeyPathKey] as? [String] {
        if keyPaths.count > 1 || keyPaths.first != "contents" {
            updateImage(animated: true)
        }
    }
}
Copy
1
2
3
4
5
6
7
8
9
- (void)annotationChangedNotification:(NSNotification *)notification {
    // Check if we need to re-render ourselves.
    if ([self.annotation isEqual:notification.object]) {
        NSArray *keyPaths = notification.userInfo[PSPDFAnnotationChangedNotificationKeyPathKey];
        if (keyPaths.count > 1 || ![keyPaths.firstObject isEqual:@"contents"]) {
            [self updateImageAnimated:YES];
        }
    }
}

In PSPDFKit, annotations are usually created using the PSPDFAnnotationStateManager. If you create a drawing, the state manager is in the draw mode and will cache drawn lines until you exit the draw state, which will, in turn, cause a PSPDFInkAnnotation object to be created. After creation, an undo operation will remove the whole annotation - but while you're in the drawing mode, undo/redo operates on a finer level, affecting one stroke at a time.

If you change annotations, make sure to also post appropriate change notifications. Read The annotation object model for details.

Was this page helpful? We're happy to answer any questions.