24

I'm looking for a Java library that will can take a PDF and create a thumbnail image (PNG) from the first page.

I've already looked at JPedal, but its insane licensing fee is completely prohibitive. I am using iText to manipulate PDF files at the moment, but I believe it doesn't do thumbnail generation. I can use something like Ghostscript on the command line, but I'm hoping to keep my project all-Java if possible.

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • See if [jPod](http://sourceforge.net/projects/jpodlib/) can do that, I'd bet it can. (Not an answer, as it is just a suggestion.) –  May 16 '10 at 18:38
  • Looking at jPod now. I'd prefer a free-open source solution if possible, I guess. Even 99 Euros is too much to spend if I can avoid it. Can't easily find an API for it, either. – Shaggy Frog May 16 '10 at 18:42
  • Well, hmm, last time I downloaded jPod it was free. I guess I need to check out what happened... –  May 16 '10 at 18:45
  • As far as I understand, jPod itself is under BSD license (so is free). You probably confused it with some their products built on top, which are probably not free. Anyway, it's certainly not a ready solution and I see one already proposed below, so scratch jPod for this. –  May 16 '10 at 18:54
  • It wasn't really clear to me on the English version of the site. If jPod is distributed under a BSD license then that sounds good. The question is, does jPod do what I need? – Shaggy Frog May 16 '10 at 19:08
  • Not directly. It is basically a good library that contains PDF framework, but to do anything real, you still need some code on top. We now use it for text indexation of PDF files and for this it is vastly superior than Apache PDFBox, at least in current state of affairs. –  May 16 '10 at 19:18
  • So apparently jPod doesn't do PDF thumbnail generation... apparently "jPodRenderer" does, and it costs money for a commercial licence. I'm going to pursue PDF Renderer for now. – Shaggy Frog May 17 '10 at 20:23
  • See also http://stackoverflow.com/questions/4392640/thumbnail-of-a-pdf-page-java which references PDFBox and icePDF. – JasonPlutext Oct 10 '13 at 21:56

5 Answers5

23

PDF Renderer is a LGPL licensed pure-java library that makes this as simple as (taken from their example page):

File file = new File("test.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);

// draw the first page to an image
PDFPage page = pdffile.getPage(0);

//get the width and height for the doc at the default zoom 
Rectangle rect = new Rectangle(0,0,
                (int)page.getBBox().getWidth(),
                (int)page.getBBox().getHeight());

//generate the image
Image img = page.getImage(
                rect.width, rect.height, //width & height
                rect, // clip rect
                null, // null for the ImageObserver
                true, // fill background with white
                true  // block until drawing is done
                );
Simon Sobisch
  • 6,263
  • 1
  • 18
  • 38
FRotthowe
  • 3,662
  • 25
  • 31
5

PDF Renderer is fine so long as you only use the subset of PDF files they use. With JPod and JPedal you are paying for an active and developed library not a dead project.

mark stephens
  • 3,205
  • 16
  • 19
  • My solution needs to work with arbitrary PDF files. Can you explain more by "only the subset [PDF Render] uses"? Re: JPedal, I've already dismissed it due to its ridiculous cost; re: JPod, I'm still not sure if it will do what I need above. – Shaggy Frog May 17 '10 at 18:56
  • PDF Renderer does not support compressed objects and a number of other features in a large number of current PDFs. – mark stephens May 18 '10 at 07:28
  • 2
    "PDFRenderer only deals with up to version 1.4 of the PDF spec. The current version is 1.6, and there have been quite a few additions and changes between 1.4 and 1.6 which seem to break PDFRenderer." (taken from an edit) (also cc @ShaggyFrog) – Marc Gravell Jan 24 '11 at 11:12
0

create Multiple PDF file's thumbnails in adapter as like images loading using Picasso or Glide You need to integrate picasso library

After that

You need to create PdfRequestHandler class :-

public class PdfRequestHandler extends RequestHandler{
    public static String SCHEME_PDF="pdf";
    @Override
    public boolean canHandleRequest(Request data) 
    {
        String scheme = data.uri.getScheme();
        return (SCHEME_PDF.equals(scheme));
    }

    @Override
    public Result load(Request data, int arg1) throws IOException 
    {
        ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(new File(data.uri.getPath()), MODE_READ_ONLY);
        PdfRenderer renderer = new PdfRenderer(fileDescriptor);
        final int pageCount = renderer.getPageCount();
        if(pageCount > 0){
            PdfRenderer.Page page = renderer.openPage(0);
            Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bitmap, 0, 0, null);
            page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
            page.close();
        return new Result(bm,LoadedFrom.DISK);
        }
        return null;     
    }
}

After That create instance in adapter

Picasso picassoInstance;

Initilize the instance in constructor of adapter

picassoInstance = new Picasso.Builder(context.getApplicationContext())
        .addRequestHandler(new PdfRequestHandler())
        .build();

Then load file from path in bindViewHolder method of adapter

picassoInstance.load(PdfRequestHandler.SCHEME_PDF+":"+filePath)
               .fit()
               .into(holder.pdfThumbnailImageView);
Rahul
  • 579
  • 1
  • 8
  • 18
0

Qoppa Software has a java SDK that can convert PDFs to images. https://www.qoppa.com/pdfimages/

//Export the first page in all three formats
pdfDoc.savePagesAsJPEG(0, "c:\\somefile.jpg",150,0.80f);
pdfDoc.savePagesAsTIFF(0, "c:\\somefile.jpg",150,TIFFCompression.TIFF_FAX_GROUP4));
pdfDoc.savePagesAsPNG(0, "c:\\somefile.jpg",150f);
Amber
  • 2,413
  • 1
  • 15
  • 20
0

Thumbnails4j (I'm a maintainer, but it's owned by Elastic) is an Apache 2 licensed library for creating thumbnails, and supports PDF inputs.

File input = new File("/path/to/my_file.pdf");
Thumbnailer thumbnailer = new PDFThumbnailer();
List<Dimensions> outputDimensions = Collections.singletonList(new Dimensions(100, 100));
BufferedImage output = thumbnailer.getThumbnails(input, outputDimensions).get(0);
Sean
  • 2,315
  • 20
  • 25
  • Sean - I tried using Thumbnails4j and it did generate the thumbnail, but I do see a warning in the console: Have you encountered that? I am using OpenJDK17, Spring Boot 2.6.7, Tomcat 9.... 2022-05-05 14:00:07.624 WARN 30628 --- [ Finalizer] org.apache.pdfbox.cos.COSDocument : Warning: You did not close a PDF Document – B-Wieg May 05 '22 at 19:06
  • @B-Wieg, no I haven't seen that error before. But if you file an issue for this at https://github.com/elastic/thumbnails4j/issues (with reproduction steps, environment, any other surrounding logs, etc) my team and I can take a look! – Sean May 31 '22 at 20:52