Create thumbnail image for PDF in Java

那年仲夏 提交于 2019-12-27 20:15:34

问题


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.


回答1:


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



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/2844961/create-thumbnail-image-for-pdf-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!