If you need to render single
annotation objects into
Bitmap instances, PSPDFKit offers several options for doing this. 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 Adding the License Key guide before continuing with this guide, in order to have PSPDFKit fully initialized.
Loading a Document
The first step in rendering annotations of a document is to load the document into memory. The
PdfDocumentLoader class offers a variety of static methods for doing this. The following example loads a PDF document from the app’s assets using
PdfDocumentLoader.openDocument() and a
file:///android_asset URI. The complete list of available document sources and ways to load them is outlined in our Using an Activity guide:
💡 Tip: The synchronous
PdfDocumentLoader.openDocument() method used in this example will throw an exception if there is an error while loading the document. You should wrap the call into
try/catch to handle any errors properly.
To retrieve the annotations of your loaded
PdfDocument instance, get a reference of your document’s
AnnotationProvider by calling
document.getAnnotationProvider(). You can then query annotations by page number or annotation type:
💡 Tip: If you want to retrieve all annotations of your document at once, you can call
The rendering of annotations can be performed synchronously or asynchronously using the
renderToBitmapAsync() methods of
Preparing a Bitmap
You first need to create a target
Bitmap instance, which is usually done using
Bitmap.createBitmap(). Since annotations can have transparent regions as well, it makes sense to use a bitmap format that supports this —
Bitmap.Config.ARGB_8888 is a good choice:
While rendering, the annotation will be stretched to fill the entire bitmap area. If you want to maintain the aspect ratio of the annotation, you need to create a bitmap with the same aspect ratio as the annotation.
Calculating the Bitmap Size
You can calculate the
Bitmap size from the annotation size stored within the bounding box of your annotation. To retrieve the bounding box, use
ℹ️ Note: The bounding box holds PDF coordinates, which, unlike Android’s coordinates, are vertically flipped. As such, the returned
height() is negative and has to be inverted.
To synchronously render an annotation into a
Bitmap instance, call
renderToBitmap() on your annotation, passing in the already prepared
⚠️ Warning: When calling
annotation.renderToBitmap(), the method will block until rendering has finished, so don’t use it on your main thread! Instead, use asynchronous rendering.
In most instances, it’s better to render an annotation asynchronously to ensure you do not block the current thread. This can be done by using
renderToBitmapAsync(), which can also be used on the main thread. The method returns
Single<Bitmap>, which will emit the
Bitmap once rendering is done:
💡 Tip: Rendering will only start once you call
Single<Bitmap>. Since rendering is asynchronous, you can cancel it at any time — for example, by calling
dispose() on the
Disposable that is returned by
Providing an Annotation Render Configuration
Both synchronous and asynchronous render methods allow you to specify an
AnnotationRenderConfiguration object for defining render options. You can create the configuration using its
Available Render Options
It is possible to specify several different render configuration options via the