Rendering Annotations

PSPDFKit offers several options for rendering a single annotation object. This article serves as a step-by-step guide to get you started quickly.

ℹ️ Note: Before loading a document and rendering its annotations, you have to initialize PSPDFKit by providing your license. Make sure to follow the steps in our license integration guide before continuing with this guide, in order to have PSPDFKit fully initialized.


Loading a Document

The first step in rendering annotations of a PDF is to load the document in memory. The PSPDFDocument class offers a variety of methods for doing this. The following example loads a PDF document from the app's Samples directory using -[PSPDFDocument initWithURL:]:

Copy
1
2
3
let samplesURL = Bundle.main.resourceURL?.appendingPathComponent("Samples")
let documentURL = samplesURL?.appendingPathComponent("document.pdf")
let document = PSPDFDocument(url: documentURL)
Copy
1
2
3
NSURL *samplesURL = [NSBundle.mainBundle.resourceURL URLByAppendingPathComponent:@"Samples"];
NSURL *documentURL = [samplesURL URLByAppendingPathComponent:@"document.pdf"];
PSPDFDocument *document = [[PSPDFDocument alloc] initWithURL:documentURL];

The complete list of available document initializers is provided in our PSPDFDocument initialization API reference.

Retrieving Annotations

To retrieve all the annotations of your loaded PSPDFDocument instance, call [-[PSPDFDocument allAnnotationsOfType:]. For example, you can retrieve all ink annotations from a document, like so:

1
2
// Retrieves all ink annotations of the document.
let annotations = document.allAnnotations(of: .all).values.flatMap { $0 }
Copy
1
2
3
4
5
// Retrieves all ink annotations of the document.
NSMutableArray<PSPDFAnnotation *> *inkAnnotations = [NSMutableArray<PSPDFAnnotation *> array];
for (NSArray<PSPDFAnnotation *> *annotations in [document allAnnotationsOfType:PSPDFAnnotationTypeInk].allValues) {
    [inkAnnotations addObjectsFromArray:annotations];
}

You can also query annotations from a specific page using -[PSPDFDocument annotationsForPageAtIndex:type:]:

Copy
1
2
3
// Retrieves all ink annotations of a given page index of the loaded document.
let pageIndex = ...
let inkAnnotations = document.annotationsForPage(at: pageIndex, type: .ink)
Copy
1
2
3
// Retrieves all ink annotations of a given page index of the loaded document.
NSUInteger pageIndex = ...
NSMutableArray<PSPDFAnnotation *> *inkAnnotations = [document annotationsForPageAtIndex:pageIndex type:PSPDFAnnotationTypeInk];

💡 Tip: If you want to retrieve all of your document's annotations at once, you can pass PSPDFAnnotationTypeAll.

Rendering Annotations

You can render an annotation as a UIImage using -[PSPDFAnnotation imageWithSize:withOptions:]:

1
2
// Render annotation as a UIImage
let renderedImage = annotation.image(with: CGSize(width: 200, height: 200), withOptions: nil)
1
2
// Render annotation as a UIImage
UIImage *renderedImage = [annotation imageWithSize:CGSizeMake(200.f, 200.f) withOptions:nil];

ℹ️ Note: The bounding box is using PDF coordinates, which, unlike UIKit's coordinates, are vertically flipped.