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

word、excel文件转PDF(documents4j方式,简单)

1 documents4j方式

引入pom

<dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.1.12</version></dependency><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.1.12</version></dependency>#excel转pdf需要的jar
<dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-excel</artifactId><version>1.1.12</version></dependency>

2 代码实现(word,excel转pdf代码)

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;public static void main(String[] args) {File inputWord = new File("E:\\111\\123.docx");File outputFile = new File("E:\\111\\123.pdf");try  {InputStream docxInputStream = new FileInputStream(inputWord);OutputStream outputStream = new FileOutputStream(outputFile);IConverter converter = LocalConverter.builder().build();converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();outputStream.close();} catch (Exception e) {e.printStackTrace();}}
/*** documents4j方式excel转pdf* @param file 源文件* @return 返回转成功的文件*/public static File wordToPDFByDocuments4j(File file) {File outputFile = new File(String.format("%s%s%s%s", file.getParentFile(),  File.separator, System.currentTimeMillis(), ".pdf"));try  {InputStream docxInputStream = new FileInputStream(file);OutputStream outputStream = new FileOutputStream(outputFile);IConverter converter = LocalConverter.builder().build();converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();outputStream.close();} catch (Exception e) {e.printStackTrace();}return outputFile;}

2 poi方式

http://www.lryc.cn/news/250561.html

相关文章:

  • 【Linux】:信号(三)捕捉
  • 数据结构 / 队列 / 循环队列 / 概念
  • elasticsearch 内网下如何以离线的方式上传任意的huggingFace上的NLP模型(国内闭坑指南)
  • vue中中的动画组件使用及如何在vue中使用animate.css
  • MATLAB 模型参考自适应控制 - Model Reference Adaptive Control
  • 【如何用批处理文件实现自动编译Keil工程和C# Visual Studio工程】
  • 大模型的实践应用11-“书生”通用大模型的搭建与模型代码详细介绍,以及快速使用方法
  • 【开发PaaS】基于Postgresql的开发平台Supabase
  • 前端开启gzip优化页面加载速度
  • 用Java写一个俄罗斯方块
  • 应用于智慧金融的AI边缘计算盒子+AI算法软硬一体化方案
  • 目标检测——Faster R-CNN算法解读
  • Wireshark (一)安装入门 —— 软件介绍
  • Web框架与Django路由层
  • 什么是CAS, 什么是AQS
  • 蓝桥杯每日一题2023.12.1
  • 正则表达式从放弃到入门(1):“正则表达式”是什么?
  • SQL解惑 - 谜题2
  • FWT+高维前缀和:Gym - 103202M
  • 【C++】string类的接口综合运用
  • 分布式ID生成框架Leaf升级踩坑
  • 常用的设计模式
  • git的相关实用命令
  • 【使用`model.status`来获取gurobi求解过程中的模型状态】
  • 【UGUI】Unity教程:实现物品的拖拽功能
  • 【奇淫技巧】两数交换
  • Java核心知识点整理大全26-笔记
  • “上云”还是“下云”?探云计算的下一站未来!
  • Linux中top命令输出日志分析?
  • 执行栈和执行上下文