Annotation Z-Index

The annotation z-index defines the stacking order of annotations on a page. An annotation with a lower z-index will be positioned at the back of the page, whereas annotations with a higher number will be positioned on top of annotations with a lower z-index. This means the annotations with a lower z-index will be obscured if the annotations are overlapping.

A z-index of 0 means that the annotation is all the way at the back, while the highest z-index means that the annotation is at the front.

By default, when creating and adding new annotations, the new annotations will be added on top of existing annotations.

Determine the Z-Index of an Annotation

The z-index is not a property on an annotation, but it is defined by the index in the annotations array of a page. So, to determine the current z-index of an annotation, you need to fetch all annotations for a page and get the index of the annotation in question in the array.

Change the Z-Index of an Annotation

The z-index is not a property on an annotation, but it is defined by the index in the annotations array of a page. To determine the current z-index of an annotation, you need to call [AnnotationProvider#getZIndex(Annotation)][].

User Interface

The z-index can be changed via the annotation inspector and the annotation list. This UI feature can be disabled by setting allowAnnotationZIndexMoves of PSPDFConfiguration to NO.

Annotation List

The moving of annotations is also supported by reordering them in the annotation list while in edit mode. This can be disabled via the allowAnnotationZIndexMoves property on PSPDFAnnotationTableViewController. This property is set to the value of PSPDFConfiguration.allowAnnotationZIndexMoves whenever PSPDFAnnotationTableViewController is presented from within PSPDFKit.

Inspector

When an annotation supports changing the z-index (when there are at least two annotations on a page), PSPDFAnnotationStyleViewController shows an Order entry, where the annotation can be moved to the front, one position forward, one position backward, or to the back.

Programmatically

You can programmatically move annotations by calling updateAnnotationsOnPageAtIndex:error:withUpdateBlock: on PSPDFAnnotationManager and using moveAnnotationAtZIndex:toZIndex:error: on the object provided in the update block. This will move an annotation at the specified z-index to another z-index.

To move an annotation all the way to the back (to z-index 0), you can use the following code snippet:

Copy
1
2
3
4
5
6
7
let annotation: PSPDFAnnotation = // Annotation to move.

guard let annotationManager = document.documentProviderForPage(at: annotation.pageIndex)?.annotationManager else { return }
annotationManager.updateAnnotationsOnPage(at: annotation.pageIndex, withUpdateBlock: { annotationUpdate in
    let currentZIndex = annotationUpdate.annotations.firstIndex(of: annotation)
    annotationUpdate.moveAnnotation(atZIndex: currentZIndex, toZIndex: 0)
})
Copy
1
2
3
4
5
6
7
PSPDFAnnotation *annotation = // Annotation to move.

PSPDFAnnotationManager *annotationManager = [document documentProviderForPageAtIndex:annotation.pageIndex];
[annotationManager updateAnnotationsOnPageAtIndex:annotation.pageIndex error:nil withUpdateBlock:^(id<PSPDFAnnotationUpdate> annotationUpdate) {
    NSUInteger currentZIndex = [annotationUpdate.annotations indexOfObjectIdenticalTo:annotation];
    [annotationUpdate moveAnnotationAtZIndex:currentZIndex toZIndex:0 error:nil];
}];

Add a New Annotation at a Specific Z-Index

When adding a new annotation, you can also specify the z-index it should be inserted at. This can be done with the insertAnnotation:atZIndex:options: API on PSPDFDocument.

Considerations

Currently, z-index moving is unsupported when using multiple annotation providers.

Also bear in mind that when specifying the z-index, either when inserting or when moving annotations, it always needs to be less than the number of annotations that will be on the document page.

There are a few annotation types that always float on top of other annotations, since they are not drawn on the PDF page but are overlaid as views, and therefore don’t respect the z-index. These annotations are note, sound, and link annotations.

Selected annotations are always rendered on top, so if you change the z-index of the selected annotation via the inspector, the result will not become apparent before deselecting.

Some annotation types, like stamps, don’t have any editable properties other than the z-index, so we decided not to show the inspector for these annotations. These annotations must be moved using the annotation list.