用 Java 创建 PDF 的缩略图

发布于 2024-09-01 13:27:47 字数 183 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

香草可樂 2024-09-08 13:27:47

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

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
                );
风透绣罗衣 2024-09-08 13:27:47

只要您只使用 PDF 渲染器使用的 PDF 文件的子集,PDF 渲染器就可以。使用 JPod 和 JPedal,您是在为一个活跃且已开发的库而不是一个死项目付费。

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.

大海や 2024-09-08 13:27:47

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

之后

您需要创建 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;     
    }
}

之后在适配器中创建实例

Picasso picassoInstance;

在适配器的构造函数中初始化实例

picassoInstance = new Picasso.Builder(context.getApplicationContext())
        .addRequestHandler(new PdfRequestHandler())
        .build();

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

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

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);
海螺姑娘 2024-09-08 13:27:47

Qoppa Software 有一个 java SDK,可以将 PDF 转换为图像。
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);

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);
你怎么敢 2024-09-08 13:27:47

Thumbnails4j (我是维护者,但它属于 Elastic) 是一个 Apache 2 许可的库,用于创建缩略图,并支持 PDF 输入。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文