zoukankan      html  css  js  c++  java
  • java实现MsOffice文档向pdf转化之OpenOffice软件

    本篇文档实现功能,将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
  • 相关阅读:
    输入三个整数x、y、z,请把这三个数由小到大输出
    输入某年某月某日,判断这一天是这一年的第几天?
    JS实现背景透明度可变,文字不透明的效果
    tomcat+java的web程序持续占cpu问题调试
    java中HashMap在多线程环境下引起CPU100%的问题解决
    Java HashSet和LinkedHashSet的用法
    关于List Map Set的线程安全的问题
    java 程序消耗 cpu 100% 查找方法
    新浪微博开放平台——话题跟踪
    Mina 断线重连
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/3922060.html
Copyright © 2011-2022 走看看