在Java中将PDF转换为缩略图图像

在Java中将PDF转换为缩略图图像,java,image,pdf,Java,Image,Pdf,有谁能给我推荐一个免费的Java库,它可以转换PDF格式并从第一页创建缩略图(PNG) 谢谢 您可以试试,这是一个纯java解决方案。下面的代码创建第一页的图像 File pdfFile = new File("/path/to/pdf.pdf"); RandomAccessFile raf = new RandomAccessFile(pdfFile, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.

有谁能给我推荐一个免费的Java库,它可以转换PDF格式并从第一页创建缩略图(PNG)

谢谢

您可以试试,这是一个纯java解决方案。下面的代码创建第一页的图像

File pdfFile = new File("/path/to/pdf.pdf");
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
PDFPage page = pdf.getPage(0);

// create the image
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(),
                                 (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height,
                                  BufferedImage.TYPE_INT_RGB);

Image image = 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
);
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, null);
ImageIO.write(bufferedImage, format, new File( "/path/to/image.jpg" ));

非常好的斯多拉,谢谢你的意见。 为了转换pdf中的所有页面,我重新处理了您的示例

希望这能对你们中的一些人有所帮助

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.imageio.ImageIO;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class Main {

    public static void main(String[] args) throws IOException {
        File pdfFile = new File("c:\\YOURPDF.pdf");
        RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        PDFFile pdf = new PDFFile(buf);

        for (int i=0; i<pdf.getNumPages(); i++) {
            createImage(pdf.getPage(i), "c:\\PICTURE_" + i + ".jpg");
        }
    }

    public static void createImage(PDFPage page, String destination) throws IOException{
        Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(),
                (int) page.getBBox().getHeight());
        BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height,
                         BufferedImage.TYPE_INT_RGB);

        Image image = 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
        );
        Graphics2D bufImageGraphics = bufferedImage.createGraphics();
        bufImageGraphics.drawImage(image, 0, 0, null);
        ImageIO.write(bufferedImage, "JPG", new File( destination ));
    }

}
导入java.awt.Graphics2D;
导入java.awt.Image;
导入java.awt.Rectangle;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.io.RandomAccessFile;
导入java.nio.ByteBuffer;
导入java.nio.channels.FileChannel;
导入javax.imageio.imageio;
导入com.sun.pdfview.PDFFile;
导入com.sun.pdfview.PDFPage;
公共班机{
公共静态void main(字符串[]args)引发IOException{
文件pdfFile=新文件(“c:\\YOURPDF.pdf”);
RandomAccessFile raf=新的RandomAccessFile(Pdfile,“r”);
FileChannel=raf.getChannel();
ByteBuffer buf=channel.map(FileChannel.MapMode.READ_ONLY,0,channel.size());
Pdfile pdf=新的Pdfile(buf);
对于(int i=0;i