当前位置: 首页 > news >正文

java中word快速转pdf

java中word快速转pdf

网上其他方法转pdf要不转的太慢,要不就是损失格式,故而留下此方法留作备用。


文章目录

  • java中word快速转pdf
  • 一、依赖
  • 二、依赖包
  • 三、代码


一、依赖

<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>21.6</version><classifier>jdk16</classifier>
</dependency>

二、依赖包

请将aspose.7z文件解压,将解压后文件放到maven的com目录下。

网盘下载:

通过网盘分享的文件:word快速转pdf
链接: https://pan.baidu.com/s/1XXHnqy9FY3oA8SwiIgd4RA?pwd=1234 提取码: 1234 
--来自百度网盘超级会员v6的分享

csdn下载:

https://download.csdn.net/download/weixin_44399264/90968983

三、代码

package com.tdxx.common.utils;import com.aspose.words.Document;
import com.aspose.words.SaveFormat;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Modifier;public class DocGenUtil {/*** 将word文档转换为pdf,并以InputStream形式返回** @param inputStream 输入的word文档流* @return 转换后的pdf文档流*/public static InputStream wordConvertPdf(InputStream inputStream) {Document doc = null;ByteArrayOutputStream outputStream = new ByteArrayOutputStream();try {// 加载输入流中的Word文档removeWaterMark(); // 移除水印(如果需要)doc = new Document(inputStream);// 将Word文档保存为PDF格式到ByteArrayOutputStreamdoc.save(outputStream, SaveFormat.PDF);// 返回生成的PDF文档流return new ByteArrayInputStream(outputStream.toByteArray());} catch (Exception e) {throw new RuntimeException("Failed to convert Word to PDF", e);} finally {// 确保关闭流try {if (inputStream != null) inputStream.close();outputStream.close();} catch (Exception e) {// 忽略关闭流时的异常}}}/*** 将word文档转换成pdf输出到指定目录* @param filePath* @param outFilePath*/public static void wordConvertPdf(String filePath, String outFilePath) {Document doc = null;Path path = Paths.get(filePath);try (InputStream is = Files.newInputStream(path);FileOutputStream os = new FileOutputStream(outFilePath)) {removeWaterMark();doc = new Document(is);doc.save(os, SaveFormat.PDF);} catch (Exception e) {throw new RuntimeException(e);}}/*** 去除软件包工具自带水印(毕竟是收费的,嘿嘿)* 使用反射替换变量* @return*/public static void removeWaterMark() throws Exception {Class<?> aClass = Class.forName("com.aspose.words.zzXyu");java.lang.reflect.Field zzZXG = aClass.getDeclaredField("zzZXG");zzZXG.setAccessible(true);java.lang.reflect.Field modifiersField = zzZXG.getClass().getDeclaredField("modifiers");modifiersField.setAccessible(true);modifiersField.setInt(zzZXG, zzZXG.getModifiers() & ~Modifier.FINAL);zzZXG.set(null,new byte[]{76, 73, 67, 69, 78, 83, 69, 68});}
}

相关文章:

  • 合肥做网站的价格免费数据分析网站
  • 陕西省建设厅官网查询上海网站关键词排名优化报价
  • 个人做短视频网站全球搜钻
  • 平面设计要什么学历seo基本步骤
  • jsp源码做网站优化设计四年级上册数学答案
  • 织梦怎么做英文版网站网络营销首先要进行
  • 2.2.2 ASPICE的需求分析
  • 大话软工笔记—需求分析概述
  • Vue 3 + WebSocket 实战:公司通知实时推送功能详解
  • 测试微信模版消息推送
  • Yii2项目自动向GitLab上报Bug
  • Linux-08 ubuntu 的 chrome浏览器不能使用 搜狗 输入法,但是火狐可以
  • 【MATLAB第119期】基于MATLAB的KRR多输入多输出全局敏感性分析模型运用(无目标函数,考虑代理模型)
  • 原型模式深度解析:Java设计模式实战指南与克隆机制优化实践
  • C# dll版本冲突解决方案
  • C# 使用表达式树(Expression Tree)代替反射赋值
  • 机器学习之聚类Kmeans算法
  • MongoDB 入门指南:安装、配置与 Navicat 连接教程
  • 冒泡排序C++实现
  • Linux系统部署KES
  • MySQL中text,longtext,mediumtext区别
  • CSS设置元素的宽度根据其内容自动调整
  • 如何将数据从 iPhone 传输到 Android?
  • 网页版便签应用开发:HTML5本地存储与拖拽交互实践
  • c++默认类模板参数
  • iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版​分享