java实现MsOffice文档向pdf转化之OpenOffice软件
本篇文档实现功能,将word和excel,ppt文档的文件转化成pdf格式的文档
第一步:下载第三方软件OpenOffice软件(不同的操作系统下载不同的版本)
下载地址:http://www.openoffice.org/
第二步:下载jodconverter压缩包
下载地址:http://www.artofsolving.com/opensource/jodconverter/
第三步:导入jar包
第四步直接调用工具类
MsOffice2Pdf类
package com.zdxy.shangxiaofei; import java.io.File;
import java.io.FileNotFoundException; import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager; public class MsOffice2Pdf { //jodconverter-core-30-beta-4.jar包中的一个对象
private static OfficeManager officeManager;
//服务器上OpenOffice软件安装路径
private static String OFFICE_HOME = "C:\\Program Files (x86)\\OpenOffice 4";
//转化端口
private static int port[] = { 8100 }; /**
* 将MsOffice文档或者txt文档转换成pdf文档的方法
* @Title: convert2PDF
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param inputFile
* @param outputFile
* @throws FileNotFoundException
* @return void 返回类型
* @author 尚晓飞
* @date 2014-8-19 上午11:29:28
*/
public static void convert2PDF(String inputFile, String outputFile) throws FileNotFoundException { //判断输入的文件路径是否存在
File file=new File(inputFile);
boolean flag=file.exists();
if(!flag){
System.out.println("指定转换的文件不存在");
return;
} String fileName = null; //如果传进来的txt文件,则需将txt文件转换成odt文件,才能转换成pdf文件
String fileName1 = inputFile.substring(0, inputFile.lastIndexOf("."));
if(inputFile.endsWith("txt")){
String odtFile = fileName1+".odt";
if(new File(odtFile).exists()){
System.out.println("odt文件存在");
inputFile = odtFile;
}else{
FileUtil.copyFile(inputFile, odtFile);
inputFile = odtFile;
}
}
startService();
System.out.println("进行文档转换:" + inputFile + " --> " + outputFile);
OfficeDocumentConverter converter = new OfficeDocumentConverter(
officeManager);
converter.convert(new File(inputFile), new File(outputFile));
stopService();
System.out.println(); } /**
* 打开OpenOffice软件
* @Title: startService
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return void 返回类型
* @author 尚晓飞
* @date 2014-8-19 上午11:11:06
*/
public static void startService() {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
System.out.println("准备启动openoffice服务....");
configuration.setOfficeHome(OFFICE_HOME);// // 设置OpenOffice.org安装目录
configuration.setPortNumbers(port); // 设置转换端口,默认为8100
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//设置任务队列超时为24小时 officeManager = configuration.buildOfficeManager();
officeManager.start(); // 启动服务
System.out.println("office转换服务启动成功!");
} catch (Exception ce) {
System.out.println("office转换服务启动失败" + ce);
ce.printStackTrace();
}
} /**
* 关闭OpenOffice软件
* @Title: stopService
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return void 返回类型
* @author 尚晓飞
* @date 2014-8-19 上午11:15:24
*/
public static void stopService() {
System.out.println("准备关闭openoffice转换服务....");
if (officeManager != null) {
officeManager.stop();
}
System.out.println("关闭openoffice转换服务成功!");
} /**
* 测试文件转换
* @Title: main
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param args
* @return void 返回类型
* @author 尚晓飞
* @date 2014-8-19 上午11:23:08
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String inputFile = "E:\\lantian.ppt";
String outputFile = "E:\\4.pdf"; try {
convert2PDF(inputFile, outputFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
FileUtil类
package com.zdxy.shangxiaofei; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class FileUtil { /**
* 将txt文件转换成odt文件
* @Title: copyFile
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param inputFile
* @param outputFile
* @throws FileNotFoundException
* @return void 返回类型
* @author 尚晓飞
* @date 2014-8-19 上午11:18:50
*/
public static void copyFile(String inputFile, String outputFile)
throws FileNotFoundException {
File sFile = new File(inputFile);
File tFile = new File(outputFile);
FileInputStream fis = new FileInputStream(sFile);
FileOutputStream fos = new FileOutputStream(tFile);
int temp = 0;
try {
while ((temp = fis.read()) != -1) {
fos.write(temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
java实现MsOffice文档向pdf转化之OpenOffice软件的更多相关文章
- java实现MsOffice文档向pdf文档转化
本篇文档实现功能,将word和ppt文档的文件转化成pdf格式的文档 应用到jacob 第一步:下载压缩包 (1)jacob官网下载jacob压缩包 (2)网址:http://sourceforge. ...
- Java实现office文档与pdf文档的在线预览功能
最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...
- C#实现MS-Office文档转Pdf(Word、Execel、PowerPoint、Visio、Project)
using System; using Microsoft.Office.Core; namespace Office { class Util { private Util() { } /// &l ...
- java将office文档pdf文档转换成swf文件在线预览
第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...
- Java实现web在线预览office文档与pdf文档实例
https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...
- Java环境中,word文档转PDF直接打开浏览而非下载
在平台上,需要把文档直接浏览而非下载,实现方法是先把文档转为PDF文件, 但在linux系统中确实汉字字库,所以转换失败,以下是解决方法 后面正式服务器也要添加字库,不然会转换出乱码文件,处理步骤如下 ...
- Java中几种office文档转pdf的方式
最近公司要做office的文档,搜集了几种office文档转pdf的方式,简单的做下总结 我主要尝试了三种方式:openoffice,aspose,jacob 对他们进行了大文件,小文件,在linux ...
- word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法
公司有个需求,需要将word转成pdf并且抽取首页用以展示,word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网 ...
- 使用OpenOffice.org将各类文档转为PDF
http://blog.zhaojie.me/2010/05/convert-document-to-pdf-via-openoffice.html ————————————————————————— ...
随机推荐
- 892. Surface Area of 3D Shapes
问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求这个3D多边形的表面积. Input: [[1,2],[3,4]] Output: 34 思路 只要 ...
- Android ActionBar自定义
关于自定义的ActionBar的实现过程,这里做下笔记以供之后查看. 1.默认状态 使用Android Studio新建一个名为“ActionBar”的应用,默认actionbar如图(1)所示. 图 ...
- Underscore-逐行分析
标签: // Underscore.js 1.8.3// http://underscorejs.org// (c) 2009-2015 Jeremy Ashkenas, DocumentCloud ...
- Oozie的详细启动步骤(CDH版本的3节点集群)
不多说,直接上干货! 我的机器情况是,bigdatamaster(Oozie是安装在这台节点上).bigdataslave1和bigdatalsave2. 启动步骤 第一:先启动mysqld服务进程 ...
- TED #05# How we can face the future without fear, together
Rabbi Lord Jonathan Sacks: How we can face the future without fear, together 1. what was it like bei ...
- dependencyManagement、parent与dependencies
本文总结自:https://www.cnblogs.com/feibazhf/p/7886617.html 当我们的项目很多时,为了适应子项目的依赖项使用统一的版本,我们可以创建一个parent项目来 ...
- linux各版本基线检查脚本(centos6、centos7、ubuntu系列)
以下是centos7基线检查脚本: #!/bin/bash #version v1. by pensar #操作系统linux 配置规范--centos7 cat <<EOF ****** ...
- 20145204《Java程序设计》第8周学习总结
20145204 <Java程序设计>第8周学习总结 教材学习内容总结 java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件,就可以在java平台中使用.使 ...
- 如何运行.ipynb文件
首先cmd下面输入: pip install jupyter notebook ,安装慢的改下pip的源为国内的源 然后cmd中输入: jupyter notebook就会弹出一个页面 先upload ...
- git使用合集
1.git 克隆时重命名本地文件夹或目录 如:git clone https://github.com/torvalds/linux.git linux_kernel 2.git查看tag git t ...