相关文章推荐
稳重的肉夹馍  ·  C# ...·  1 年前    · 
酒量大的乒乓球  ·  WPF Canvas, how to ...·  1 年前    · 
爱旅游的保温杯  ·  node.js - nodejs + ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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, 2010 at 18:42 Well, hmm, last time I downloaded jPod it was free. I guess I need to check out what happened... user319799 May 16, 2010 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. user319799 May 16, 2010 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, 2010 at 19:08

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
                Unfortunately I don't think PDF Renderer will be able to get the job done. I've come up with a solution using Ghostscript on the command line instead, but +1 for a thorough answer.
– Shaggy Frog
                May 20, 2010 at 1:20
                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, 2010 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, 2010 at 7:28
                "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, 2011 at 11:12

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);

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);

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 - 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 5, 2022 at 19:06
                @B-Wieg, no I haven't seen that error before. But if you file an issue for this at 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, 2022 at 20:52
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.