前提:Windows环境
在项目中,有需求Word转PDF或者Excel转PDF,例举一个简单有效的转换工具。
Windows环境亲测有效。

Maven项目依赖:

        <!-- https://mvnrepository.com/artifact/com.documents4j/documents4j-local --><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-local</artifactId><version>1.1.5</version></dependency><!-- https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer-msoffice-word --><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-word</artifactId><version>1.1.5</version></dependency><!-- https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer-msoffice-excel --><dependency><groupId>com.documents4j</groupId><artifactId>documents4j-transformer-msoffice-excel</artifactId><version>1.1.5</version></dependency>

示例代码如下:


import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;import java.io.*;/*** @description  PDF转换工具类 仅支持Windows环境*/
public class PdfConvertUtil {/*** @param sourceFilePath 源文件路径* @param pdfFilePath    PDF文件路径* @description Word转PDF 仅支持Windows环境*/public static boolean word2pdf(String sourceFilePath, String pdfFilePath) {boolean boo = false;File inputFile = new File(sourceFilePath);File outputFile = new File(pdfFilePath);if (!inputFile.exists()) {System.err.println("源文件不存在");return boo;}if (!outputFile.exists()) {try {outputFile.createNewFile();} catch (IOException e) {System.err.println("PDF文件创建失败");e.printStackTrace();}}try {InputStream inputStream = new FileInputStream(inputFile);OutputStream outputStream = new FileOutputStream(outputFile);IConverter converter = LocalConverter.builder().build();converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();outputStream.close();boo = true;} catch (Exception e) {e.printStackTrace();}return boo;}/*** @param sourceFilePath 源文件路径* @param pdfFilePath    PDF文件路径* @description Excel转PDF 仅支持Windows环境*/public static boolean excel2pdf(String sourceFilePath, String pdfFilePath) {boolean boo = false;File inputFile = new File(sourceFilePath);File outputFile = new File(pdfFilePath);if (!inputFile.exists()) {System.err.println("源文件不存在");return boo;}if (!outputFile.exists()) {try {outputFile.createNewFile();} catch (IOException e) {System.err.println("PDF文件创建失败");e.printStackTrace();}}try {InputStream inputStream = new FileInputStream(inputFile);OutputStream outputStream = new FileOutputStream(outputFile);IConverter converter = LocalConverter.builder().build();converter.convert(inputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();outputStream.close();boo = true;} catch (Exception e) {e.printStackTrace();}return boo;}}

Java Word转PDF Excel转PDF相关推荐

  1. Uipath 处理excel转PDF或者合并PDF(excel to pdf, join pdf files)

    Uipath 处理excel转PDF或者合并PDF(excel to pdf, join pdf files) 应用案例:需要将两个独立的excel分别转成PDF,并合并成一个PDF文件(比如:EXC ...

  2. java使用aspose实现Excel转PDF加入密码保护并解密

    添加依赖 <dependency><groupId>com.luhuiguo</groupId><artifactId>aspose-cells< ...

  3. java采用Jacob将Excel转PDF

    注意事项:使用此方法需安装Office import java.io.File; import java.util.Date;import com.jacob.activeX.ActiveXCompo ...

  4. Java Word文档如何转为PDF文件

    最近做的功能要求是Java 调用ftl模板生成Word文件,再将Word文件转为PDF 1.首先是ftl模板生成Word文件.Java 利用ftl模板生成Word用的技术是freemarker,相关技 ...

  5. Java中导入/导出excel,导出pdf报表信息

    1.项目中经常需要用到报表生成,信息导入数据库的功能.主要有以下几种. 2.其中比较简单的是 外部数据无需处理直接 导入数据库中,这种比较简单.直接利用Navicat数据库工具 导入外部.示例如下 1 ...

  6. java通过poi导出excel和pdf

    [背景] 由于各户的需求,所以需要增加导出excel这个功能,其实大部分系统都需要这个导出功能的,所以这里也就不详细说明具体导出的背景了O(∩_∩)O~ 干完导出excel将现有的导出pdf也进行了独 ...

  7. Java 文件转换工具类(word,html,excel,pdf,md,jpeg,png互转)

    一.前言 目前支持: word 转 html doc 转 docx html 转 word html 转 pdf excel 转 pdf word 转 jpeg word 转 png markdown ...

  8. word excel转pdf,pdf合并,插入页码

    word excel转pdf,pdf合并,插入页码 将word转换为pdf,excel转换为pdf.pdf合并及在最终的pdf中添加页码 word转换为pdf String wordPath = &q ...

  9. 利用Spire.PDF转换Excel成PDF

      在一般情况下,报表输入一般以Excel和PDF格式为主,Excel生成有很多已经相当成熟的工具,但现阶段PDF生成相对较为生硬.Spire.PDF提供了更好的解决方案,可以高效无损的将Excel转 ...

最新文章

  1. Nat. Genet. | 基于遗传学主导的方法定义免疫相关性状的药物靶标
  2. java开发checklist,Java API设计CheckList
  3. java实现插入排序算法 附单元测试源码
  4. CTF短秘钥的RSA解密
  5. 如何异地加载 Spring Boot 配置文件?
  6. 软件设计原则(五)接口隔离原则 -Interface Segregation Principle
  7. [javascript]图解+注释版 Ext.extend()
  8. SAP Cloud Application Programming bookshop 例子的 Fiori Preview
  9. javascript php 性能,浅谈页面装载js及性能分析方法_javascript技巧
  10. php 查看引入文件,include - 引入文件
  11. Win7设置自环的方法(网卡终结器)
  12. ENVI Flaash大气校正与6S大气校正(TM为例)
  13. springboot调用so文件
  14. Ubuntu16.04火狐浏览器问题汇总
  15. 世界上最简单的会计书-笔记
  16. JavaScript中获取键盘事件
  17. 生命,因追逐梦想而精彩
  18. win10语言栏恢复原位
  19. 【电商】电商供应链产品介绍
  20. __attribute__中的constructor和destructor

热门文章

  1. sqlcmd去掉表头和X行受影响
  2. 现在市面跑步耳机哪款好用、分享五款适合跑步用的耳机推荐
  3. 吴京被刘德华的京哥吓了一跳,他们到底谁的贡献最大?
  4. MFC中如何将两个独立的程序合并为一个程序(障眼法)
  5. 使用ArcGIS获取行政区四至坐标的两种方法
  6. python 在列表中间插入字符串
  7. web前端开发主要课程,CSS字体样式值,赶紧收藏!
  8. OpenCV三角测量
  9. 硬件控制RS485电路图设计
  10. 各自然带代表植被_下图中两图代表安第斯山脉东西两坡上的植被分布图,①~⑦代表不同的植被。读图,回答下列各题。...