java实现MsOffice文档向pdf转化之OpenOffice软件

简介: 本篇文档实现功能,将word和excel,ppt文档的文件转化成pdf格式的文档 第一步:下载第三方软件OpenOffice软件(不同的操作系统下载不同的版本)    下载地址:http://www.

本篇文档实现功能,将word和excel,ppt文档的文件转化成pdf格式的文档

第一步:下载第三方软件OpenOffice软件(不同的操作系统下载不同的版本)

   下载地址:http://www.openoffice.org/

第二步:下载jodconverter压缩包

   下载地址:http://www.artofsolving.com/opensource/jodconverter/

第三步:导入jar包

    

第四步直接调用工具类

MsOffice2Pdf类

 

  1 package com.zdxy.shangxiaofei;
  2 
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 
  6 import org.artofsolving.jodconverter.OfficeDocumentConverter;
  7 import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
  8 import org.artofsolving.jodconverter.office.OfficeManager;
  9 
 10 
 11 
 12 public class MsOffice2Pdf {
 13     
 14     //jodconverter-core-30-beta-4.jar包中的一个对象
 15     private static OfficeManager officeManager;
 16     //服务器上OpenOffice软件安装路径
 17     private static String OFFICE_HOME = "C:\\Program Files (x86)\\OpenOffice 4";
 18     //转化端口
 19     private static int port[] = { 8100 };
 20     
 21     
 22     /**
 23      * 将MsOffice文档或者txt文档转换成pdf文档的方法
 24     * @Title: convert2PDF 
 25     * @Description: TODO(这里用一句话描述这个方法的作用) 
 26     * @param inputFile
 27     * @param outputFile
 28     * @throws FileNotFoundException
 29     * @return void    返回类型 
 30     * @author 尚晓飞
 31     * @date 2014-8-19 上午11:29:28
 32      */
 33     public static void convert2PDF(String inputFile, String outputFile) throws FileNotFoundException {
 34        
 35         //判断输入的文件路径是否存在
 36         File file=new File(inputFile);
 37         boolean flag=file.exists();
 38         if(!flag){
 39             System.out.println("指定转换的文件不存在");
 40             return;
 41         }
 42         
 43         
 44         String fileName = null;
 45         
 46        //如果传进来的txt文件,则需将txt文件转换成odt文件,才能转换成pdf文件
 47         String fileName1 = inputFile.substring(0, inputFile.lastIndexOf("."));
 48         if(inputFile.endsWith("txt")){
 49             String odtFile = fileName1+".odt";
 50             if(new File(odtFile).exists()){
 51                 System.out.println("odt文件存在");
 52                 inputFile = odtFile;
 53             }else{
 54                 FileUtil.copyFile(inputFile, odtFile);
 55                 inputFile = odtFile;
 56             }
 57         }
 58         startService();
 59         System.out.println("进行文档转换:" + inputFile + " --> " + outputFile);
 60         OfficeDocumentConverter converter = new OfficeDocumentConverter(
 61                 officeManager);
 62         converter.convert(new File(inputFile), new File(outputFile));
 63         stopService();
 64         System.out.println();
 65 
 66     }
 67 
 68     /**
 69      * 打开OpenOffice软件
 70     * @Title: startService 
 71     * @Description: TODO(这里用一句话描述这个方法的作用) 
 72     * @return void    返回类型 
 73     * @author 尚晓飞
 74     * @date 2014-8-19 上午11:11:06
 75      */
 76     public static void startService() {
 77         DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
 78         try {
 79             System.out.println("准备启动openoffice服务....");
 80             configuration.setOfficeHome(OFFICE_HOME);// // 设置OpenOffice.org安装目录 
 81             configuration.setPortNumbers(port); // 设置转换端口,默认为8100
 82             configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//设置任务执行超时为5分钟  
 83             configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//设置任务队列超时为24小时  
 84 
 85             officeManager = configuration.buildOfficeManager();
 86             officeManager.start(); // 启动服务
 87             System.out.println("office转换服务启动成功!");
 88         } catch (Exception ce) {
 89             System.out.println("office转换服务启动失败" + ce);
 90             ce.printStackTrace();
 91         }
 92     }
 93 
 94     /**
 95      * 关闭OpenOffice软件
 96     * @Title: stopService 
 97     * @Description: TODO(这里用一句话描述这个方法的作用) 
 98     * @return void    返回类型 
 99     * @author 尚晓飞
100     * @date 2014-8-19 上午11:15:24
101      */
102     public static void stopService() {
103         System.out.println("准备关闭openoffice转换服务....");
104         if (officeManager != null) {
105             officeManager.stop();
106         }
107         System.out.println("关闭openoffice转换服务成功!");
108     }
109 
110     
111     
112     /**
113      * 测试文件转换
114     * @Title: main 
115     * @Description: TODO(这里用一句话描述这个方法的作用) 
116     * @param args
117     * @return void    返回类型 
118     * @author 尚晓飞
119     * @date 2014-8-19 上午11:23:08
120      */
121     public static void main(String[] args) {
122         // TODO Auto-generated method stub
123         String inputFile = "E:\\lantian.ppt";
124         String outputFile = "E:\\4.pdf";
125         
126         try {
127             convert2PDF(inputFile, outputFile);
128         } catch (FileNotFoundException e) {
129             // TODO Auto-generated catch block
130             e.printStackTrace();
131         }
132     }
133 }
View Code

 

 

 FileUtil类

 1 package com.zdxy.shangxiaofei;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 
 9 public class FileUtil {
10 
11     /**
12      * 将txt文件转换成odt文件
13     * @Title: copyFile 
14     * @Description: TODO(这里用一句话描述这个方法的作用) 
15     * @param inputFile
16     * @param outputFile
17     * @throws FileNotFoundException
18     * @return void    返回类型 
19     * @author 尚晓飞
20     * @date 2014-8-19 上午11:18:50
21      */
22     public static void copyFile(String inputFile, String outputFile)
23             throws FileNotFoundException {
24         File sFile = new File(inputFile);
25         File tFile = new File(outputFile);
26         FileInputStream fis = new FileInputStream(sFile);
27         FileOutputStream fos = new FileOutputStream(tFile);
28         int temp = 0;
29         try {
30             while ((temp = fis.read()) != -1) {
31                 fos.write(temp);
32             }
33         } catch (IOException e) {
34             e.printStackTrace();
35         } finally {
36             try {
37                 fis.close();
38                 fos.close();
39             } catch (IOException e) {
40                 e.printStackTrace();
41             }
42         }
43     }
44 
45 }
View Code

 

相关文章
|
3天前
|
JavaScript Java 测试技术
基于Java的物流配送人员车辆调度管理系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的物流配送人员车辆调度管理系统的设计与实现(源码+lw+部署文档+讲解等)
9 0
|
3天前
|
JavaScript Java 测试技术
基于Java的网上书城的设计与实现(源码+lw+部署文档+讲解等)
基于Java的网上书城的设计与实现(源码+lw+部署文档+讲解等)
18 0
|
3天前
|
JavaScript Java 测试技术
基于Java的网络游戏交易系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的网络游戏交易系统的设计与实现(源码+lw+部署文档+讲解等)
16 0
|
3天前
|
JavaScript Java 测试技术
基于Java的家政公司服务平台的设计与实现(源码+lw+部署文档+讲解等)
基于Java的家政公司服务平台的设计与实现(源码+lw+部署文档+讲解等)
20 1
|
3天前
|
JavaScript Java 测试技术
基于Java的怀旧唱片售卖系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的怀旧唱片售卖系统的设计与实现(源码+lw+部署文档+讲解等)
25 5
|
6天前
|
JavaScript Java 测试技术
基于Java的阅微文学网站的设计与实现(源码+lw+部署文档+讲解等)
基于Java的阅微文学网站的设计与实现(源码+lw+部署文档+讲解等)
16 2
|
7天前
|
JavaScript Java 测试技术
基于Java的智能实时疫情监管服务平台的设计与实现(源码+lw+部署文档+讲解等)
基于Java的智能实时疫情监管服务平台的设计与实现(源码+lw+部署文档+讲解等)
20 0
|
7天前
|
JavaScript Java 测试技术
基于Java的教师上课系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的教师上课系统的设计与实现(源码+lw+部署文档+讲解等)
23 0
|
7天前
|
JavaScript Java 测试技术
基于Java的校园心理健康网站的设计与实现(源码+lw+部署文档+讲解等)
基于Java的校园心理健康网站的设计与实现(源码+lw+部署文档+讲解等)
21 1
基于Java的校园心理健康网站的设计与实现(源码+lw+部署文档+讲解等)
|
7天前
|
JavaScript Java 测试技术
基于Java的数学竞赛网站的设计与实现(源码+lw+部署文档+讲解等)
基于Java的数学竞赛网站的设计与实现(源码+lw+部署文档+讲解等)
15 0