How to Render a PDF Document to Bitmap Image on Android

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.

Render a PDF  to Bitmap Image on Android

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.

First, you need to create a bitmap object with a given WIDTH and HEIGHT.

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 PASSWORD.

Finally, call the drawPage function to draw a page into the bitmap object.

If you need to get a dimension for a page, use pageSize function in PDFDocument class.

Voila!

15 replies
  1. Luke
    Luke says:

    PlugPDFCore core = new PlugPDFCore(PATH, PASSWORD);

    just throws an exception “cannot open file”

    I can open this pdf in other apps (Polaris Office for example)

    Reply
    • admin
      admin says:

      Hi Luke,

      Did you try with other PDF files? If the problem only occurs with a specific PDF files, sending the PDF file to us will help to solve the issue.
      Thanks!

      Jeong

      Reply
  2. Takayuki
    Takayuki says:

    Hi

    This API happens SIGSEGV in my application.
    I want to draw my PDF of top page in assets like thumbnail.
    But it crashes at drawPage API.
    Here is logs of Android4.1.2(3.2also)

    I/DEBUG ( 243): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000048
    I/DEBUG ( 243): backtrace:
    I/DEBUG ( 243): #00 pc 0009b620 /data/data/com.mypackage.mydemo/lib/libplugpdf-jni.so (Pdf_Document::getPage(int)+15)
    I/DEBUG ( 243): #01 pc 00078b7f /data/data/com.mypackage.mydemo/lib/libplugpdf-jni.so (Java_com_epapyrus_plugpdf_PlugPDFCore_gotoPageInternal+70)
    I/DEBUG ( 243):

    And this is my code.
    It’s in adapter class for gridview.

    final String PASSWORD = “”;
    core = new PlugPDFCore(mData[position], mDataSize[TEMP_POS], PASSWORD);
    Bitmap bitmap = Bitmap.createBitmap(1280, 800, Bitmap.Config.ARGB_8888);
    final int pageIdx = 0;
    final int pageW = 1280;
    final int pageH = 800;
    final int patchX = 0;
    final int patchY = 0;
    final int patchW = 200;
    final int patchH = 141;
    core.drawPage(pageIdx, bitmap, pageW, pageH, patchX, patchY, patchW, patchH);

    Data, datasize, core and bitmap are not null.
    This data could draw using SimpleDocumentViewer;
    I tried to point to “core” in debugger.
    m_filePath=null
    m_pageCount=null
    m_pageHeight=0.0
    m_pageIdx=-1
    m_pageWidth=0.0

    SIGSEGV happened again when I used other constructor (new PlugPDFCore(PATH, PASSWORD)).

    Please give me advice.
    Thanks.

    Reply
    • Takayuki
      Takayuki says:

      Sorry I solved myself.
      But I don’t know why.

      I only wrote this code after constructor, but It worked.

      Log.d(TAG, “pageCount:” + core.pageCount());

      Is it bug?
      Thanks anyway.

      Reply
      • admin
        admin says:

        Thank you For your question.

        We have checked It And found a bug in product.

        This week we plan to release in the 2.3.2 version with fix it

        Thank you. Mr Takayuki 🙂

        Reply
  3. coconut oil for skin acne
    coconut oil for skin acne says:

    as soon as I began reading the 1st sentence, I believed this merely destined to be another boring
    article. But WOW, now that I am done, I have so many more magnificent insights I would not have had
    before. Thank you!

    Reply
  4. CJ Scholten
    CJ Scholten says:

    I’m trying to get this to work in Android Studio, but it can’t resolve PlugPDFCore.
    I believe I have all the neccesary imports. Am I missing something?
    Is it possible to supply the entire project, instead of just 3 lines of code?

    Reply
  5. Soham
    Soham says:

    I keep getting an exception did not yet try to open the document. trying to do this:

    File pdfFile = new File(pdfPath);
    FileInputStream pdfFileStream = new FileInputStream(pdfFile);
    byte[] b = new byte[(int) pdfFile.length()];
    pdfFileStream.read(b);
    pdfFileStream.close();

    Any help please?

    Reply
    • Dr. Plug
      Dr. Plug says:

      Hi Soham,

      Thanks very much for working with our PlugPDFSDK. We will quickly test your provided code and then we will reach out to you with a remedy. We’ll be in touch very shortly.

      Thanks again,

      Dr. Plug

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *