须安装软件: 1.OOo_3.3.0_Win_x86_install-wJRE_zh-CN
2.swftools
说明:
1.启动OpenOffice
//此为文件安装的地址,视具体情况有不同
ProgramFiles\OpenOffice\program
//此为启动该服务的命令
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
2.swftools 也可以随意安装在任何目录下,但必须修改项目中必须配置相应的命令路径如: (C:\\Program Files\\SWFTools\\pdf2swf.exe -s flashversion=9 -o )地址为你相应的目录
由于安装文件过大,未上传; 如需要的话可以回复邮箱,我发附件
package com.format.test; import java.io.File; import java.io.IOException; import java.net.ConnectException; import java.util.UUID; import org.apache.commons.io.FileUtils; 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 FormatConversion { //转换路径命令 private String execSWFTools = "C:\\Program Files (x86)\\SWFTools\\pdf2swf.exe -s flashversion=9 -o "; //文件上传 public boolean upload(File uploadFile, String fileName) { boolean flag = false; /** 保存的具体路径 */ String savepath = "path"; /** 根据保存的路径创建file对象 */ File file = new File(savepath); //动态随机生成一个文件名 String storeName = UUID.randomUUID().toString(); //获取文件后缀 String extension = fileName.substring(fileName.indexOf(".")); /*这里其实是这样的,我将它们改为.odt后缀后,那么再用jodconverter来打印时,它就会调用openoffice来打印, 那么就相当于它用openoffice打开它的odt格式的文件,再点击打印成pdf;因为它对*.odt文件支持中文,所以它打开它时, 中文是正确的,其实它在读这个非odt的纯文本文件时,它就能识别出它的编码格式,所以就不用我们再去找那个原文件的编码格式, 再进行转换,我们可以直接打印。所以关键还是理解jodconverter的打印原理,它不过是对openoffice的打印接口的调用。*/ //转换txt文本文件为odt文件,解决中文乱码问题 if(extension.equals(".txt")){ extension=".odt"; } try { //类容拷贝到新文件, 创建新目录及文件 FileUtils.copyFile(uploadFile, new File(file, storeName + extension)); flag = true; } catch (IOException e) { e.printStackTrace(); } } public synchronized void conversion(String fileName) { String name = fileName.substring(0, fileName.lastIndexOf(".")); System.out.println("name" + name); String path = "D://uploaddFile/"; String file = path + fileName; System.out.println("file" + file); String swf = path + name + ".swf"; String tmp = path + name + ".pdf"; if (fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase() .equals("pdf")) { this.pdf2Swf(file, swf); } else { this.offic2Pdf(new File(file), new File(tmp)); this.pdf2Swf(tmp, swf); } } //转PDF private void offic2Pdf(File inputFile, File outputFile) { OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); } catch (ConnectException cex) { cex.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); connection = null; } } } //转SWF private void pdf2Swf(String pdfFile, String swfFile) { String command = execSWFTools +"\""+ swfFile + "\" -t \"" + pdfFile + "\""; System.out.println("Command语句为"+command); try { Runtime.getRuntime().exec(command); } catch (IOException e) { e.printStackTrace(); } } }