开发者

Create thumbnail image for PDF in Java

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.


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜