步驟一:(涉及到的工具)
在線查看使用FlexPaper軟件(http://flexpaper.devaldi.com/download/)
doc、docs、xls、xlsx轉換dpf工具openoffice(http://www.openoffice.org/download/index.html)
pdf轉成swf工具swftools-0.9.2(http://www.pc6.com/softview/SoftView_51612.html)
步驟二:(配置工程)
1、在項目中導入下載FlexPaper解壓後文件(js、css、swf文件等)
2、編寫一個html頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
html, body { height:100%; }
body { margin:0; padding:0; overflow:auto; }
#flashContent { display:none; }
</style>
<script type="text/javascript" src="../js/swfobject/swfobject.js"></script>
<script type="text/javascript" src="../js/flexpaper_flash.js"></script>
<script type="text/javascript">
<!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. -->
var swfVersionStr = "10.0.0";
<!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
/* var xiSwfUrlStr = "playerProductInstall.swf"; */
var xiSwfUrlStr = ""; //值可以任意
var flashvars = {
SwfFile : escape("lucene4.6.swf"), //swf文件路徑(lucene4.6和頁面同一個位置)
Scale : 0.6,
ZoomTransition : "easeOut",
ZoomTime : 0.5,
ZoomInterval : 0.1,
FitPageOnLoad : false,
FitWidthOnLoad : true,
PrintEnabled : true,
FullScreenAsMaxWindow : false,
ProgressiveLoading : true,
PrintToolsVisible : true,
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
FullScreenVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: "en_US"
};
var params = {
}
params.quality = "high";
params.bgcolor = "#ffffff";
params.allowscriptaccess = "sameDomain";
params.allowfullscreen = "true";
var attributes = {};
attributes.id = "FlexPaperViewer";
attributes.name = "FlexPaperViewer";
swfobject.embedSWF(
"FlexPaperViewer.swf", "flashContent",
"800", "600",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
swfobject.createCSS("#flashContent", "display:block;text-align:center;");
</script>
</head>
<body>
<div style="position:absolute;left:200px;top:10px;">
<div id="flashContent">
</div>
</div>
</body>
</html>
打開html頁面就可以運行,效果如下:
步驟三:pdf轉換成swf格式(借鑑別人)
1、下載 jodconverter-2.2.2 ,將lib目錄所有jar 複製到項目 lib目錄
2、寫一個java工具類(doc、dcox、xls、xlsx->pdf->swf)
package com.netsix.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.util.ResourceBundle;
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;
/**
* 將文件轉成swf格式
*
* @author Administrator
*
*/
public class ConvertSwf {
/**
* 入口方法-通過此方法轉換文件至swf格式
* @param filePath 上傳文件所在文件夾的絕對路徑
* @param fileName 文件名稱
* @return 生成swf文件名
*/
public String beginConvert(String filePath, 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 TOOL = "pdf2swf.exe";
String outFile = "";
String fileNameOnly = "";
String fileExt = "";
if (null != fileName && fileName.lastIndexOf(".") > 0) {
int index = fileName.lastIndexOf(".");
fileNameOnly = fileName.substring(0, index);
fileExt = fileName.substring(index).toLowerCase();
}
String inputFile = filePath + File.separator + fileName;
String outputFile = "";
//如果是office文檔,先轉爲pdf文件
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 + TOOL;
outputFile = filePath + File.separator + fileNameOnly + SWF;
convertPdf2Swf(inputFile, outputFile, toolFile);
outFile = outputFile;
}
return outFile;
}
/**
* 將pdf文件轉換成swf文件
* @param sourceFile pdf文件絕對路徑
* @param outFile swf文件絕對路徑
* @param toolFile 轉換工具絕對路徑
*/
private void convertPdf2Swf(String sourceFile, String outFile,
String toolFile) {
String command = toolFile + " \"" + 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 轉換成功");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* office文檔轉pdf文件
* @param sourceFile office文檔絕對路徑
* @param destFile pdf文件絕對路徑
* @return
*/
private int office2PDF(String sourceFile, String destFile) {
ResourceBundle rb = ResourceBundle.getBundle("OpenOfficeService");
String OpenOffice_HOME = "D:\\Program Files\\OpenOffice 4"; //openoffice安裝路徑
String host_Str = "127.0.0.1"; //地址
String port_Str = "8100"; //端口號
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1; // 找不到源文件
}
// 如果目標路徑不存在, 則新建該路徑
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
// 如果從文件中讀取的URL地址最後一個字符不是 '\',則添加'\'
if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
OpenOffice_HOME += "\\";
}
// 啓動OpenOffice的服務
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);
// 連接openoffice服務
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();
return 0;
} catch (FileNotFoundException e) {
System.out.println("文件未找到!");
e.printStackTrace();
return -1;
} catch (ConnectException e) {
System.out.println("OpenOffice服務監聽異常!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
}
}
測試類
package com.netsix.test;
import com.netsix.util.ConvertSwf;
public class MainClass {
public static void main(String[] args) {
String dirPath = "D:\\workspace\\20140403_WebScaner\\pdf"; // 下載swftools-0.9.2.exe 安裝後,複製安裝目錄中 pdf2swf.exe 到項目附件目錄中。
String fileName = "lucene4.6.doc";
String outPath = new ConvertSwf().beginConvert(dirPath,fileName);
System.out.println("生成swf文件:" + outPath);
}
}
demo例子:http://download.csdn.net/detail/lin062854/7145653