Java文件操作实现doc格式转pdf

使用场景

在进行生成文档或者报告的时候,生成的word想要转换为pdf格式,这样才能保证报告的不可修改性,但是aspose-words的工具的License令人头疼,这篇文章就是解决这个doc转pdf的License的问题。话不多说,直接上实操。

所需资源

首先,需要一个本地的jar包,放在resources下的lib文件夹中,没有的话自己新建一个

这个jar包没有的小伙伴,可以在这边获取下载:aspose-words-21.11-jdk17.jar

代码实现

把本地的jar包导入到项目pom中

XML 复制代码
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>21.11</version>
    <scope>system</scope>
    <systemPath>${pom.basedir}/src/main/resources/lib/aspose-words-21.11-jdk17.jar</systemPath>
</dependency>

代码实现doc转pdf,新建一个工具类PDFHelper.java

java 复制代码
import com.aspose.words.License;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
public class PDFHelper {

    @SneakyThrows
    public static void doc2pdfV2(String sourceFile, String targetFile) {
        InputStream is = null;
        String licenseStr =
                "<License>\n" +
                        "    <Data>\n" +
                        "        <Products>\n" +
                        "            <Product>Aspose.Total for Java</Product>\n" +
                        "            <Product>Aspose.Words for Java</Product>\n" +
                        "        </Products>\n" +
                        "        <EditionType>Enterprise</EditionType>\n" +
                        "        <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                        "        <LicenseExpiry>20991231</LicenseExpiry>\n" +
                        "        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                        "    </Data>\n" +
                        "    <Signature>\n" +
                        "        sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n" +
                        "    </Signature>\n" +
                        "</License>";
        //            is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\tool\\license.xml");
        is = new ByteArrayInputStream(licenseStr.getBytes(StandardCharsets.UTF_8));
        License license = new License();
        try {
            license.setLicense(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            long old = System.currentTimeMillis();
            FileOutputStream os = new FileOutputStream(targetFile);
            com.aspose.words.Document doc = new com.aspose.words.Document(sourceFile);
            doc.save(os, com.aspose.words.SaveFormat.PDF);
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            e.printStackTrace();
            log.error("word转pdf 报告生成异常");
            throw new Exception("报告生成pdf失败!");
        }
    }
}

其中辅助使用到了lombok模块,如果项目本身没有的,可以自行修改或者导入

导入pom代码如下:

XML 复制代码
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
相关推荐
开开心心就好9 小时前
用户推荐的文件解锁与强制操作工具
安全·智能手机·pdf·scala·音视频·symfony·1024程序员节
shuaiqinke1 天前
分享噪音测试分贝仪 精准测噪 数据直观超实用
pdf
NiceCloud喜云1 天前
Claude API PDF 文档问答实战:从原生解析到分页引用的完整方案
java·服务器·前端·网络·数据库·人工智能·pdf
w2018002 天前
新高考答题卡模板全套PDF可打印(语文数学英语等)
pdf·高考
奋斗的老史2 天前
LibreOffice封装文档转 PDF 工具类
java·pdf
优化控制仿真模型2 天前
【26年最新】新高考英语大纲词汇表3500个电子版PDF(含正序版、乱序版和默写版)
经验分享·pdf
Eiceblue2 天前
使用 C# 高效替换 PDF 中的文本:全页、区域与正则匹配
visualstudio·pdf·c#
Upsy-Daisy2 天前
AI Agent 项目学习笔记(十):文件操作、终端执行与 PDF 生成工具
笔记·学习·pdf
霸道流氓气质2 天前
批量收集多源 URL 并异步转 PDF 打包下载的完整实现(Spring Boot + Feign + 异步任务)
windows·spring boot·pdf
开开心心_Every3 天前
支持自定义名单的实用随机抽签工具
运维·服务器·pdf·电脑·excel·启发式算法·宽度优先