An annotation in a PDF can specify an 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 about this in the related blog post: What Are Appearance Streams?)
Appearance Stream Generator
We provide basic implementations of the
AssetAppearanceStreamGeneratorgenerates an appearance stream from the first page of a PDF stored in your app’s assets.
ContentResolverAppearanceStreamGeneratorgenerates an appearance stream from the first page of a PDF stored in Android’s
ℹ️ Note: Setting an appearance stream generator does not automatically remove the existing appearance stream. The appearance stream only gets generated when the annotation has been modified or when calling
Annotation with an Appearance Stream Generator
Here’s how to programmatically add a vector stamp annotation:
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 an appearance stream generator from a PDF containing a vector logo stored in assets. stampAnnotation.appearanceStreamGenerator = AssetAppearanceStreamGenerator("PSPDFKit Logo.pdf") // Add the newly created annotation to the document. document.getAnnotationProvider().addAnnotationToPage(stampAnnotation)
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 an appearance stream generator from a PDF containing a 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
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
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.