本篇文档实现功能,将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软件的更多相关文章

  1. java实现MsOffice文档向pdf文档转化

    本篇文档实现功能,将word和ppt文档的文件转化成pdf格式的文档 应用到jacob 第一步:下载压缩包 (1)jacob官网下载jacob压缩包 (2)网址:http://sourceforge. ...

  2. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  3. C#实现MS-Office文档转Pdf(Word、Execel、PowerPoint、Visio、Project)

    using System; using Microsoft.Office.Core; namespace Office { class Util { private Util() { } /// &l ...

  4. java将office文档pdf文档转换成swf文件在线预览

    第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...

  5. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...

  6. Java环境中,word文档转PDF直接打开浏览而非下载

    在平台上,需要把文档直接浏览而非下载,实现方法是先把文档转为PDF文件, 但在linux系统中确实汉字字库,所以转换失败,以下是解决方法 后面正式服务器也要添加字库,不然会转换出乱码文件,处理步骤如下 ...

  7. Java中几种office文档转pdf的方式

    最近公司要做office的文档,搜集了几种office文档转pdf的方式,简单的做下总结 我主要尝试了三种方式:openoffice,aspose,jacob 对他们进行了大文件,小文件,在linux ...

  8. word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法

    公司有个需求,需要将word转成pdf并且抽取首页用以展示,word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网 ...

  9. 使用OpenOffice.org将各类文档转为PDF

    http://blog.zhaojie.me/2010/05/convert-document-to-pdf-via-openoffice.html ————————————————————————— ...

随机推荐

  1. java Pattern(正则)类

    Pattern的静态方法matches 用于快速匹配字符串,该方法适合用于只匹配一次,且匹配全部字符串. Boolean b=Pattern.matches("^((13[0-9])|(15 ...

  2. 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)

    Problem A. Mischievous Problem Setter 签到. #include <bits/stdc++.h> using namespace std; #defin ...

  3. centos7修改hostname

    [root@centos7 ~]$ hostnamectl set-hostname prd_web1 # 使用这个命令会立即生效且重启也生效 [root@centos7 ~]$ hostname # ...

  4. 来自MSDN的RibbonGadgets练习

    来自MSDN的RibbonGadgets练习 RibbonGadgets是少见的来自官方的资源. 如果选择Menu Items = 集合,则会有向下的小箭头,同时会出现下来菜单. 这个都关系叫做gal ...

  5. 2018-2019-1 20189218《Linux内核原理与分析》第三周作业

    mykernel 实验 实验楼里按步骤运行一切顺利,make等待的时间特别久: 但是,启动mykernel后,实验楼的界面就不响应了,所以还是在自己虚拟机上做这个实验. 虚拟机搭建 mykernel ...

  6. K-Means 算法(Java)

    kMeans算法原理见我的上一篇文章.这里介绍K-Means的Java实现方法,参考了Python的实现方法. 一.数据点的实现 package com.meachine.learning.kmean ...

  7. Installing VirtualBox DKMS in Kali 2.0

    Kali linux is one of the mainly used operating system by the Ethical hackers and information securit ...

  8. POJ 1860 Currency Exchange(最短路&spfa正权回路)题解

    题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多? 思路:因为过程中可能有负权值,用spfa.求是否有正权回路,dis[ ...

  9. Redis之数据备份与恢复

    Redis 数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. 语法 redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实例 re ...

  10. java bitmap/bitvector的分析和应用

    转自: http://shmilyaw-hotmail-com.iteye.com/blog/1741608  简介     bitmap在很多海量数据处理的情况下会用到.一些典型的情况包括数据过滤, ...