`
zhuyufufu
  • 浏览: 134647 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

文档展示:使用OpenOffice转换文档为PDF

阅读更多
上接 文档展示
http://zhuyufufu.iteye.com/admin/blogs/2009600

本篇实现使用OpenOffice转换文档为PDF功能

上代码:

package com.zas.openoffice.test;

import java.io.File;
import java.net.ConnectException;

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;

public class OpenOfficeConvertToPDFTest {
	/**
	 * 转换函数
	 * 
	 * @param sourceFile
	 * @param destFile
	 * @return
	 */
	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();
			}
			// 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();
			return 0;
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	public static void main(String[] args) {
		String filePath = "D:\\pdf\\2010110系统需求分析说明书正式.doc";
		String outputFilePath = "D:\\pdf\\ppt\\2010110系统需求分析说明书正式.pdf";
		OpenOfficeConvertToPDFTest.office2PDF(filePath, outputFilePath);
	}
}



已测试转换PPT Doc 到PDF

当文档超过20M时转换很慢

使用OpenOffice可支持windows linux平台

对于文档展示,从技术上应该只剩下性能调优了。

年后再从功能完善角度实现文档展示功能。

参考文档:

http://www.iteye.com/topic/352103
http://iteye.blog.163.com/blog/static/186308096201211107128117/
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics