当前位置: 移动技术网 > IT编程>开发语言>Java > Java利用openoffice将doc、docx转为pdf实例代码

Java利用openoffice将doc、docx转为pdf实例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

王保安 老大震怒,张衡地震仪,情侣道歉短信

本文研究的主要是java编程利用openoffice将doc、docx转为pdf的实现代码,具体如下。

1. 需要用的软件

openoffice , jodconverter

2.启动openoffice的服务

我到网上查如何利用openoffice进行转码的时候,都是需要先用cmd启动一个soffice服务,启动的命令是:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"。

但是实际上,对于我的项目,进行转码只是偶尔进行,然而当openoffice的转码服务启动以后,该进程(进程名称是soffice.exe)会一直存在,并且大约占100m的内存,感觉非常浪费。于是我就想了一个办法,可以将执行该服务的命令直接在java代码里面调用,然后当转码完成的时候,直接干掉这个进程。在后面的java代码里面会有解释。

所以,实际上,这第2步可以直接跳过

3.将jodconverter相关的jar包添加到项目中

将jodconverter解压缩以后,把lib下面的jar包全部添加到项目中

注意:安装openoffice

4. 下面就是重点喽,详见java代码解析

package cn;
import java.io.bufferedreader;
import java.io.file;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.inputstreamreader;
import com.artofsolving.jodconverter.documentconverter;
import com.artofsolving.jodconverter.openoffice.connection.openofficeconnection;
import com.artofsolving.jodconverter.openoffice.connection.socketopenofficeconnection;
import com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter;
/** 
 * office转化为pdf 
 * pdf转化为swf文件 
 * @author administrator 
 * 
 */
public class converter {
	private static string openofficepath = "e:\\安装软件\\openoffice\\date";
	//openoffice软件的安装路径 
	/** 
   * 将office文档转换为pdf. 运行该函数需要用到openoffice和jodconverter-2.2.2 
   * <pre> 
   * 方法示例: 
   * string sourcepath = "f:\\office\\source.doc"; 
   * string destfile = "f:\\pdf\\dest.pdf"; 
   * converter.office2pdf(sourcepath, destfile); 
   * </pre> 
   *  
   * @param sourcefile 
   *      源文件, 绝对路径. 可以是office2003-2007全部格式的文档, office2010的没测试. 包括.doc, 
   *      .docx, .xls, .xlsx, .ppt, .pptx等. 示例: f:\\office\\source.doc 
   * @param destfile 
   *      目标文件. 绝对路径. 示例: f:\\pdf\\dest.pdf 
   * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0, 
   *     则表示操作成功; 返回1, 则表示转换失败 
   */
	public static int office2pdf(string sourcefile, string destfile) {
		try {
			file inputfile = new file(sourcefile);
			if (!inputfile.exists()) {
				return -1;
				// 找不到源文件, 则返回-1
			}
			// 如果目标路径不存在, 则新建该路径  
			file outputfile = new file(destfile);
			if (!outputfile.getparentfile().exists()) {
				outputfile.getparentfile().mkdirs();
			}
			string openoffice_home = openofficepath;
			//这里是openoffice的安装目录  
			// 如果从文件中读取的url地址最后一个字符不是 '\',则添加'\'  
			if (openoffice_home.charat(openoffice_home.length() - 1) != '\\') {
				openoffice_home += "\\";
			}
			// 启动openoffice的服务  
			string command = openoffice_home  
			          + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;
			urp;
			\"";
			process 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();
		}
		return 1;
	}
	public static void main(string []args) throws exception {
		string sourcepath = "c:\\users\\administrator\\desktop\\1\\分组情况一览表.xls";
		string destfile = "c:\\users\\administrator\\desktop\\1\\dest.pdf";
		int flag = converter.office2pdf(sourcepath, destfile);
		if (flag == 1) {
			system.out.println("转化失败");
		} else if(flag == 0){
			system.out.println("转化成功");
		} else {
			system.out.println("找不到源文件, 或url.properties配置错误");
		}
	}
}

总结

以上就是本文关于java利用openoffice将doc、docx转为pdf实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网