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

    }


相关推荐
p***s911 分钟前
【SpringBoot】日志文件
java·spring boot·spring
zqy02275 分钟前
python安装与环境配置
开发语言·python
z***D6488 分钟前
SpringBoot 新特性
java·spring boot·后端
冷雨夜中漫步9 分钟前
Maven BOM(Bill of Materials)使用指南与常见错误
java·数据库·maven
客梦9 分钟前
Java教室管理系统
java·笔记
拼好饭和她皆失14 分钟前
Java学习---Arrays类
java·开发语言·学习
BUTCHER514 分钟前
maven插件
java·maven
Wise玩转AI17 分钟前
从LLM到Agent:技术迁移的必然趋势
人工智能·python·语言模型·ai智能体