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

    }


相关推荐
古法安卓4 小时前
Android-日志系统源码解析
android·java·android studio
用户7783366132115 小时前
用 React Hook 封装搜索数据:useSerp 的防抖、缓存与错误处理
python·api
小夏coding5 小时前
从"一把梭"到"精妙拆解" —— 滑动窗口计时框架的设计演进
java·后端
MacroZheng6 小时前
同事问我:"Claude Code经常失忆,不怕它把项目搞炸?",我:"怕,三个Markdown文件给它装个永不丢失的外置大脑!"
java·人工智能·后端
十年Java程序媛8 小时前
深度实战:JDK21 虚拟线程 + HikariCP 生产正确配比、坑点、监控全套
java·spring boot
xiaoqiMikko8 小时前
有人在搜一个不存在的 Tomcat 版本
java·tomcat
65岁退休Coder9 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
用户31268748772011 小时前
ConcurrentHashMap 怎么保证线程安全?从分段锁到 CAS+synchronized
java
vipxieliang11 小时前
ValidX vs Apache Commons Validator:功能与性能对比
java·spring boot
SimonKing11 小时前
升级Spring Boot 4后,从 Jackson 2 到 3,到底有哪些变化
java·后端·程序员