Java 将PDF 转为图片 工具 【Free Spire.PDF for Java】(免费版)

Java 将PDF 转为图片

使用工具:Free Spire.PDF for Java(免费版)

Jar文件获取及导入:

方法1:通过官网下载jar文件包。下载后,解压文件,并将lib文件夹下的Spire.Pdf.jar文件导入Java程序。

方法2:可通过maven仓库安装导入。参考导入方法

xml 复制代码
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
xml 复制代码
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.pdf.free</artifactId>
    <version>5.1.0</version>
</dependency>

Java代码示例

java 复制代码
public class PdfToImageExample {

    public static void main(String[] args) {
        //程序的逻辑代码
        String pdfFilePath = "D:\\Users\\65720\\Desktop\\电信项目-方案\\文艺\\勇罕\\勇罕\\勇罕 2022年 第 4 期.pdf"; // 替换为您的PDF文件路径
        String outputImagePath = "D:\\Users\\65720\\Desktop\\电信项目-方案\\文艺\\book\\fc5484fd24b72fb25bd57adf57138d3d\\"; // 保存为PNG格式的图片路径
        spirePdfFree(pdfFilePath, outputImagePath);


    }
    
    private static void spirePdfFree(String pdfFilePath, String outputImagePath) {
        try {
            PdfDocument pdf = new PdfDocument(pdfFilePath);
            BufferedImage image;
            for (int i = 0; i < pdf.getPages().getCount(); i++) {
                image = pdf.saveAsImage(i);
                File file = new File(String.format(outputImagePath + "page%d.png", i));
                ImageIO.write(image, "PNG", file);
            }
            pdf.close();
        }catch (IOException e){
            e.printStackTrace();
        }

    }


}

产出图片


问题

需要设置图片的大小,能加载10页pdf,但是只能获得3张图片。

参考链接:

Java 将PDF 转为Word、图片、SVG、XPS、Html、PDF/A

使用工具: Spire.PDF for Java

试了一下收费版本的 9.12.0,不购买能加载10页,生成的图片带水印。清晰度挺高,速度页非常快。价格也高!

xml 复制代码
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
xml 复制代码
 <dependency>
     <groupId>e-iceblue</groupId>
     <artifactId>spire.pdf</artifactId>
     <version>9.12.0</version>
 </dependency>
java 复制代码
 private static void spirePdfFree(String pdfFilePath, String outputImagePath) {
        try {
            PdfDocument pdf = new PdfDocument(pdfFilePath);
            for (int i = 0; i < 10; i++) {
                BufferedImage image = pdf.saveAsImage(i, PdfImageType.Bitmap,500,500);
                File file = new File(String.format(outputImagePath + "page%d.png", i));
                ImageIO.write(image, "PNG", file);
            }
            pdf.close();
        }catch (IOException e){
            e.printStackTrace();
        }

    }


相关推荐
lczdyx23 分钟前
PNG转ico图标(支持圆角矩形/方形+透明背景)Python脚本 - 随笔
图像处理·python
小雅痞29 分钟前
[Java][Leetcode middle] 55. 跳跃游戏
java·leetcode
com未来35 分钟前
使用 NSSM 安装 Tomcat 11.0.6 为 Windows 服务
java·windows·tomcat
TDengine (老段)40 分钟前
基于 TSBS 标准数据集下 TimescaleDB、InfluxDB 与 TDengine 性能对比测试报告
java·大数据·开发语言·数据库·时序数据库·tdengine·iotdb
养军博客42 分钟前
spring boot3.0自定义校验注解:文章状态校验示例
java·前端·spring boot
lgily-122543 分钟前
常用的设计模式详解
java·后端·python·设计模式
IT成长史1 小时前
deepseek梳理java高级开发工程师微服务面试题
java·微服务
茶本无香1 小时前
Feign+Resilience4j实现微服务熔断机制:原理与实战
java·微服务·feignclient·熔断·resilience4j
遇见火星1 小时前
Ansible模块——从控制节点向目标主机复制文件!
java·服务器·ansible
小码ssim1 小时前
通过POI实现对word基于书签的内容替换、删除、插入
java·word