Springboot -- DOCX转PDF(二)

之前记录了按照模板生成 DOCX 文件、并转换为 PDF 文件的方法
https://blog.youkuaiyun.com/qq_40096897/article/details/131979177?spm=1001.2014.3001.5501

但是使用效果并不是很理想,转换完的 PDF 格式和原本的文档格式不匹配。所以在此重新找了一个文件转 PDF 的方法。

<dependency>
   <groupId>com.luhuiguo</groupId>
   <artifactId>aspose-pdf</artifactId>
   <version>23.1</version>
</dependency>
<dependency>
   <groupId>com.luhuiguo</groupId>
   <artifactId>aspose-words</artifactId>
   <version>23.1</version>
</dependency>

可以保存为输出流

InputStream inputStream = new FileInputStream("F:\\test.docx");
try (ByteArrayOutputStream  outPDF = new ByteArrayOutputStream()) {
    //转PDF
    com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
    doc.save(outPDF, com.aspose.words.SaveFormat.PDF);
} catch (IOException e) {
    e.printStackTrace();
}
inputStream.close();

也可以直接保存为指定文件

InputStream inputStream = new FileInputStream("F:\\test.docx");
com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
doc.save("F:\\test.pdf", com.aspose.words.SaveFormat.PDF);
inputStream.close();

效果如下:
这是DOCX模板,模板格式如下
在这里插入图片描述
这是按照模板生成文档后再转为PDF的效果:
在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mingvvv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值