当下做一个项目,就是各种操作office,客户的需求总是各种不按常理,来需求就得搞啊.对JAVA操作office这方面真是头大,弟弟是真滴不懂不会啊。无奈只好试啊试的。网上一大堆好使的,一大堆不好使的。整了一大堆word转PDF的方法。但是客户要求有水印,又不想花钱。硬着头皮试了一堆。虽说暂时还都没采用,但是收获也是有的。

以下都是亲测可用的(很多都是借鉴了前辈的博文)一来给自己用(毕竟我记性不好), 二来是希望能对看到本博客的帅哥靓妹们有点用处,免得一用都是不好使的。

以下方法都是转换结果比较可观的。。。。要用哪个自己斟酌

本博客的方法也许会不断的增加。有心自己看吧!!

----------------------------------------------------第一种方法 ------------------------------------------------

JobConverter + OpenOffice     ( Windows系统下 )

JobConverter的jar包下载地址 :  点我去下载

OpenOffice的下载地址 : 点我去下载

OpenOffice 的安装路径自己定义。 代码中要使用对应路径调用启动服务  (此处友情提示)

import java.io.File;
import java.io.IOException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

// 将word格式的文件转换为pdf格式
public static void WordToPDF(String startFile, String overFile) throws IOException {
// 源文件目录
File inputFile = new File(startFile);
if (!inputFile.exists()) {
System.out.println("源文件不存在!");
return;
}

// 输出文件目录
File outputFile = new File(overFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().exists();
}

// 调用openoffice服务线程
/** 我把openOffice下载到了 C:/Program Files (x86)/下 ,下面的写法自己修改编辑就可以**/
String command = "C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
Process p = Runtime.getRuntime().exec(command);

// 连接openoffice服务
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
connection.connect();

// 转换
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);

// 关闭连接
connection.disconnect();

// 关闭进程
p.destroy();
}

public static void main(String[] args) {
String start = "F:\\新建文件夹\\我是word测试文件.docx";
String over = "F:\\新建文件夹\\成了.pdf";
try {
WordToPDF(start, over);
} catch (IOException e) {
e.printStackTrace();
}
}
-

-

-

-

-

----------------------------------------------------第二种方法 ------------------------------------------------

SaveAsPDFandXPS + jacob (Windows操作系统下,电脑里有office)

SaveAsPDFandXPS 下载地址  :  点我去下载

jacob 的jar包 下载地址 : 点我去下载

先安装SaveAsPDFandXPS ,安装成功后 , 打开jacob的jar包  ,里面的结构应该是这样的

把jacob.jar 放到项目下  ,  然后 这里注意 把  两个后缀为dll的文件 放到 jre目录bin的里面   必须放进去  不然会报错(java.lang.NoClassDefFoundError) ,

实在不懂看此图    如果是默认安装 大概就是这个文件里面。

然后准备就绪  对应代码

import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

private static final int wdFormatPDF = 17;// PDF 格式
public static void wordToPDF(){

ActiveXComponent app = null;
Dispatch doc = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();

//转换前的文件路径
String startFile = "F:\\新建文件夹\\我是word版本" + ".doc";
//转换后的文件路劲
String overFile = "F:\\新建文件夹\\我是转换后的pdf文件" + ".pdf";

doc = Dispatch.call(docs, "Open" , startFile).toDispatch();
File tofile = new File(overFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,"SaveAs", overFile, wdFormatPDF);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
Dispatch.call(doc,"Close",false);
if (app != null)
app.invoke("Quit", new Variant[] {});
}
//结束后关闭进程
ComThread.Release();
}

public static void main(String[] args) {
wordToPDF();
}
-

-

-

-

-

-----------------------------------------------第三种方法----------------------------------------------

这种方法纯属是搜刮出来的,因为测试好用。所以也弄了上来。这个里面依赖的jar包挺麻烦的。但是只要导入jar包就可以获得立竿见影的效果,缺点可能就速度上比较上面两种能慢一些,还有就是支持的是docx的文档格式 。

我把所有jar包一点一点的搜集起来整合了一下。(jar包的版本也会造成问题。所以我整合了一版)

大家可以去这里下载→  整合版jar包下载地址

这个代码相比较上面两种就比较容易了。导入jar包就能用。

废话不说。贴代码

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.poi.xwpf.converter.core.utils.StringUtils;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class WordToPDF {

/**
* 将word文档, 转换成pdf, 中间替换掉变量
* @param source 源为word文档, 必须为docx文档
* @param target 目标输出
* @param params 需要替换的变量
* @throws Exception
*/
public static void wordConverterToPdf(InputStream source,
OutputStream target, Map<String, String> params) throws Exception {
wordConverterToPdf(source, target, null, params);
}

/**
* 将word文档, 转换成pdf, 中间替换掉变量
* @param source 源为word文档, 必须为docx文档
* @param target 目标输出
* @param params 需要替换的变量
* @param options PdfOptions.create().fontEncoding( "windows-1250" ) 或者其他
* @throws Exception
*/
public static void wordConverterToPdf(InputStream source, OutputStream target,
PdfOptions options,
Map<String, String> params) throws Exception {
XWPFDocument doc = new XWPFDocument(source);
paragraphReplace(doc.getParagraphs(), params);
for (XWPFTable table : doc.getTables()) {
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
paragraphReplace(cell.getParagraphs(), params);
}
}
}
PdfConverter.getInstance().convert(doc, target, options);
}

/** 替换段落中内容 */
private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {
if (MapUtils.isNotEmpty(params)) {
for (XWPFParagraph p : paragraphs){
for (XWPFRun r : p.getRuns()){
String content = r.getText(r.getTextPosition());
if(StringUtils.isNotEmpty(content) && params.containsKey(content)) {
r.setText(params.get(content), 0);
}
}
}
}
}

public static void main(String[] args) {
String filepath = "F:\\新建文件夹\\我是word.docx";
String outpath = "F:\\新建文件夹\\我是结果.pdf";

InputStream source;
OutputStream target;
try {
source = new FileInputStream(filepath);
target = new FileOutputStream(outpath);
Map<String, String> params = new HashMap<String, String>();

PdfOptions options = PdfOptions.create();

wordConverterToPdf(source, target, options, params);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

第一种效果不错,windows . linux 都可以配置。 linux有兴趣可以自己查阅 。

第二种最好,几乎不管水印还是格式都完美的转换。 但是只能支持windows 。

第三种最方便, 效果和第一种差不多,但是速度稍微慢了一些。

原文:https://blog.csdn.net/csdnFlyFun/article/details/79523262

最新文章

  1. 【Python】使用正则表达式实现计算器练习
  2. [MySQL] 号称永久解决了复制延迟问题的并行复制,MySQL5.7
  3. Good Bye 2015B(模拟或者二进制枚举)
  4. [转]如何让div中的内容垂直居中
  5. js控制input type=checkbox 的勾选
  6. SQL Server 复制 订阅与发布
  7. 【原】Storm分布式RPC
  8. Programming Assignment 1: Percolation
  9. ArcGIS学习记录&mdash;Arcgis中点、线、面的相互转换方法
  10. SQL PLUS远程连接
  11. lucene原理及源码解析--核心类
  12. Python 开发个人微信号在运维开发中的使用
  13. SQL Server查询中对于单列数据&#39;,&#39;分割的数据进行的拆分操作,集合的每一个行变多行
  14. Cell自适应高度及自定义cell混合使…
  15. DRF 商城项目 - 用户( 登录, 注册,登出,个人中心 ) 逻辑梳理
  16. LeetCode算法题-Reshape the Matrix(Java实现)
  17. Generative Adversarial Nets[Theory&amp;MSE]
  18. 思甜雅---关于qq的NABCD的模型分析
  19. 集合框架_DAY16
  20. 【题解】 bzoj2435: [Noi2011]道路修建 (傻逼题)

热门文章

  1. CUDA使用Event进行程序计时
  2. docker仓库操作
  3. 关闭使用ShellExecute打开的进程!!!!!
  4. Fragment 简介 基础知识 总结 MD
  5. FrameWork数据权限浅析3之基于角色的配置表实现行级数据安全
  6. [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
  7. (笔试题)关于C++的虚函数和多态性
  8. Android Activity的生命周期的几个方法介绍
  9. jtemplate 为javascript前端html模版引擎
  10. UML和模式应用学习笔记-2(迭代和进化式开发)