Java Word转PDF Excel转PDF
前提: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相关推荐
- Uipath 处理excel转PDF或者合并PDF(excel to pdf, join pdf files)
Uipath 处理excel转PDF或者合并PDF(excel to pdf, join pdf files) 应用案例:需要将两个独立的excel分别转成PDF,并合并成一个PDF文件(比如:EXC ...
- java使用aspose实现Excel转PDF加入密码保护并解密
添加依赖 <dependency><groupId>com.luhuiguo</groupId><artifactId>aspose-cells< ...
- java采用Jacob将Excel转PDF
注意事项:使用此方法需安装Office import java.io.File; import java.util.Date;import com.jacob.activeX.ActiveXCompo ...
- Java Word文档如何转为PDF文件
最近做的功能要求是Java 调用ftl模板生成Word文件,再将Word文件转为PDF 1.首先是ftl模板生成Word文件.Java 利用ftl模板生成Word用的技术是freemarker,相关技 ...
- Java中导入/导出excel,导出pdf报表信息
1.项目中经常需要用到报表生成,信息导入数据库的功能.主要有以下几种. 2.其中比较简单的是 外部数据无需处理直接 导入数据库中,这种比较简单.直接利用Navicat数据库工具 导入外部.示例如下 1 ...
- java通过poi导出excel和pdf
[背景] 由于各户的需求,所以需要增加导出excel这个功能,其实大部分系统都需要这个导出功能的,所以这里也就不详细说明具体导出的背景了O(∩_∩)O~ 干完导出excel将现有的导出pdf也进行了独 ...
- Java 文件转换工具类(word,html,excel,pdf,md,jpeg,png互转)
一.前言 目前支持: word 转 html doc 转 docx html 转 word html 转 pdf excel 转 pdf word 转 jpeg word 转 png markdown ...
- word excel转pdf,pdf合并,插入页码
word excel转pdf,pdf合并,插入页码 将word转换为pdf,excel转换为pdf.pdf合并及在最终的pdf中添加页码 word转换为pdf String wordPath = &q ...
- 利用Spire.PDF转换Excel成PDF
在一般情况下,报表输入一般以Excel和PDF格式为主,Excel生成有很多已经相当成熟的工具,但现阶段PDF生成相对较为生硬.Spire.PDF提供了更好的解决方案,可以高效无损的将Excel转 ...
最新文章
- Nat. Genet. | 基于遗传学主导的方法定义免疫相关性状的药物靶标
- java开发checklist,Java API设计CheckList
- java实现插入排序算法 附单元测试源码
- CTF短秘钥的RSA解密
- 如何异地加载 Spring Boot 配置文件?
- 软件设计原则(五)接口隔离原则 -Interface Segregation Principle
- [javascript]图解+注释版 Ext.extend()
- SAP Cloud Application Programming bookshop 例子的 Fiori Preview
- javascript php 性能,浅谈页面装载js及性能分析方法_javascript技巧
- php 查看引入文件,include - 引入文件
- Win7设置自环的方法(网卡终结器)
- ENVI Flaash大气校正与6S大气校正(TM为例)
- springboot调用so文件
- Ubuntu16.04火狐浏览器问题汇总
- 世界上最简单的会计书-笔记
- JavaScript中获取键盘事件
- 生命,因追逐梦想而精彩
- win10语言栏恢复原位
- 【电商】电商供应链产品介绍
- __attribute__中的constructor和destructor