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

    }


相关推荐
ZTL-NPU4 分钟前
Jetbrains开发ros
ide·python·pycharm·编辑器·ros·clion
棉花骑士19 分钟前
【AI Agent】面向 Java 工程师的Claude Code Harness 学习指南
java·开发语言
爱敲代码的小鱼34 分钟前
springboot(2)从基础到项目创建:
java·spring boot·spring
环黄金线HHJX.40 分钟前
TSE框架配置与部署详解
开发语言·python
前端摸鱼匠1 小时前
YOLOv11与OpenCV 联动实战:读取摄像头实时视频流并用 YOLOv11 进行检测(三)
人工智能·python·opencv·yolo·目标检测·计算机视觉·目标跟踪
Pyeako1 小时前
PyQt5 + PaddleOCR实战:打造桌面级实时文字识别工具
开发语言·人工智能·python·qt·paddleocr·pyqt5
迈巴赫车主1 小时前
蓝桥杯19724食堂
java·数据结构·算法·职场和发展·蓝桥杯
i220818 Faiz Ul2 小时前
动漫商城|基于springboot + vue动漫商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·动漫商城系统
喝凉白开都长肉的大胖子2 小时前
在 Matplotlib 中fontweight一般怎么设置
python·matplotlib
海兰2 小时前
【实战】MCP 服务在 Nacos 中注册状态分析与优化
android·java·github·银行系统·银行ai