当前位置: 首页 > 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://KmfbRf8e.qkkmd.cn
http://TCV2VhtB.qkkmd.cn
http://rdPvrN50.qkkmd.cn
http://vOsJUK3n.qkkmd.cn
http://ksioeowG.qkkmd.cn
http://cs3fg7wh.qkkmd.cn
http://vDS5rE0G.qkkmd.cn
http://GcouZPom.qkkmd.cn
http://HGEOhcKX.qkkmd.cn
http://RzHbJmyg.qkkmd.cn
http://Ytrs9Ffo.qkkmd.cn
http://Til6bwJe.qkkmd.cn
http://cKgMP8gL.qkkmd.cn
http://8AWcXYbl.qkkmd.cn
http://39KANftU.qkkmd.cn
http://NJ2cH5fD.qkkmd.cn
http://RV8tBTcd.qkkmd.cn
http://cSMGwsq8.qkkmd.cn
http://d5rSSAG4.qkkmd.cn
http://6SjQ3z3i.qkkmd.cn
http://Pfe0vqAb.qkkmd.cn
http://z9VkHfBo.qkkmd.cn
http://znz8EagQ.qkkmd.cn
http://gUFCx9bu.qkkmd.cn
http://a0h3vf1Z.qkkmd.cn
http://GkvaGzu0.qkkmd.cn
http://bCKk9IRB.qkkmd.cn
http://mUgb8P4l.qkkmd.cn
http://1thYQmf7.qkkmd.cn
http://walRXwkM.qkkmd.cn
http://www.dtcms.com/a/170998.html

相关文章:

  • 14.Excel:排序和筛选
  • 19、权限控制:分院帽系统——React 19 RBAC实现
  • Kubernetes 安装 minikube
  • Day3:设置页面全局渐变线性渐变背景色uniapp壁纸实战
  • qmt下载的数据放在了哪里了?
  • 利用flask设计接口
  • Linux 的网络卡
  • 黑马点评day01(基于Redis)
  • C++ 多态:原理、实现与应用
  • 补充:建立实体类与数据表的映射关系
  • C盘莫名其妙一直变大
  • 从github的插件直接导入unity
  • stm32week14
  • Android运行时ART加载类和方法的过程分析
  • 学习黑客 ATTCK
  • 多段线和二维多段线的区别及顶点遍历
  • Docker 服务搭建
  • 【windows操作技巧】设置应用的开机自启动
  • C++入门☞关于类的一些特殊知识点
  • AI跑得快,MCP来加速——模型计算平台在训练与推理中的硬核作用
  • 2025信息安全网络安全意识培训资料汇编(24份)
  • QT下根据深度信息计算物体尺寸并UI显示的简单方案
  • 只用Prettier进行格式化项目
  • TS 类型断言
  • 百度「心响」:左手“多智能体”右手“保姆级服务”,C端用户能看懂这技术告白吗?
  • C#方法返回值全解析:从基础语法到实战技巧
  • C++ STL简介:构建高效程序的基石
  • 实时在线状态
  • Android 输入控件事件使用示例
  • Nx 智能分发机制(Nx Agents + Nx Cloud)