nutian 2014-03-13
准备工作
下载openoffice:
http://www.openoffice.org/download/index.html
下载swftools:
http://www.swftools.org/download.html
只用到pdf2swf.exe
以上安装文件要注意一点:安装目录避免带有空格
用openoffice将office文档生成pdf
/**
* office文档转pdf文件
* @param sourceFileoffice文档绝对路径
* @param destFilepdf文件绝对路径
* @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();
}
// 启动OpenOffice的服务
String command = OpenOffice_HOME
+ "/program/soffice.exe -headless -accept=\"socket,host="
+ host_Str + ",port=" + port_Str + ";urp;-nofirststartwizard &\"";
System.out.println("###\n" + command);
Process pro = Runtime.getRuntime().exec(command);
// 连接openoffice服务
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
host_Str, Integer.parseInt(port_Str));
connection.connect();
// 转换
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
System.out.println("inputFile:" + inputFile);
System.out.println("outputFile:" + outputFile);
converter.convert(inputFile, outputFile);
// 关闭连接和服务
connection.disconnect();
pro.destroy();
return 0;
} catch (FileNotFoundException e) {
System.out.println("文件未找到!");
e.printStackTrace();
return -1;
} catch (ConnectException e) {
System.out.println("OpenOffice服务监听异常!");
e.printStackTrace();
boolean flag = true;
System.out.println("sourceFile------" + sourceFile);
File srcFile = new File(sourceFile);
if (!srcFile.exists()) {
return -1; // 找不到源文件
}
System.out.println("<><><>" + srcFile.getName());
int fileLen = srcFile.getName().length();
String excPath = sourceFile.substring(0, sourceFile.length()-fileLen-1);
if(flag){
beginConvert(excPath,"",srcFile.getName());
flag = false;
}
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
将pdf文件转换成swf文件
private void convertPdf2Swf(String sourceFile, String outFile,
String toolFile) {
// -s languagedir=D:\\xpdf-chinese-simplified 用于处理pdf转换swf中文乱码问题
String command = toolFile + " \"" + sourceFile + "\" -o \"" + outFile
+ "\" -s flashversion=9 -s languagedir=D:\\xpdf-chinese-simplified";
if("Linux".equals(props.getProperty("os.name"))){
command = "/usr/bin/pdf2swf" + " " + sourceFile + " -o " + outFile
+ " -s flashversion=9 ";
}
System.out.println("<><><>" + command);
try {
Process process = Runtime.getRuntime().exec(command);
System.out.println("1--" + loadStream(process.getInputStream()));
System.err.println("2--" + loadStream(process.getErrorStream()));
System.out.println("3--" + loadStream(process.getInputStream()));
System.out.println("###--Msg: swf 转换成功");
} catch (Exception e) {
e.printStackTrace();
}
}
入口方法-通过此方法转换文件至swf格式
/**
* 入口方法-通过此方法转换文件至swf格式
* @param filePath上传文件所在文件夹的绝对路径
* @param dirPath文件夹名称
* @param fileName文件名称
* @return生成swf文件名
*/
public String beginConvert(String filePath, String dirName, String fileName) {
final String DOC = ".doc";
final String DOCX = ".docx";
final String XLS = ".xls";
final String XLSX = ".xlsx";
final String PDF = ".pdf";
final String SWF = ".swf";
final String TXT = ".txt";
final String ODT = ".odt";
final String PPT = ".ppt";
final String PPTX = ".pptx";
final String TOOL = "pdf2swf.exe";
String outFile = "";
String fileNameOnly = "";
String fileExt = "";
if (null != fileName && fileName.indexOf(".") > 0) {
int index = fileName.lastIndexOf(".");
fileNameOnly = fileName.substring(0, index);
fileExt = fileName.substring(index).toLowerCase();
}
String inputFile = filePath +File.separator+ fileName;
String outputFile = "";
String supportingFilePath = filePath +"(support)";
File file = new File(supportingFilePath);
if(!file.exists()){
file.mkdirs();
}
//如果是txt文档,先转为pdf文件
if(fileExt.equals(TXT)){
File docFile = new File(filePath +File.separator+ fileName);
File odtFile = new File(supportingFilePath +File.separator+fileNameOnly+ODT);
try {
FileUtil.copyFile(docFile, odtFile);
} catch (IOException e) {
e.printStackTrace();
}
inputFile = supportingFilePath +File.separator+fileNameOnly+ODT;
fileExt = ODT;
}
//如果是office文档,先转为odt文件
if (fileExt.equals(DOC) || fileExt.equals(DOCX) || fileExt.equals(XLS)
|| fileExt.equals(XLSX)|| fileExt.equals(ODT)|| fileExt.equals(PPT)
|| fileExt.equals(PPTX)) {
outputFile = supportingFilePath +File.separator+ fileNameOnly + PDF;
office2PDF(inputFile, outputFile);
inputFile = outputFile;
fileExt = PDF;
}
if (fileExt.equals(PDF)) {
String toolFile = propertyContext +"2/" + TOOL;
outputFile = supportingFilePath +File.separator+ fileNameOnly + SWF;
convertPdf2Swf(inputFile, outputFile, toolFile);
outFile = outputFile;
}
return outFile;
}
还有就是调用flexpaperviewer
var fp = new FlexPaperViewer(
'FlexPaperViewer',
'viewerPlaceHolder', { config : {
SwfFile :encodeURI('<%=basePath%>ueditor/jsp/<%=filePath%>'),
Scale : 1,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : true,
PrintEnabled : false,
FullScreenAsMaxWindow : true,
ProgressiveLoading : true,
MinZoomSize : 0.2,
MaxZoomSize : 1,
SearchMatchAll : true,
InitViewMode : 'Portrait',
PrintPaperAsBitmap : false,
PrintToolsVisible : false,
ViewModeToolsVisible : true,
ZoomToolsVisible : false,
NavToolsVisible : false,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'zh_CN'
}});