用java将简单的word文档换成pdf文档
用java将简单的word文档换成pdf文档的方式很多,因为很多都没有实际测试过,所以这里就先泛泛的说一下
整体上来看分两种:
1.纯java代码实现,有很多优秀的开源软件可以用,比如poi,itext,xdocreport,docx4j等等。主要缺点是只能处理简单的文档
2.通过在操作系统安装转换软件,在java代码中调用软件命令来实现转换。常用的有OpenOffice,Pandoc,Jacob(限于Windows环境)等软件,优点是对于复杂的文档也能很好的处理。缺点是会麻烦一点,有的不能跨平台,速度上可能也会慢一点
这里主要说一下我用xdocreport将word文档转成pdf文档的代码,xdocreport其实是对poi和itext的封装,进一步简化代码。下面看我的maven依赖
<!--Maven依赖,只多不少-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>common-codec</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
<version>1.0-2</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.0.0</version>
</dependency>
maven依赖的版本很多都比较老了,但这不重要,能实现功能就是好的,要是换成其他更高的版本可能会报错,另外里面可能有个别依赖不是必须的,你有兴趣可以自己试一试。下面上代码
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.*;
import java.util.HashMap;
import java.util.Map; public class WordToPDF { /**
* 将word文档, 转换成pdf, 中间替换掉变量
* @param source 源为word文档, 必须为docx文档
* @param target 目标输出
* @throws Exception
*/
public static void wordConverterToPdf(InputStream source, OutputStream target) throws Exception {
XWPFDocument doc = new XWPFDocument(source);
PdfOptions options = null;//因为是简单处理,该参数就设置成了null,有需要的可以研究一下
PdfConverter.getInstance().convert(doc, target, options);
} //测试
public static void main(String[] args) {
String filepath = "F:\\temp\\test.docx";
String outpath = "F:\\temp\\test.pdf";
InputStream source;
OutputStream target;
try {
source = new FileInputStream(filepath);
target = new FileOutputStream(outpath);
Map<String, String> params = new HashMap<String, String>();
wordConverterToPdf(source, target);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} }
至此就算实现了将简单word文档转成pdf文档的功能。最后再说一下注意的地方,
1.如果你改变了maven依赖的版本可能会有报错,
2.注意word文档中汉字的字体

这里会显示你文档的汉字字体名称,其中有一些字体在转换的时候会消失,不能显示,就我知道有"宋体(正文)",注意它不同于“宋体”。
有错误欢迎指出,有好代码希望能分享一下。
附上参考资料地址:https://github.com/opensagres/xdocreport/wiki/DocxReportingJavaMainConverter
用java将简单的word文档换成pdf文档的更多相关文章
- 使用Spire PDF for .NET将HTML转换成PDF文档
目录 开发环境说明 Spire PDF for .NET (free edition)体验 资源下载 开发环境说明 Microsoft Visual Studio 2013 Ultimate Edit ...
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- C#实现文档转换成PDF
网上有很多将doc.ppt.xls等类型的文档转换成pdf的方法,目前了解到的有两大类: 1.使用虚拟打印机将doc.ppt.xls等类型的文档 2.使用OFFICE COM组件 我采用了第二种方法实 ...
- 如何新建PDF文档,新建PDF文档的方法
新建PDF文件的话,有两种方式,一种是直接通过使用PDF编辑器http://bianji.xjpdf.com/来新建PDF文件,,还有一种就是将PDF文件转换成Word文件,然后在Word文件中添加, ...
- Linux不用使用软件把纯文本文档转换成PDF文件的方法
当你有一大堆文本文件要维护的时候,把它们转换成PDF文档会好一些.比如,PDF更适合打印,因为PDF文档有预定义布局.除此之外,还可以减少文档被意外修改的风险. 要将文本文件转换成PDF格式,你要按照 ...
- [Swift通天遁地]七、数据与安全-(8)创建普通PDF文档和加密PDF文档
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- java调用com组件将office文件转换成pdf
在非常多企业级应用中都涉及到将office图片转换成pdf进行保存或者公布的场景,由于pdf格式的文档方便进行加密和权限控制(类似于百度文库).总结起来眼下将office文件转换 成pdf的方法主要有 ...
- Java利用aspose-words将word文档转换成pdf(破解 无水印)
首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...
随机推荐
- 通过Git和GitHub项目管理
用Git来管理代码文件 安装环境 windows 首先是安装git: 1.到git官网下载一个安装包 2.安装git,详细过程略 3.打开项目文件夹,并鼠标右击,打开git bash 4.从未使用过g ...
- Django Redis配置
Django Redis配置 # Django默认不支持redis,需要第三方插件来支持 pipenv install django-redis pipenv install hiredis # 不是 ...
- zabbix-proxy及ELK
1.添加tomcat监控模版 yum install java-1.8.0-openjdk tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp ...
- zabbix-web切换为nginx及https
目录 zabbix-web切换为nginx及https 1.背景和环境 2.安装nginx 2.1.编译参数 2.2.修改配置文件并配置https 2.3.配置nginx为系统服务 3.安装php 3 ...
- Jmeter 中正则表达式提取器Regular Expression Extractor
正则表达式提取器点击后置处理器中Post Processors 中的正则表达式提取器 Regular Expression Extractor Appy to: 表示作用于哪一个请求Main samp ...
- InteiiJ IDEA中如何制定制定哪一个配置文件
项目下有好些application.property文件 彼此之间也不是什么 从application.property中指定dev就去对应dev的关系 就想用我本地的数据库 于是添加了一个appl ...
- Java 使用Builder解决构造函数参数过多的问题
原文:https://blog.csdn.net/michael_f2008/article/details/77715075 //Builder Pattern public class Nutri ...
- 【转】spring bean 卸载
spring bean 卸载起因: 群里的一个朋友问到: 关于配置destory-method, springboot中 yml如何指定 首先介绍 bean卸载的三种形式 自定义destory-met ...
- SQL进阶系列之1CASE表达式
配置环境: 下载地址:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads#windows 使用数据库: C:\Po ...
- 转 C#关于DateTime得到的当前时间的格式和用法
DateTime.Now.ToShortTimeString() DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.T ...