




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、java將office文檔pdf文檔轉換成swf文件在線預覽第一步,安裝 是一套sun的開源office辦公套件,能在widows,linux,solaris等操作系統(tǒng)上執(zhí)行。主要模塊有writer(文本文檔),impress(演示文稿),calc(電子表格),draw(繪圖),math(公式),base(數(shù)據(jù)庫)筆者下載的是 3.3.0。下載完直接安裝即可。 但是,我們還需要啟動openoffice server。有兩種做法: 1.以命令行方式啟動openoffice server,缺點是每次系統(tǒng)重啟,都需要
2、手動去把openoffice server啟動。 2.將openoffice server作為操作系統(tǒng)的服務啟動,既然成為了系統(tǒng)服務,就可以設定開機自動啟動了。 我們先來看第一種方式,1.以命令行方式啟動openoffice server在cmd命令下,cd opeonofiice的安裝路徑/program 如:cd c:program 3programsoffice -headless -accept=socket,host=,port=8100;urp; -nofirststartwizard2.以系統(tǒng)服務的方式啟動 這里我們還需要
3、windows resource kit tools ,將openoffice server設為系統(tǒng)服務。windows resource kit tools 是微軟專為管理人員、開發(fā)人員和高級用戶開發(fā)的,包括管理活動目錄、組策略、tcp/ip網(wǎng)絡、注冊表、系統(tǒng)安全、監(jiān)測等涉及windows server 2003 操作系統(tǒng)的其它很多方面的非常規(guī)安裝的工具組件。resource kit tools for xp的發(fā)布使得xp用戶也能使用resource kit tools對這些問題進行處理。 下載windows resource kit tools,我們進行默認安裝。 1.打開windows
4、resource kit tools 在command shell執(zhí)行以下命令:c:program fileswindows resource kitstoolsinstsrv openofficeunoserver c:program fileswindows resource kitstoolssrvany.exe打開 管理工具-服務 可以找到以 openofficeunoserver 命名的服務 2.打開注冊表尋找以下路徑 hkey_local_machine - system -controlset001 -services -openofficeunoserver新建項 parame
5、ters,在該項下添加兩個字符串值:key:application value:c:program filesopeno 3programsoffice.exe key:appparametersvalue:-invisible -headless -accept=socket,host=,port=8100;urp; -nofirststartwizard 3.在服務控制臺,啟動 openoffice 服務 4.在cmd中用以下命令查看8100是否已被監(jiān)聽:netstat -anop tcp這樣openoffice3.0就以服務方式運行在windows系統(tǒng)
6、上了。(使用cmd命令:netstat -anp tcp查看8100端口是否工作)然後可以通過socket方式連接openoffice,以使用openoffice提供的某些服務,如文件轉換服務,ms office轉pdf等等。開源項目 jodconverter 就是結合openoffice來進行文檔轉換的java組件。另外有一個命令行工具swftools,該工具可以將pdf轉換為swf格式的文檔,提供給ie客戶端流覽。另外,我們可以將該配置用bat文件來快速實現(xiàn),運行前請先修改相應目錄參數(shù): openoffice service.bat文件 c:program fileswindows res
7、ource kitstoolsinstsrv openofficeunoserver c:program fileswindows resource kitstoolssrvany.exe reg add hkey_local_machinesystemcontrolset001servicesopenofficeunoserverparameters /ve /d reg add hkey_local_machinesystemcontrolset001servicesopenofficeunoserverparameters /v application /t reg_sz /d c:pr
8、ogram filesopeno 3programsoffice.exe reg add hkey_local_machinesystemcontrolset001servicesopenofficeunoserverparameters /v appparameters /t reg_sz /d -invisible -headless -accept=socket,host=,port=8100;urp; -nofirststartwizard第二步,使用jodconverter將office文檔轉換為pdfjodconverter是一個java的ope
9、nducument文件轉換器,可以進行許多文件格式的轉換,它利用openoffice來進行轉換工作,它能進行以下的轉換工作: 1.microsoft office格式轉換為openducument,以及openducument轉換為microsoft office 2.openducument轉換為pdf,word、excel、powerpoint轉換為pdf,rtf轉換為pdf等。它是一個開源項目。我的項目是在myeclipse下開發(fā)的。下載最新版的jodconverter-2.2.2,把lib文件夾的包導入到你的docconverter項目的lib文件夾內。(假設你的項目是docconve
10、rter)新建doc2pdfutil.javapackage com.iori.webapp.util;import java.io.file; import java.io.ioexception;import .connectexception; import java.util.date; import com.artofsolving.jodconverter.documentconverter; import com.artofsolving.jodconverter.openoffice.connection.openofficeconnection; import com.art
11、ofsolving.jodconverter.openoffice.connection.socketopenofficeconnection; import com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter; public class doc2pdfutil extends java.lang.thread private file inputfile;/ 需要轉換的文件 private file outputfile;/ 輸出的文件 public doc2pdfutil(file i
12、nputfile, file outputfile) this.inputfile = inputfile; this.outputfile = outputfile; public void doctopdf() date start = new date(); openofficeconnection connection = new socketopenofficeconnection(8100); try connection.connect(); documentconverter converter = new openofficedocumentconverter(connect
13、ion); converter.convert(inputfile, outputfile); catch (connectexception cex) cex.printstacktrace(); finally / close the connection if (connection != null) connection.disconnect(); connection = null; /* * 由于服務是線程不安全的,所以需要啟動線程 */ public void run() this.doctopdf(); public file getinputfile() return inp
14、utfile; public void setinputfile(file inputfile) this.inputfile = inputfile; public file getoutputfile() return outputfile; public void setoutputfile(file outputfile) this.outputfile = outputfile; /* * 測試main方法 * param args */ public static void main(string args) file inputfile = new file(c:/temp/33
15、3.xls); file outputfile = new file(c:/temp/333.pdf); doc2pdfutil dp=new doc2pdfutil(inputfile,outputfile); dp.start(); 在doc2pdfutil.java,右鍵屬性 - run as - java application ,輸出main的測試結果。在jsp中執(zhí)行新建mydoc2pdftest.jsp simple jsp page place your content here在項目docconverter根目錄,右鍵屬性 - run as - myeclipse server
16、 application發(fā)布到之前安裝的tomcat 6.0的根目錄,然后用url路徑訪問:http:/localhost:8080/docconverter/mydoc2pdftest.jsp進行測試。jodconverter將office文檔轉換pdf,用到的代碼如下:file inputfile = new file(c:/temp/333.xls);file outputfile = new file(c:/temp/333.pdf); / 鏈接 一個運行在8100端口的openo 實例openofficeconnection connection = new soc
17、ketopenofficeconnection(8100);connection.connect(); / 創(chuàng)建一個converter對象并轉換格式documentconverter converter = new openofficedocumentconverter(connection);converter.convert(inputfile, outputfile); / 關閉連接connection.disconnect();第三步,使用swftools將pdf轉換為swf建議下載swftools-0.9.1,筆者起先下載的是最新版的swftools-1.0版。貌似轉換時出錯,缺少什
18、么組件。 繼續(xù)筆者的docconverter項目。筆者使用的開發(fā)環(huán)境是myeclipse 9.0。新建pdf2swfutil.javapackage com.iori.webapp.util;import java.io.bufferedreader;import java.io.ioexception;import java.io.inputstream;import java.io.inputstreamreader;public class pdf2swfutil /* * 利用swftools工具將pdf轉換成swf,轉換完后的swf文件與pdf同名 * author iori * p
19、aram filedir pdf文件存放路徑(包括文件名) * param exepath 轉換器安裝路徑 * throws ioexception */ public static synchronized void pdf2swf(string filedir, string exepath) throws ioexception /文件路徑 string filepath = filedir.substring(0, filedir.lastindexof(/); /文件名,不帶后綴 string filename = filedir.substring(filepath.length(
20、) + 1), filedir.lastindexof(.); process pro = null; if (iswindowssystem() /如果是windows系統(tǒng) /命令行命令 string cmd = exepath + + filedir + -o + filepath + / + filename + .swf; /runtime執(zhí)行后返回創(chuàng)建的進程對象 pro = runtime.getruntime().exec(cmd); else /如果是linux系統(tǒng),路徑不能有空格,而且一定不能用雙引號,否則無法創(chuàng)建進程 string cmd = new string3; cmd
21、0 = exepath; cmd1 = filedir; cmd2 = filepath + / + filename + .swf; /runtime執(zhí)行后返回創(chuàng)建的進程對象 pro = runtime.getruntime().exec(cmd); /非要讀取一遍cmd的輸出,要不不會flush生成文件(多線程) new dooutput(pro.getinputstream().start(); new dooutput(pro.geterrorstream().start(); try /調用waitfor方法,是為了阻塞當前進程,直到cmd執(zhí)行完 pro.waitfor(); cat
22、ch (interruptedexception e) e.printstacktrace(); /* * 判斷是否是windows操作系統(tǒng) * author iori * return */ private static boolean iswindowssystem() string p = system.getproperty(); return p.tolowercase().indexof(windows) = 0 ? true : false; /* * 多線程內部類 * 讀取轉換時cmd進程的標準輸出流和錯誤輸出流,這樣做是因為如果不讀取流,進程將死鎖 * auth
23、or iori */ private static class dooutput extends thread public inputstream is; /構造方法 public dooutput(inputstream is) this.is = is; public void run() bufferedreader br = new bufferedreader(new inputstreamreader(this.is); string str = null; try /這里并沒有對流的內容進行處理,只是讀了一遍 while (str = br.readline() != null
24、); catch (ioexception e) e.printstacktrace(); finally if (br != null) try br.close(); catch (ioexception e) e.printstacktrace(); /* * 測試main方法 * param args */ public static void main(string args) /轉換器安裝路徑 string exepath = c:/program files/swftools/pdf2swf.exe; try pdf2swfutil.pdf2swf(c:/temp/333.pdf
25、, exepath); catch (ioexception e) system.err.println(轉換出錯!); e.printstacktrace(); 在pdf2swfutil.java,右鍵屬性 - run as - java application ,輸出main的測試結果。在jsp中執(zhí)行新建mypdf2swftest.jsp simple jsp page place your content here在項目docconverter根目錄,右鍵屬性 - run as - myeclipse server application發(fā)布到之前安裝的tomcat 6.0的根目錄,然后
26、用url路徑訪問:http:/localhost:8080/docconverter/mypdf2swftest.jsp進行測試。第四步,office文檔轉為pdf,同時進一步轉為swf網(wǎng)上資料有很多office文檔轉為pdf,pdf轉為swf,但都是單步轉換。關于一起轉換的資料比較少。一起轉換有個問題就是轉為pdf時,這個轉換過程將花費一段時間才能成功,如何控制在pdf轉換成功后,才進行swf的轉換。以及多個文檔批量轉換又該怎么辦。 有幸筆者還是找到了一篇同時轉換的代碼:新建docconverter.javapackage com.iori.webapp.util;import java.i
27、o.bufferedinputstream;import java.io.file;import java.io.ioexception;import java.io.inputstream;import com.artofsolving.jodconverter.documentconverter;import com.artofsolving.jodconverter.openoffice.connection.openofficeconnection;import com.artofsolving.jodconverter.openoffice.connection.socketopen
28、officeconnection;import com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter;/* * doc docx格式轉換 * author administrator */public class docconverter private static final int environment=1;/環(huán)境1:windows 2:linux(涉及pdf2swf路徑問題) private string filestring; private string outputpath=
29、;/輸入路徑,如果不設置就輸出在默認位置 private string filename; private file pdffile; private file swffile; private file docfile; public docconverter(string filestring) ini(filestring); /* * 重新設置 file * param filestring */ public void setfile(string filestring) ini(filestring); /* * 初始化 * param filestring */ private
30、void ini(string filestring) this.filestring=filestring; filename=filestring.substring(0,filestring.lastindexof(.); docfile=new file(filestring); pdffile=new file(filename+.pdf); swffile=new file(filename+.swf); /* * 轉為pdf * param file */ private void doc2pdf() throws exception if(docfile.exists() if
31、(!pdffile.exists() openofficeconnection connection=new socketopenofficeconnection(8100); try connection.connect(); documentconverter converter=new openofficedocumentconverter(connection); converter.convert(docfile,pdffile); /close the connection connection.disconnect(); system.out.println(*pdf轉換成功,p
32、df輸出:+pdffile.getpath()+*); catch(.connectexception e) /todo auto-generated catch block e.printstacktrace(); system.out.println(*swf轉換異常,openoffice服務未啟動!*); throw e; catch(com.artofsolving.jodconverter.openoffice.connection.openofficeexception e) e.printstacktrace(); system.out.println(*swf轉換器異常,讀取轉
33、換文件失敗*); throw e; catch(exception e) e.printstacktrace(); throw e; else system.out.println(*已經(jīng)轉換為pdf,不需要再進行轉化*); else system.out.println(*swf轉換器異常,需要轉換的文檔不存在,無法轉換*); /* * 轉換成swf */ private void pdf2swf() throws exception runtime r=runtime.getruntime(); if(!swffile.exists() if(pdffile.exists() if(env
34、ironment=1)/windows環(huán)境處理 try process p=r.exec(c:/program files/swftools/pdf2swf.exe +pdffile.getpath()+ -o +swffile.getpath()+ -t 9); system.out.print(loadstream(p.getinputstream(); system.err.print(loadstream(p.geterrorstream(); system.out.print(loadstream(p.getinputstream(); system.err.println(*swf
35、轉換成功,文件輸出:+swffile.getpath()+*); if(pdffile.exists() pdffile.delete(); catch (exception e) e.printstacktrace(); throw e; else if(environment=2)/linux環(huán)境處理 try process p=r.exec(pdf2swf +pdffile.getpath()+ -o +swffile.getpath()+ -t 9); system.out.print(loadstream(p.getinputstream(); system.err.print(lo
36、adstream(p.geterrorstream(); system.err.println(*swf轉換成功,文件輸出:+swffile.getpath()+*); if(pdffile.exists() pdffile.delete(); catch (exception e) e.printstacktrace(); throw e; else system.out.println(*pdf不存在,無法轉換*); else system.out.println(*swf已存在不需要轉換*); static string loadstream(inputstream in) throws
37、 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 boolean conver() if(swffile.exists() system.out.println(*swf轉換器開始工作,該文件已經(jīng)轉換為swf*); return true; if(environm
38、ent=1) system.out.println(*swf轉換器開始工作,當前設置運行環(huán)境windows*); else system.out.println(*swf轉換器開始工作,當前設置運行環(huán)境linux*); try doc2pdf(); pdf2swf(); catch (exception e) / todo: auto-generated catch block e.printstacktrace(); return false; if(swffile.exists() return true; else return false; /* * 返回文件路徑 * param s
39、*/ public string getswfpath() if(swffile.exists() string tempstring =swffile.getpath(); tempstring=tempstring.replaceall(, /); return tempstring; else return ; /* * 設置輸出路徑 */ public void setoutputpath(string outputpath) this.outputpath=outputpath; if(!outputpath.equals() string realname=filename.substring(filename.lastindexof(/),filename.lastindexof(.); if(outputpath.charat(outputpath.length()=/
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2023七年級英語下冊 Unit 12 What did you do last weekend Section B 第4課時(2a-2c)教學設計 (新版)人教新目標版
- 2024-2025年高中語文 第4單元 14 《詩經(jīng)》兩首教學設計 粵教版必修1
- 歡迎加入我們-公司規(guī)章制度培訓
- 旅游規(guī)劃創(chuàng)新創(chuàng)業(yè)
- 2024年七年級地理上冊 2.1 大洲和大洋教學設計 (新版)新人教版
- 1自主選擇課余生活《課余生活我選擇》教學設計-2023-2024學年道德與法治五年級上冊統(tǒng)編版
- 13《玩轉巧妙萬花筒》 (教案)-二年級勞動北師大版
- 癲癇患者的護理小講課
- 血透導管封管操作流程
- 2023七年級語文上冊 第四單元 寫作 思路要清晰教學設計 新人教版
- 鈾礦冶安全規(guī)程
- 國標熱鍍鋅鋼管規(guī)格尺寸理論重量表
- 設計方案投標技術標文件
- 圓來如此簡單公開課優(yōu)質課件獲獎
- (本科)審計(第五版)全套教學課件完整版PPT
- GB∕T 3639-2021 冷拔或冷軋精密無縫鋼管
- 西師版六年級下冊數(shù)學第五單元 總復習 教案
- 拖欠貨款合同糾紛起訴狀范本
- 幼兒繪本故事:迪迪不想原諒人
- 碳酸丙烯酯法脫碳工藝工程設計
- 巧用繪本提升自閉癥兒童語言表達能力
評論
0/150
提交評論