Java跨平台将word转为pdf(结合Jodconverter 和OpenOffice.org)

Java跨平台将word转为pdf

(结合Jodconverter开源框架 和OpenOffice.org办公软件)

Jodconverter的版本:jodconverter 2.2.1 

网上的流传的方法:

1. 安装OpenOffice 3     下载路径:http://zh.openoffice.org/new/zh_cn/downloads.html

 

2. 启动OpenOffice服务

 cd C:\Program Files\OpenOffice.org 3\program

         soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

3. 利用Jodconverter编写转换类

 

Java代码

public void convert(String input, String output){   

   File inputFile = new File(input);    

  1.         File outputFile = new File(output);    
  2.         OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);    
  3.         try {    
  4.             connection.connect();    
  5.             DocumentConverter converter = new OpenOfficeDocumentConverter(connection);    
  6.             converter.convert(inputFile, outputFile);    
  7.         } catch(Exception e) {    
  8.             e.printStackTrace();    
  9.         } finally {    
  10.             tryif(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}    
  11.         }    
  12.     }   
public void convert(String input, String output){ 
        File inputFile = new File(input); 
        File outputFile = new File(output); 
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); 
        try { 
            connection.connect(); 
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection); 
            converter.convert(inputFile, outputFile); 
        } catch(Exception e) { 
            e.printStackTrace(); 
        } finally { 
            try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){} 
        } 
    } 

 

 

以上方法需要启动OpenOffice的服务,麻烦且占内存。

后来,有人又整理了一个方法:直接在转换类中调用启动服务。

 

 

Java代码
  1. public static int office2PDF(String sourceFile, String destFile) {   
  2.   
  3.         String OpenOffice_HOME = "D:/Program Files/OpenOffice.org 3";// 这里是OpenOffice的安装目录,   
  4.                                                                         // 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是尽对没题目的   
  5.         // 假如从文件中读取的URL地址最后一个字符不是 '\',则添加'\'   
  6.         if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {   
  7.             OpenOffice_HOME += "/";   
  8.         }   
  9.         Process pro = null;   
  10.         try {   
  11.             // 启动OpenOffice的服务   
  12.             String command = OpenOffice_HOME   
  13.                     + "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";   
  14.             pro = Runtime.getRuntime().exec(command);   
  15.             // connect to an OpenOffice.org instance running on port 8100   
  16.             OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1"8100);   
  17.             connection.connect();   
  18.   
  19.             // convert   
  20.             DocumentConverter converter = new OpenOfficeDocumentConverter(connection);   
  21.             converter.convert(inputFile, outputFile);   
  22.   
  23.             // close the connection   
  24.             connection.disconnect();   
  25.             // 封闭OpenOffice服务的进程   
  26.             pro.destroy();   
  27.   
  28.             return 0;   
  29.         } catch (FileNotFoundException e) {   
  30.             e.printStackTrace();   
  31.             return -1;   
  32.         } catch (IOException e) {   
  33.             e.printStackTrace();   
  34.         } finally {   
  35.             pro.destroy();   
  36.         }   
  37.   
  38.         return 1;   
  39.     }  
public static int office2PDF(String sourceFile, String destFile) {

		String OpenOffice_HOME = "D:/Program Files/OpenOffice.org 3";// 这里是OpenOffice的安装目录,
																		// 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是尽对没题目的
		// 假如从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
		if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {
			OpenOffice_HOME += "/";
		}
		Process pro = null;
		try {
			// 启动OpenOffice的服务
			String command = OpenOffice_HOME
					+ "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
			pro = Runtime.getRuntime().exec(command);
			// connect to an OpenOffice.org instance running on port 8100
			OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
			connection.connect();

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

			// close the connection
			connection.disconnect();
			// 封闭OpenOffice服务的进程
			pro.destroy();

			return 0;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return -1;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			pro.destroy();
		}

		return 1;
	}
 

 

本人却在google code无意中发现jodconverter 的新版本jodconverter-core 3.0-beta-4。

但是不知道为什么用不了maven,只能下载包 。地址:https://code.google.com/p/jodconverter/downloads/list

并结合其他开发者的整理如下:

 

Java代码
  1.        /**  
  2.  * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice  
  3.  *   
  4.  * @param sourceFile  
  5.  *            源文件,绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx等.  
  6.  *   
  7.  * @param destFile  
  8.  *            目标文件.绝对路径.  
  9.  */  
  10. public static void word2pdf(String inputFilePath) {   
  11.     DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();   
  12.   
  13.     String officeHome = getOfficeHome();   
  14.     config.setOfficeHome(officeHome);   
  15.   
  16.     OfficeManager officeManager = config.buildOfficeManager();   
  17.     officeManager.start();   
  18.   
  19.     OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);   
  20.     String outputFilePath = getOutputFilePath(inputFilePath);   
  21.     File inputFile = new File(inputFilePath);   
  22.     if (inputFile.exists()) {// 找不到源文件, 则返回   
  23.         File outputFile = new File(outputFilePath);   
  24.         if (!outputFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径   
  25.             outputFile.getParentFile().mkdirs();   
  26.         }   
  27.         converter.convert(inputFile, outputFile);   
  28.     }   
  29.   
  30.     officeManager.stop();   
  31. }   
  32.   
  33. public static String getOutputFilePath(String inputFilePath) {   
  34.     String outputFilePath = inputFilePath.replaceAll(".doc"".pdf");   
  35.     return outputFilePath;   
  36. }   
  37.   
  38. public static String getOfficeHome() {   
  39.     String osName = System.getProperty("os.name");   
  40.     if (Pattern.matches("Linux.*", osName)) {   
  41.         return "/opt/openoffice.org3";   
  42.     } else if (Pattern.matches("Windows.*", osName)) {   
  43.         return "D:/Program Files/OpenOffice.org 3";   
  44.     } else if (Pattern.matches("Mac.*", osName)) {   
  45.         return "/Application/OpenOffice.org.app/Contents";   
  46.     }   
  47.     return null;   
  48. }  
        /**
	 * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice
	 * 
	 * @param sourceFile
	 *            源文件,绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx等.
	 * 
	 * @param destFile
	 *            目标文件.绝对路径.
	 */
	public static void word2pdf(String inputFilePath) {
		DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();

		String officeHome = getOfficeHome();
		config.setOfficeHome(officeHome);

		OfficeManager officeManager = config.buildOfficeManager();
		officeManager.start();

		OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
		String outputFilePath = getOutputFilePath(inputFilePath);
		File inputFile = new File(inputFilePath);
		if (inputFile.exists()) {// 找不到源文件, 则返回
			File outputFile = new File(outputFilePath);
			if (!outputFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径
				outputFile.getParentFile().mkdirs();
			}
			converter.convert(inputFile, outputFile);
		}

		officeManager.stop();
	}

	public static String getOutputFilePath(String inputFilePath) {
		String outputFilePath = inputFilePath.replaceAll(".doc", ".pdf");
		return outputFilePath;
	}

	public static String getOfficeHome() {
		String osName = System.getProperty("os.name");
		if (Pattern.matches("Linux.*", osName)) {
			return "/opt/openoffice.org3";
		} else if (Pattern.matches("Windows.*", osName)) {
			return "D:/Program Files/OpenOffice.org 3";
		} else if (Pattern.matches("Mac.*", osName)) {
			return "/Application/OpenOffice.org.app/Contents";
		}
		return null;
	}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智汇优库

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值