第一步:工具安装
1、? 将office文档转换成PDF,使用openoffice工具,此次代码示例将此工具安装在D:/OpenOffice目录下 ?如果要改路径则要改配置文件OpenOfficeService.properties中OO_HOME 的值,安装后启动服务,直接开启quickstart即可
2、将PDF转换成swf文件,使用pdf2swf工具,此次代码示例将此工具安装在D:/SWFTools目录下,改目录要改代码cmd命令的工具路径
3、代码
package utils;
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;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.ConnectException;
import java.util.ResourceBundle;
public class ConvertSwf
{
// ?public static void main(String[] args)
// ?{
// ? ?String outPath = new ConvertSwf().beginConvert("D:openOffice","openOffice","aa.doc");
// ? ?System.out.println("生成swf文件:" + outPath);
// ?}
? private static int pdf2swf=0;
? private static int office2pdf=0;
? public String beginConvert(String filePath,String dirName,String fileName)
? {
? ? String DOC = ".doc";
? ? String DOCX = ".docx";
? ? String XLS = ".xls";
? ? String XLSX = ".xlsx";
? ? String PDF = ".pdf";
? ? String SWF = ".swf";
? ? String TOOL = "pdf2swf.exe";
? ? String outFile = "";
? ? String fileNameOnly = "";
? ? String fileExt = "";
? ? if ((fileName != null) && (fileName.indexOf(".") > 0)) {
? ? ? int index = fileName.indexOf(".");
? ? ? fileNameOnly = fileName.substring(0,index);
? ? ? fileExt = fileName.substring(index).toLowerCase();
? ? }
? ? String inputFile = filePath + File.separator + fileName;
? ? String outputFile = "";
? ??
? ? if ((fileExt.equals(".doc")) || (fileExt.equals(".docx")) || (fileExt.equals(".xls")) ||?
? ? ? (fileExt.equals(".xlsx"))) {
? ? ? outputFile = filePath + File.separator + fileNameOnly + ".pdf";
? ??
? ? ??
? ? ? office2PDF(inputFile,outputFile);
? ? ? inputFile = outputFile;
? ? ? fileExt = ".pdf";
? ? }
? ? if (fileExt.equals(".pdf")) {
? ? ? String toolFile = filePath + File.separator + "pdf2swf.exe";
? ? ? outputFile = filePath + File.separator + fileNameOnly + ".swf";
? ? ? convertPdf2Swf(inputFile,outputFile,toolFile);
? ? ? outFile = outputFile;
? ? }
? ? System.out.println("outFile=============="+outFile);
? ? return outFile;
? }
/**
?* pdf转换成swf
?* @param sourceFile
?* @param outFile
?* @param toolFile
?*/
? private void convertPdf2Swf(String sourceFile,String outFile,String toolFile)
? {
? ? String command = "D:SWFToolspdf2swf.exe" + " "" + sourceFile + "" -o ?"" + outFile +?
? ? ? "" -s flashversion=9 ";
? ? try {
? ? ? Process process = Runtime.getRuntime().exec(command);
? ? ? System.out.println(loadStream(process.getInputStream()));
? ? ? System.err.println(loadStream(process.getErrorStream()));
? ? ? System.out.println(loadStream(process.getInputStream()));
? ? ? System.out.println("###--Msg: swf 转换成功");
? ? ? pdf2swf=1;
? ? }
? ? catch (Exception e) {
? ? ? System.out.println(e);
? ? ? e.printStackTrace();
? ? }
?
? }
/**
?* office文档转换成PDF
?* @param sourceFile
?* @param destFile
?* @return
?*/
? private int office2PDF(String sourceFile,String destFile)
? {
? ? ResourceBundle rb = ResourceBundle.getBundle("OpenOfficeService");
? ? String OpenOffice_HOME = rb.getString("OO_HOME");
? ? String host_Str = rb.getString("oo_host");
? ? String port_Str = rb.getString("oo_port");
? ? try {
? ? ? File inputFile = new File(sourceFile);
? ? ? if (!inputFile.exists()) {
? ? ? ? return -1;
? ? ? }
? ? ? File outputFile = new File(destFile);
? ? ? if (!outputFile.getParentFile().exists()) {
? ? ? ? outputFile.getParentFile().mkdirs();
? ? ? }
? ? ? String command = OpenOffice_HOME +?
? ? ? ? "/program/soffice.exe -headless -accept="socket,host=" +?
? ? ? ? host_Str + ",port=" + port_Str + ";urp;"";
? ? ? System.out.println("###n" + command);
? ? ? Process pro = Runtime.getRuntime().exec(command);
? ? ? OpenOfficeConnection connection = new SocketOpenOfficeConnection(
? ? ? ? host_Str,Integer.parseInt(port_Str));
? ? ? connection.connect();
? ? ? DocumentConverter converter = new OpenOfficeDocumentConverter(
? ? ? ? connection);
? ? ? converter.convert(inputFile,outputFile);
? ? ? connection.disconnect();
? ? ? pro.destroy();
? ? ??
? ? } catch (FileNotFoundException e) {
? ? ? System.out.println("文件未找到!");
? ? ? e.printStackTrace();
? ? ? return -1;
? ? } catch (ConnectException e) {
? ? ? System.out.println("OpenOffice服务监听异常!");
? ? ? e.printStackTrace();
? ? } catch (IOException e) {
? ? ? e.printStackTrace();
? ? }
?
? ? office2pdf=1;
? ? return 1;
? }
? static String loadStream(InputStream in) throws IOException {
? ? int ptr = 0;
? ? in = new BufferedInputStream(in);
? ? StringBuffer buffer = new StringBuffer();
? ? while ((ptr = in.read()) != -1) {
? ? ? buffer.append((char)ptr);
? ? }
? ? return buffer.toString();
? }
??
public static void main(String[] args) throws FileNotFoundException
{
? String outPath = new ConvertSwf().beginConvert("D:openOffice","1.docx");
?}
}
配置文件
OO_HOME = D:/OpenOffice oo_host = 127.0.0.1 oo_port =8100