This time we are going to learn how to convert a PDF page to a bitmap image on the Android platform. Since Android has no built-in API support for PDF we have to use a third-party library for this, and naturally, we’ll use PlugPDF in today’s post.
Good news for indies! Indie developers can use PlugPDF completely free without any functional restriction. So if you are an indie, download and setup PlugPDF SDK right now and have a look at the step by step guide & video tutorial available:
Follow the Developer’s Guide and go to section 6. How to USE PlugPDF – 3. From 6-4, the tutorial will walk you through creating a PDF reader, rather than rendering a PDF page to an image.
Now we are ready to do the job. The following is the code snippet for drawing a PDF page into a bitmap.
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888);
PDFDocument doc = new PDFDocument(PATH, PASSWORD);
doc.drawPage(PAGE_IDX, bitmap, PAGE_WIDTH, PAGE_HEIGHT, CLIP_X, CLIP_Y, CLIP_WIDTH, CLIP_HEIGHT);
First, you need to create a
bitmap object with a given
Next, create a
PDFDocument object. You don’t need to create a viewer instance such as a
SimpleDocumentViewer here as you only want to render a PDF page to image. If there is no password for your PDF, you can pass “” for
PASSWORD. Or a password string for the place of
Finally, call the
drawPage function to draw a page into the
If you need to get a dimension for a page, use
pageSize function in