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

org.apache.poi——将 office的各种类型文件(word等文件类型)转为 pdf

org.apache.poi——将 office的各种类型文件(word等文件类型)转为 pdf

  • 简介
  • 使用方法
    • word转pdf
  • 使用示例
    • word转pdf

简介

使用方法

word转pdf

Maven坐标为

		<dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.0.3</version></dependency><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.0.3</version></dependency>

核心方法如下所示。

//创建转换器
IConverter converter = LocalConverter.builder().build();
//转换需要的参数,依次是输入流、转换前的原类型、输出流、转换后的目标类型
boolean execute = converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).schedule().get();

使用示例

word转pdf

亲测有效

//    public static void main(String[] args) {
//        WordToPdfConverter converter = new WordToPdfConverter("C:\\Users\\LJH\\Desktop\\上传文件sha1为02.docx","C:\\Users\\LJH\\Desktop\\testConvert.pdf");
//        converter.convert();
//    }public class WordToPdfConverter {String sourcePath;String targetPath;public WordToPdfConverter(String sourcePath, String targetPath){this.sourcePath = sourcePath;this.targetPath = targetPath;}/*** 输入:目前被限制为Path.toUri()* return:可能为null* */public File convert() {File inputWord = new File(sourcePath.toUri());File outputPdf = new File(targetPath.toUri());InputStream docxInputStream = null;OutputStream outputStream = null;try {docxInputStream = new FileInputStream(inputWord);outputStream = new FileOutputStream(outputPdf);IConverter converter = LocalConverter.builder().build();boolean execute = converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).schedule().get();if (execute) {System.out.println("转换完毕 targetPath = " + outputPdf.getAbsolutePath());} else {System.out.println("[documents4J] word转pdf失败:");return null;}converter.shutDown();} catch (Exception e) {System.out.println("[documents4J] word转pdf失败:"+e.toString());return null;}finally {if (outputStream != null) {try {outputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}if (docxInputStream != null) {try {docxInputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}return outputPdf;}
}
http://www.xdnf.cn/news/277849.html

相关文章:

  • 解决 Builroot 系统编译 perl 编译报错问题
  • jeecg查询指定时间
  • 【分享】4D视差壁纸
  • Java运算符学习笔记
  • 【React Hooks原理 - useCallback、useMemo】
  • 大根堆+小根堆 问题
  • 【C++】封装unordered_set和unordered_map
  • 如何快速获取GPU参数,并解读其性能?
  • 【翻译、转载】【译文】图解模型上下文协议(MCP)
  • Day3:设置页面全局渐变线性渐变背景色uniapp壁纸实战
  • SALOME源码分析: SolverLab
  • 【Trae+LucidCoder】三分钟编写专业Dashboard页面
  • LabVIEW温控系统热敏电阻滞后问题
  • C++类_嵌套类
  • 【题解】CF196D (哈希)
  • 强化学习机器人模拟器——RobotApp:一个交互式强化学习模拟器
  • stm32卡在SystemClock_Config();的解决方法
  • 华为鸿蒙PC:开启国产操作系统自主化新纪元
  • 【网络】什么是串口链路(Serial Link)?
  • python hasattr()
  • 深入了解 OpenIddict:实现 OAuth 2.0 和 OpenID Connect 协议的 .NET 库
  • 《算法导论(第4版)》阅读笔记:p6-p6
  • 可信执行环境(TEE):保障数据安全的核心技术
  • 【深入浅出MySQL】之数据类型介绍
  • Git推送大文件导致提交回退的完整解决记录
  • n8n工作流自动化平台的实操:生成统计图的两种方式
  • Solr 与 传统数据库的核心区别
  • 前端面试宝典---性能优化
  • OpenLayers:侦听缩放级别的变化
  • 消息队列MQ