Inserting an Image into a PDF on Android
The PDF file format is extremely versatile, and it allows you to include many different kinds of content in your documents. Today we’ll take a look at how to embed images into existing documents using our Android PDF Library.
There are three ways to add images to a document when using PSPDFKit. One option is to append an image as an annotation with the help of our Annotations component. The second option is to use the Processor API with the help of our Document Editor component. The third option — which is also available with Document Editor — is to use the Document Editor API.
We cover the first two options in this post. Let’s start by looking at the simplest way: adding images as annotations.
Adding Images Using Annotations
Using an annotation to include an image in a PDF is a straightforward operation. Simply create a StampAnnotation
with your image set and add it to the document:
// First, you need to load your bitmap. val image = BitmapFactory.decodeStream(assets.open("images/android.png")) // Then, you can create a `StampAnnotation` based on it. val imageStamp = StampAnnotation( 0, // Use the image size to calculate the correct bounding box. RectF(60f, 400f, (60 + image.width / 4).toFloat(), (400 - image.height / 4).toFloat()), image ) // Finally, add it to the document. document.annotationProvider.addAnnotationToPage(imageStamp)
In the above example, the image comes from the application assets, but the BitmapFactory
supports decoding bitmaps from other sources as well, including files, Android resources, and streams.
If you want to make the annotation a permanent part of the document, you can use PdfProcessor#fromDocument
to create a PdfProcessorTask
. This is an operation that will be executed on the document. Among other things, it allows you to change annotations via the changeAnnotations
method. You’ll use this method to flatten the image into the document (make it non-editable):
// Create the `PdfProcessorTask` based on the current document. val task = PdfProcessorTask.fromDocument(document) // Tell the processor to flatten the singular image stamp. This keeps all other annotations editable. task.changeAnnotations(listOf(imageStamp), PdfProcessorTask.AnnotationProcessingMode.FLATTEN) // Finally, write it to a new document. PdfProcessor.processDocument(task, File("/sdcard/processed.pdf"))
If you now open the produced file, you’ll see it includes your image, but it can no longer be moved or otherwise be edited. This is because you flattened it into the document using PdfProcessorTask.AnnotationProcessingMode.FLATTEN
.
Next, let’s look at how to use PdfProcessor
directly to embed an image without having to create an annotation first.
Adding Images Using PdfProcessor
PdfProcessor
provides an API that allows you to attach a PageCanvas
to existing pages, which then lets you to draw your content on top of them. You can use this to permanently add an image on top of the page:
// Create the `PdfProcessorTask` based on the current document. val task = PdfProcessorTask.fromDocument(document) // Then load the bitmap. val image = BitmapFactory.decodeStream(assets.open("images/android.png")) // The page size is necessary for generating the `PageCanvas` to draw on. val pageSize = document.getPageSize(0) // Now obtain a canvas to draw the bitmap on. task.addCanvasDrawingToPage(PageCanvas(pageSize, NewPage.OnDrawCanvasCallback { // Draw the image onto the canvas. it.drawBitmap(image, 60f, 60f, null) }), 0) // Finally, write it to a new document. PdfProcessor.processDocument(task, File("/sdcard/processed.pdf"))
You still load the bitmap like before, but now instead of creating a StampAnnotation
, you draw it on the Canvas
you obtained from the PageCanvas
. You can also draw anything else you desire in this callback — including text and shapes — and they’ll be included in the final PDF.
Conclusion
And there you have it: two great ways to include images using PSPDFKit for Android. You can do more with our SDK by leveraging other components. For example, combine:
-
Document Editor and Image Documents to easily add multiple image files to one PDF file.
-
Image Documents and Annotations to work with and mark up images as you would PDFs.
No matter what your use case is, if it involves PDF, Office, or image documents, PSPDFKit is the right choice. We offer a commercial, feature-rich, and completely customizable Android PDF library that’s easy to integrate and comes with well-documented APIs. Try our library using our free trial and check out our demos to see what’s possible.