Appearance Streams

An annotation in PDF can specify appearance stream which allows the annotation to be rendered differently than its default annotation rendering. Each appearance stream is a self-contained PDF object that is to be rendered inside the annotation rectangle. Learn more in the blog post: What Are Appearance Streams?

PSPDFKit allows you to set an appearance stream to any annotation by setting AppearanceStreamGenerator for your Annotation's via setAppearanceStreamGenerator() method.

Appearance Stream Generator

We provide basic implementations of the AppearanceStreamGenerator interface:

⚠️  Note: Setting an appearance stream generator does not automatically remove existing appearance stream. The appearance stream only gets generated when the annotation has been modified or when calling generateAppearanceStream() on Annotation explicitly.

Annotation with an Appearance Stream Generator

Here's how to programmatically add a vector stamp annotation:

Copy
1
2
3
4
5
6
7
8
// Create stamp annotation. You'll need to specify annotation's page, bounding box in PDF coordinates and custom subject.
val stampAnnotation = StampAnnotation(pageIndex, RectF(300f, 500f, 500f, 300f), "Stamp with custom AP stream")

// Create appearance stream generator from PDF containing vector logo stored in assets.
stampAnnotation.appearanceStreamGenerator = AssetAppearanceStreamGenerator("PSPDFKit Logo.pdf")

// Add the newly created annotation to the document.
document.getAnnotationProvider().addAnnotationToPage(stampAnnotation)
Copy
1
2
3
4
5
6
7
8
// Create stamp annotation. You'll need to specify annotation's page, bounding box in PDF coordinates and custom subject.
StampAnnotation stampAnnotation = new StampAnnotation(pageIndex, new RectF(300f, 500f, 500f, 300f), "Stamp with custom AP stream");

// Create appearance stream generator from PDF containing vector logo stored in assets.
stampAnnotation.setAppearanceStreamGenerator(new AssetAppearanceStreamGenerator("PSPDFKit Logo.pdf"));

// Add the newly created annotation to the document.
document.getAnnotationProvider().addAnnotationToPage(stampAnnotation);

For a complete example, take a look at AnnotationCreationActivity inside the Catalog app, which shows how to create vector stamp annotations programmatically using an appearance stream generator.

You can also refer to our Default Stamp Annotations guide, which illustrates how to customize the default stamp annotations available in the stamp picker dialog.

Global Appearance Stream Generators

Custom appearance streams generated via AppearanceStreamGenerator are saved into the document when saving. However, appearance streams need to be regenerated whenever annotations are modified after the document is reloaded.

It can be inconvenient to set appearance stream generators for all annotations that need to have custom appearances. A cleaner approach is to add global appearance stream generators via addAppearanceStreamGenerator() on AnnotationProvider. Global appearance stream generators added via this method have a chance to override appearance stream generation for all annotations in the document.

Your custom appearance stream generator will be asked whether it wishes to generate an annotation's appearance stream via shouldUseGeneratorForAnnotation(). When true, you are expected to return a DataProvider instance to a PDF that should be used as the annotation's appearance stream, when getDataProviderForAnnotation() is called.