java將doc文件轉換爲pdf文件的三種方法

http://feifei.im/archives/93php

——————————————————————————————————————————————html

項目要用到doc轉pdf的功能,一番google以後總結出了三種方法(免費方案),因而一一試了一下,作個總結記錄,下次要用直接查,省的忘了……

方法1.poi讀取doc + itext生成pdf (實現最方便,效果最差,跨平臺)

方法2.jodconverter + openOffice (通常格式實現效果還行,複雜格式容易有錯位,跨平臺)

方法3.jacob + msOfficeWord + SaveAsPDFandXPS (完美保持原doc格式,效率最慢,只能在windows環境下進行)

方法1:使用jdoctopdf來實現,這是一個封裝好的包,能夠把doc轉換成pdf,html,xml等格式,調用很方便
地址:http://www.maxstocker.com/jdoctopdf/downloads.php
須要本身導入poi包與itext包,須要注意的是itext要導入itext-2.1.5版本,新版本因爲包名不一樣,會出錯
也能夠本身根據網上的其餘教程根據須要本身寫方法來實現。
用jdoctopdf的實現方法以下:

java

public void doc2pdf(String docFileName) throws Exception{ String path = this.getSession().getServletContext().getRealPath("/")+"attachment/"; Parser p = new DocParser();// create a new parser instance FileInputStream fis = new FileInputStream(path+"/doc/"+ docFileName + ".doc");// creating InputStream for use with parser DocumentElement mydoc = p.parse(fis,true,false);// parse document from input stream DocWriter w = new PDFWriter();// create PDF writer w.writeDocument(mydoc,new FileOutputStream(path+"/pdf/"+docFileName + ".pdf"));// write document as pdf using writer w = new XHTMLWriter(); w.writeDocument(mydoc,new FileOutputStream(path+"/pdf/"+docFileName + ".html"));// write document as xhtml } public String materialUpload(){ try { doc2pdf("ttt"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } 

方法1轉化後pdf截圖:(itext轉中文須要額外配置,因此。。。一片空白,格式也錯位了)
111.png



方法2:使用jodconverter來調用openOffice的服務來轉換,openOffice有個各個平臺的版本,因此這種方法跟方法1同樣都是跨平臺的。
jodconverter的下載地址:http://www.artofsolving.com/opensource/jodconverter
首先要安裝openOffice,下載地址:http://www.openoffice.org/download/index.html
安裝完後要啓動openOffice的服務,具體啓動方法請自行google,windows

mac下的啓動方法爲終端輸入app

/Applications/OpenOffice.org.app/Contents/MacOS/soffice "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" -nologo -headless 

準備工做完成後在項目裏導入下載下來的包,而後加個方法就OK:less

public void createPdf(String docFileName) throws IOException{ String path = this.getSession().getServletContext().getRealPath("/")+"attachment/"; File inputFile = new File(path+"/doc/"+ docFileName + ".doc"); File outputFile = new File(path+"/pdf/"+docFileName + ".pdf"); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); // close the connection connection.disconnect(); } 

方法2的截圖(格式基本一致,有錯位)
22.pngsocket

方法3:效果最好的一種方法,可是須要window環境,並且速度是最慢的須要安裝msofficeWord以及SaveAsPDFandXPS.exe(word的一個插件,用來把word轉化爲pdf)
Office版本是2007,由於SaveAsPDFandXPS是微軟爲office2007及以上版本開發的插件
SaveAsPDFandXPS下載地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=7
jacob 包下載地址:http://sourceforge.net/projects/jacob-project/
我下的是jacob-1.17-M2.zip
下載下來的jacob裏的jar包導入到項目裏,
jacob的dll文件放到到你的jdk/jre/bin下面(不放會報錯:java.lang.NoClassDefFoundError: Could not initialize class com.jacob.com.Dispatch)ui

網上還有一種是把dll放在放在如下代碼輸出的路徑裏的任意一個路徑目錄this

System.getProperty("java.library.path"); 

這個我沒試過,應該也是能夠的
而後添加方法:google

static final int wdFormatPDF = 17;// PDF 格式 public void wordToPDF(String docFileName){ System.out.println("啓動Word..."); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch(); String path = this.getSession().getServletContext().getRealPath("/")+"attachment/"; String sfileName = path+"/doc/"+ docFileName + ".doc"; String toFileName = path+"/pdf/"+ docFileName + ".pdf"; doc = Dispatch.call(docs, "Open" , sfileName).toDispatch(); System.out.println("打開文檔..." + sfileName); System.out.println("轉換文檔到PDF..." + toFileName); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } Dispatch.call(doc, "SaveAs", toFileName, // FileName wdFormatPDF); long end = System.currentTimeMillis(); System.out.println("轉換完成..用時:" + (end - start) + "ms."); } catch (Exception e) { System.out.println("========Error:文檔轉換失敗:" + e.getMessage()); } finally { Dispatch.call(doc,"Close",false); System.out.println("關閉文檔"); if (app != null) app.invoke("Quit", new Variant[] {}); } //若是沒有這句話,winword.exe進程將不會關閉 ComThread.Release(); } 

須要注意的是,若是沒有安裝SaveAsPDFandXPS.exe的話會提示

========Error:文檔轉換失敗:Invoke of: SaveAs Source: Microsoft Word Description: 

方法3pdf最終轉換效果(格式徹底一致):
33.PNG

相關文章
相關標籤/搜索