用 Java 为 PDF 创建缩略图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2844961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:32:28  来源:igfitidea点击:

Create thumbnail image for PDF in Java

javapdfthumbnailsimage-generation

提问by Shaggy Frog

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

我正在寻找一个 Java 库,它可以获取 PDF 并从第一页创建缩略​​图(PNG)。

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.

我已经看过 JPedal,但其疯狂的许可费完全令人望而却步。我目前正在使用 iText 来操作 PDF 文件,但我相信它不会生成缩略图。我可以在命令行上使用类似 Ghostscript 的东西,但如果可能的话,我希望我的项目全是 Java。

采纳答案by FRotthowe

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

PDF Renderer是一个 LGPL 许可的纯 Java 库,它使它变得如此简单(取自他们的示例页面):

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

回答by mark stephens

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.

PDF Renderer 很好,只要您只使用他们使用的 PDF 文件的子集。使用 JPod 和 JPedal,您是为一个活跃的和开发的库而不是一个死项目付费。

回答by Rahul

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

在适配器中创建多个 PDF 文件的缩略图,就像使用 Picasso 或 Glide 加载图像一样您需要集成picasso 库

After that

在那之后

You need to create PdfRequestHandlerclass :-

您需要创建PdfRequestHandler类:-

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

然后从适配器的 bindViewHolder 方法中的路径加载文件

picassoInstance.load(PdfRequestHandler.SCHEME_PDF+":"+filePath)
               .fit()
               .into(holder.pdfThumbnailImageView);