在 Java 中将 PDF 转换成图像

JPedal 能将 PDF 文件转换为 BMP、HEIC、JPEG、JPEG2000、PNG、TIFF 或 WEBP 图像文件

使用下面的 Java 代码将 PDF 转换为图像

ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".bmp");
            // Setters to control output
            final BmpEncoderOptions options = new BmpEncoderOptions();
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();
ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".gif");
            // Setters to control output
            final GifEncoderOptions options = new GifEncoderOptions();
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();
ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".heic");
            // Setters to control output
            final HeicEncoderOptions options = new HeicEncoderOptions();
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();
ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".jpg");
            // Setters to control output
            final JpegEncoderOptions options = new JpegEncoderOptions();
            options.setQuality(90); // Default is 75. No compression is 100
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();
ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".jpx");
            // Setters to control output
            final Jpeg2000EncoderOptions options = new Jpeg2000EncoderOptions();
            options.setOutputSubtype(Jpeg2000OutputSubtype.JPX); // Default is JPX. JP2 also
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();
ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".png");
            // Setters to control output
            final PngEncoderOptions options = new PngEncoderOptions();
            options.setCompressionFormat(PngCompressionFormat.NONE);
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();
ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".tiff");
            // Setters to control output
            final TiffEncoderOptions options = new TiffEncoderOptions();
            options.setCompressionFormat(TiffCompressionFormat.DEFLATE);
            options.setResolutionUnit(TiffResolutionUnit.INCH);
            options.setXResolution(300);
            options.setYResolution(300);
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();
ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
try {
    if (convert.openPDFFile()) {
        for (int page = 1; page <= convert.getPageCount(); page++) {
            final BufferedImage bi = convert.getPageAsImage(page);
            final File out = new File("/path/to/output/" + page + ".webp");
            // Setters to control output
            final  options = new ();
            options.setQuality(90); // Default is 75. No compression is 100
            options.setCompressionFormat(WebpCompressionFormat.LOSSLESS); // Also supports LOSSY
            JDeli.write(bi, options, out);
        }
    }
} catch (PdfException | IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();
// Disable all caching to file to reduce memory usage
PdfFileReader.alwaysCacheInMemory = -1;
// bytes is a byte[] with the PDF file data
ConvertPagesToImages convert = new ConvertPagesToImages(bytes);
// convert.setPassword("password"); // If password needed
try {
    if (convert.openPDFFile()) {
        int pageCount = convert.getPageCount();
        for (int page = 1; page <= pageCount; page++) {
            BufferedImage image = convert.getPageAsImage(page);
        }
    }
} catch (PdfException e) {
    e.printStackTrace();
}
convert.closePDFfile();

其他开发者怎么看?

JPedal 的 PDF 转换图像功能的主要特性

开发者可以用 JPedal 将 PDF 文档的页面转换为高质量图像

图像输出格式设置

JPedal 可以将 PDF 输出为 BMP、HEIC、JPEG、JPEG2000、PNG、TIFF(单页/多页)以及 WEBP 格式的图像。

图像输出尺寸设置

开发者可以用 JPedal 精确控制图像输出尺寸,包括使用放大和缩小采样的选项。

内存中转换

开发者可以将 JPedal 设置为完全在内存中执行转换,以实现快速或安全的转换。

卓越性能

JPedal 比其他替代方案快 3 到 4 倍,并且内存占用量更低。

无其他依赖项

JPedal 仅需 Java 即可运行。它不使用任何其他本机或 Java 库。

Java 支持

JPedal 可在 Java 17 及以上版本中运行,并在所有新的 Java 发行版上通过了测试。

立即开始免费试用 JPedal