Hiding Annotations on iOS

By default, PSPDFKit will render all known annotation types. You can find a full list of the available types here.

There are two APIs for working with annotation types in PSPDFKit:

There isn’t always a 1:1 mapping between Annotation.Kind and Annotation.Tool. For example, Annotation.Tool.signature can create both Annotation.Kind.ink and Annotation.Kind.stamp. Additionally, Annotation.Tool.eraser and Annotation.Tool.selectionTool don’t create annotations at all.

Hiding Specific Annotations

Annotations can be hidden using the hidden flag. We provide a convenience isHidden property wrapping this flag, which you can set like this:

annotation.isHidden = true

Annotation flags are saved in the document following the PDF specification.

Hiding Annotations of Specific Types

The renderAnnotationTypes property controls which annotations are rendered. It’s an option set, and it defaults to Annotation.Kind.all.

For example, to enable all annotations except audio, image, and video annotations, you can set the following:

var annotationTypes = Annotation.Kind.all
annotationTypes.remove(.screen)
annotationTypes.remove(.richMedia)
annotationTypes.remove(.sound)
document.renderAnnotationTypes = annotationTypes
document.renderAnnotationTypes = PSPDFAnnotationTypeAll & ~(PSPDFAnnotationTypeScreen|PSPDFAnnotationTypeRichMedia|PSPDFAnnotationTypeSound|PSPDFAnnotationTypeStamp);
Information

Images are rendered as stamps, but there’s no simple way to differentiate between a text stamp (e.g. Approved) and an image stamp.

To only allow highlights and drawings to be rendered, configure the document like this:

document.renderAnnotationTypes = [.highlight, .ink]
document.renderAnnotationTypes = PSPDFAnnotationTypeHighlight|PSPDFAnnotationTypeInk;

Configure this property before the document is presented in a PDFViewController. If you change it afterward, either call reloadData() or, as a faster and more fine-grained approach, call update() on all visible page views:

for pageView in pdfController.visiblePageViews {
    pageView.update()
}
[pdfController.visiblePageViews makeObjectsPerformSelector:@selector(updateView)];

When changing the visible annotations after a document has already been rendered — either in a previous session or when the document is presented right away — you need to clear the cache for this document by calling PDFCache.remove(for:).

Disabling All Annotations

With the annotationsEnabled property on Document, annotations can be completely disabled. Note that links are also annotations, and you almost never want to disable links. There are, however, valid use cases, so this is an option. Changing this also requires a call to reloadData() if the document is already displayed. When enabling or disabling annotations after the document has already been rendered — either in a previous session or when the document is presented right away — you need to clear the cache for this document by calling PDFCache.remove(for:).