java實現線上預覽doc,txt,ppt,xls等文檔,用到了libreOffice,openOffice,SWFTools,JODConverter,flexPaper

來源:互聯網
上載者:User

涉及到的圖片請看附件。

libreOffice轉換的代碼:

public String doDocToFdpLibre() {

// File inputFile = new File("d:/1.txt");
// File inputFile = new File("d:/ppt.ppt");
// File inputFile = new File("d:/pptx.pptx");
// File inputFile = new File("d:/doc.doc");
// File inputFile = new File("d:/docx.docx");
// File inputFile = new File("d:/xls.xls");
File inputFile = new File("d:/jpg.jpg");
// File inputFile = new File("d:/gif.gif");
System.out.println("libreOffice開始轉換..............................");
Long startTime = new Date().getTime();
// 
// txt:使用libreOffice來轉換pdf,轉換成功,但是中文有亂碼!!!!!
// doc:這是office中的doc文檔,可以轉換成功,並且中文沒有亂碼
//docx:這是office中的docx文檔,可以轉換成功,並且中文沒有亂碼
//ppt和pptx:轉換成功。
//xls:轉換成功,沒有中文亂碼
//xlsx:轉換成功
//jpg和png:成功
String libreOfficePath = "D:/Program Files (x86)/LibreOffice 4";
// 此類在jodconverter中3版本中存在,在2.2.2版本中不存在
DefaultOfficeManagerConfiguration configuration = newDefaultOfficeManagerConfiguration();
// libreOffice的安裝目錄
configuration.setOfficeHome(new File(libreOfficePath));
// 連接埠號碼
configuration.setPortNumber(8100);
// configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//
// 設定任務執行逾時為5分鐘
// configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//
// 設定任務隊列逾時為24小時
OfficeManager officeManager = configuration.buildOfficeManager();
officeManager.start();
System.out.println("...start.....");
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File outputFile = new File("d:/jpgLibre.pdf");
converter.convert(inputFile, outputFile);
// converter.convert(inputFile, stw, outputFile, pdf); 
// 轉換結束
System.out.println("轉換結束。。。。。");
String pdfPath = outputFile.getPath();
long endTime = new Date().getTime();
long time = endTime - startTime;
System.out.println("libreOffice轉換所用時間為:"+time);
return pdfPath;
}

 

openOffice轉換的代碼:

public String doDocToFdpOpen() {
// File inputFile = new File("d://unicode.txt");
// File inputFile = new File("d:/pptx.pptx");
// File inputFile = new File("d:/ppt.ppt");
// File inputFile = new File("d:/docx.docx");
// File inputFile = new File("d:/doc.doc");
// File inputFile = new File("d:/xlsx.xlsx");
// File inputFile = new File("d:/jpg.jpg");
File inputFile = new File("d:/gif.gif");
System.out
.println("openOffice開始轉換....................................");
// txt:使用openoffice來轉換pdf,轉換成功,但有亂碼!!!!!
// 原因是編碼格式不統一,如果源檔案是utf-8的檔案的話,就不會有亂碼
//doc,docx,xls,xlsx:轉換成功,沒有亂碼
//!!!!!jpg,png,gif無法轉換
//ppt.pptx可以轉換

long startTime = new Date().getTime();
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
e.printStackTrace();
}
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
File outputFile = new File("d:/gifOpen.pdf");
converter.convert(inputFile, outputFile);
connection.disconnect();
String pdfPath = outputFile.getPath();
long endTime = new Date().getTime();
long time = endTime - startTime;
System.out.println("openOffice所用時間為:"+time);
return pdfPath;
}

 

pdf格式轉為swf格式的代碼:

public void doPdfToSwf(String pdfPath){
Runtime r = Runtime.getRuntime(); 
System.out.println("pdfPath:"+pdfPath);
String swfPath = pdfPath.substring(0,pdfPath.lastIndexOf('.'));
System.out.println("D:/Program Files (x86)/SWFTools/pdf2swf.exe -t"+pdfPath+" -s flashversion=9 -o "+swfPath+".swf");
try {
Process p = r.exec("D:/Program Files (x86)/SWFTools/pdf2swf.exe -t"+pdfPath+" -s flashversion=9 -o "+swfPath+".swf");

} catch (IOException e) {
e.printStackTrace();

}

 

建立web項目

將flexpaper中的js檔案拷到項目如圖所示的位置,然後建立html檔案,代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"xml:lang="en"> 
<head> 
<title>FlexPaper</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/flexpaper_flash.js"></script>
</head> 
<body> 
<div style="position:absolute;left:10px;top:10px;">
<a id="viewerPlaceHolder"style="width:680px;height:480px;display:block"></a>

<script type="text/javascript"> 
document.write(escape("d://ppt.swf"))
var fp = new FlexPaperViewer( 
'FlexPaperViewer',
'viewerPlaceHolder', { config : {
//注意地址的寫法
SwfFile : escape('d://docLibre.swf'),
Scale : 0.8, //縮放比例
ZoomTransition : 'easeOut',//縮放樣式
ZoomTime : 0.5,//變換縮放比例需要的時間
ZoomInterval : 0.2,//縮放比例之間的間隔
FitPageOnLoad : true,//初始化時自適應頁面
FitWidthOnLoad : true,//內容自適應頁面寬度
FullScreenAsMaxWindow : false,//可以最大化
ProgressiveLoading : false,//值為true的時候為逐步載入
MinZoomSize : 0.2,//最小縮放比例
MaxZoomSize : 5,//最大縮放比例
SearchMatchAll : false,//搜尋的時候,目標高顯示亮,測試後沒有效果
InitViewMode : 'Portrait',//啟動模式
PrintPaperAsBitmap : false,

ViewModeToolsVisible : true,//工具列上是否顯示樣式選擇框(就是顯示縮圖或分頁顯示的工具)
ZoomToolsVisible : true,//工具列上是否顯示縮放工具,包括全屏
NavToolsVisible : true,//工具列上是否顯示導航工具(也就是頁碼工具)
CursorToolsVisible : true,//工具列上是否顯示光線標工具
SearchToolsVisible : true,//工具列上是否顯示搜尋

// localeChain: 'en_US'//語言
localeChain: 'zh_CN'
}});
</script>
</div>
</body> 
</html>

注意:

填寫好產生的swf檔案的地址

運行html檔案

如果出現如圖所示的載入狀態,請點擊這個連結:http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065,

選擇左邊第三項,然後將剛才建立html檔案的地址加到右下方的位置,選擇始終允許。

然後再次運行html檔案,將會線上正確顯示swf檔案。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.