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

    }


相关推荐
Zenova EdgeOS6 分钟前
Linux ss 工业边缘网络分析实战
linux·python·边缘计算·工业网关
2603_965148119 分钟前
AI+API选品:下一代智能商务助手雏形已现
java·大数据·数据库·人工智能·数据挖掘
wuminyu9 分钟前
HotSpot的轻量锁与膨胀后的ObjectMonitor状态重建原理
java·linux·c语言·jvm·c++
CoderYanger17 分钟前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
北冥有鱼被烹18 分钟前
perftest 之 ib_write_bw --use-null-mr 参数详解:用 NULL MR 做 RDMA 带宽测试
java
成为深度学习高手20 分钟前
CrossLinear:即插即用的跨相关嵌入,让线性模型也能用好外生变量
人工智能·python·深度学习·数据挖掘
2601_9638702122 分钟前
基于SSM的特产代购系统
java·前端
牧瀬クリスだ29 分钟前
YAML配置详解:SpringBoot实战指南
java·spring·yml
Su57331 分钟前
RuoYi + RAGFlow 搭建私有化知识库完整集成实践(二)
java
wechatbot88837 分钟前
极客互动企业微信 SCRM 自动运营平台效果实测
java·微信·企业微信·rpa