Create thumbnail image for PDF in Java

前端 未结 3 1736
后悔当初
后悔当初 2020-11-29 02:46

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 licen

相关标签:
3条回答
  • 2020-11-29 02:56

    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.

    0 讨论(0)
  • 2020-11-29 03:03

    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
                    );
    
    0 讨论(0)
  • 2020-11-29 03:08

    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);
    
    0 讨论(0)
提交回复
热议问题