Generate Thumbnail of Pdf in Android

shanraisshan

I want to generate the image(thumbnail) from pdf file just like done by WhatsApp as shown below WhatsApp

I have tried

  1. PDFBox (https://github.com/TomRoush/PdfBox-Android)
  2. Tika (compile 'org.apache.tika:tika-parsers:1.11')
  3. AndroidPdfViewer (https://github.com/barteksc/AndroidPdfViewer)

and still unable to find a way to generate image from pdf.


PDFBox:

There is a github issue that deals with this problem (https://github.com/TomRoush/PdfBox-Android/issues/3) but this is still unresolved.

Note: I am successfully able to extract image from PDF using PDFBOX


AndroidPdfViewer:

Github issue (https://github.com/barteksc/AndroidPdfViewer/issues/49)

user6691016

Use PdfiumAndroid as mentioned by barteksc here...

Sample Code for generating pdf thumb

//PdfiumAndroid (https://github.com/barteksc/PdfiumAndroid)
//https://github.com/barteksc/AndroidPdfViewer/issues/49
void generateImageFromPdf(Uri pdfUri) {
    int pageNumber = 0;
    PdfiumCore pdfiumCore = new PdfiumCore(this);
    try {
        //http://www.programcreek.com/java-api-examples/index.php?api=android.os.ParcelFileDescriptor
        ParcelFileDescriptor fd = getContentResolver().openFileDescriptor(pdfUri, "r");
        PdfDocument pdfDocument = pdfiumCore.newDocument(fd);
        pdfiumCore.openPage(pdfDocument, pageNumber);
        int width = pdfiumCore.getPageWidthPoint(pdfDocument, pageNumber);
        int height = pdfiumCore.getPageHeightPoint(pdfDocument, pageNumber);
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        pdfiumCore.renderPageBitmap(pdfDocument, bmp, pageNumber, 0, 0, width, height);
        saveImage(bmp);
        pdfiumCore.closeDocument(pdfDocument); // important!
    } catch(Exception e) {
        //todo with exception
    }
}

public final static String FOLDER = Environment.getExternalStorageDirectory() + "/PDF";
private void saveImage(Bitmap bmp) {
    FileOutputStream out = null;
    try {
        File folder = new File(FOLDER);
        if(!folder.exists())
            folder.mkdirs();
        File file = new File(folder, "PDF.png");
        out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
    } catch (Exception e) {
        //todo with exception
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (Exception e) {
            //todo with exception
        }
    }
}

Update:

Include library in build.gradle

compile 'com.github.barteksc:pdfium-android:1.4.0'

For generating Image of any PDF Page:

Call the method generateImageFromPdf(uri) by passing any PDF uri that is stored in your storage.

The method will generate the PDF.png in PDF folder of your storage.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create thumbnail image for PDF in Android

From Dev

Create thumbnail image for PDF in Android

From Dev

Is it possible to Generate a thumbnail from a video url in android

From Dev

Ionic generate pdf in iOS and android

From Dev

ffmpeg cannot generate thumbnail

From Dev

Generate thumbnail images dynamically into bxslider

From Dev

Generate thumbnail images dynamically into bxslider

From Dev

FileNotFoundException when try to generate PDF in android

From Dev

android chat thumbnail position

From Dev

android chat thumbnail position

From Dev

Rails paperclip generate thumbnail from other style

From Dev

can't generate a thumbnail of an image using carrierwave

From Dev

Python generate thumbnail with transparancy and fixed width

From Dev

React Native generate thumbnail for video url

From Dev

Android ZXing Generate PDF417 Barcode on large XML dataset

From Dev

generate PDF from Android using PrintedPdfDocument and View.draw

From Dev

Generate Pdf from Html string with image and css for android using itext

From Dev

Create thumbnail of PDF or PPT file using php

From Dev

Create PDF thumbnail with Imagick and write to file

From Dev

Get wrong thumbnail of PDF using imageFromPDFWithDocumentRef

From Dev

PHP Imagick PDF thumbnail creation not working

From Dev

Unable to generate thumbnail generate thumbnails from phpThumb in controller in CakePHP

From Dev

How to get video thumbnail in android?

From Dev

Android JSON access thumbnail element

From Dev

How to retrieve the video thumbnail in Android

From Dev

Android - Thumbnail from video file

From Dev

PDF Thumbnail Preview in Windows Explorer without installed PDF Preview Handler?

From Dev

Generate PDF with Swift

From Dev

Generate PDF for IOS in Xamarin

Related Related

  1. 1

    Create thumbnail image for PDF in Android

  2. 2

    Create thumbnail image for PDF in Android

  3. 3

    Is it possible to Generate a thumbnail from a video url in android

  4. 4

    Ionic generate pdf in iOS and android

  5. 5

    ffmpeg cannot generate thumbnail

  6. 6

    Generate thumbnail images dynamically into bxslider

  7. 7

    Generate thumbnail images dynamically into bxslider

  8. 8

    FileNotFoundException when try to generate PDF in android

  9. 9

    android chat thumbnail position

  10. 10

    android chat thumbnail position

  11. 11

    Rails paperclip generate thumbnail from other style

  12. 12

    can't generate a thumbnail of an image using carrierwave

  13. 13

    Python generate thumbnail with transparancy and fixed width

  14. 14

    React Native generate thumbnail for video url

  15. 15

    Android ZXing Generate PDF417 Barcode on large XML dataset

  16. 16

    generate PDF from Android using PrintedPdfDocument and View.draw

  17. 17

    Generate Pdf from Html string with image and css for android using itext

  18. 18

    Create thumbnail of PDF or PPT file using php

  19. 19

    Create PDF thumbnail with Imagick and write to file

  20. 20

    Get wrong thumbnail of PDF using imageFromPDFWithDocumentRef

  21. 21

    PHP Imagick PDF thumbnail creation not working

  22. 22

    Unable to generate thumbnail generate thumbnails from phpThumb in controller in CakePHP

  23. 23

    How to get video thumbnail in android?

  24. 24

    Android JSON access thumbnail element

  25. 25

    How to retrieve the video thumbnail in Android

  26. 26

    Android - Thumbnail from video file

  27. 27

    PDF Thumbnail Preview in Windows Explorer without installed PDF Preview Handler?

  28. 28

    Generate PDF with Swift

  29. 29

    Generate PDF for IOS in Xamarin

HotTag

Archive