開發環境:windows。請務必在windows操做系統中進行本操做,由於word須要在服務器上轉爲pdf。java
請在電腦上安裝word,注意是安裝word,不是安裝wps(這一點很是重要)。windows
請在電腦上安裝word轉pdf的插件:「軟件名:SaveAsPDFandXPS」,下載地址:http://download.csdn.net/download/tiandixuanwuliang/10006993服務器
請把系統須要調用的dll文件放置在java環境的jre的bin目錄下,例如:C:\Program Files\Java\jdk1.7.0_13\bin\jacob-1.18-x64.dll,(請注意本身電腦32位仍是64位)下載地址:http://download.csdn.net/download/tiandixuanwuliang/10007003app
將jacob.jar放到C:\Windows\System32目錄下。同時將其引入的工程文件中。ui
工程中的使用方式以下:spa
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * word文件轉成PDF * * @author Tepu */ public class Word2Pdf { /** * 不保存待定的更改。 */ static final int wdDoNotSaveChanges = 0; /** * word轉PDF 格式 */ static final int wdFormatPDF = 17; /** * ppt 轉PDF 格式 */ static final int ppSaveAsPDF = 32; public static void main(String[] args) { String source1 = "E:\\work\\wc_rcs\\target\\rcs\\report\\test.doc"; String target1 = "E:\\work\\wc_rcs\\target\\rcs\\report\\test.pdf"; Word2Pdf.word2pdf(source1, target1); } public static void word2pdf(String source, String target) { System.out.println("啓動Word"); long start = System.currentTimeMillis(); ActiveXComponent app = null; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", false); Dispatch docs = app.getProperty("Documents").toDispatch(); System.out.println("打開文檔" + source); Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch(); System.out.println("轉換文檔到PDF " + target); File tofile = new File(target); if (tofile.exists()) { tofile.delete(); } Dispatch.call(doc,"SaveAs", target, wdFormatPDF); Dispatch.call(doc, "Close", false); long end = System.currentTimeMillis(); System.out.println("轉換完成..用時:" + (end - start) + "ms."); } catch (Exception e) { System.out.println("========Error:文檔轉換失敗:" + e.getMessage()); } finally { if (app != null) { app.invoke("Quit", wdDoNotSaveChanges); } } } public void ppt2pdf(String source, String target) { System.out.println("啓動PPT"); long start = System.currentTimeMillis(); ActiveXComponent app = null; try { app = new ActiveXComponent("Powerpoint.Application"); Dispatch presentations = app.getProperty("Presentations").toDispatch(); System.out.println("打開文檔" + source); Dispatch presentation = Dispatch.call(presentations,"Open",true, true, false).toDispatch(); System.out.println("轉換文檔到PDF " + target); File tofile = new File(target); if (tofile.exists()) { tofile.delete(); } Dispatch.call(presentation,"SaveAs", target, ppSaveAsPDF); Dispatch.call(presentation, "Close"); long end = System.currentTimeMillis(); System.out.println("轉換完成..用時:" + (end - start) + "ms."); } catch (Exception e) { System.out.println("========Error:文檔轉換失敗:" + e.getMessage()); } finally { if (app != null) { app.invoke("Quit"); } } } public void excel2pdf(String source, String target) { System.out.println("啓動Excel"); long start = System.currentTimeMillis(); // 啓動excel(Excel.Application) ActiveXComponent app = new ActiveXComponent("Excel.Application"); try { app.setProperty("Visible", false); Dispatch workbooks = app.getProperty("Workbooks").toDispatch(); System.out.println("打開文檔" + source); Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[] { source, new Variant(false), new Variant(false) }, new int[3]).toDispatch(); Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { target, new Variant(57), new Variant(false), new Variant(57), new Variant(57), new Variant(false), new Variant(true), new Variant(57), new Variant(true), new Variant(true), new Variant(true) }, new int[1]); Variant f = new Variant(false); System.out.println("轉換文檔到PDF " + target); Dispatch.call(workbook, "Close", f); long end = System.currentTimeMillis(); System.out.println("轉換完成..用時:" + (end - start) + "ms."); } catch (Exception e) { System.out.println("========Error:文檔轉換失敗:" + e.getMessage()); } finally { if (app != null) { app.invoke("Quit", new Variant[] {}); } } } public boolean imgToPdf(String imgFilePath, String pdfFilePath) throws IOException { File file = new File(imgFilePath); if (file.exists()) { Document document = new Document(); FileOutputStream fos = null; try { fos = new FileOutputStream(pdfFilePath); PdfWriter.getInstance(document, fos); // 添加PDF文檔的某些信息,好比做者,主題等等 document.addAuthor("arui"); document.addSubject("test pdf."); // 設置文檔的大小 document.setPageSize(PageSize.A4); // 打開文檔 document.open(); // 寫入一段文字 // document.add(new Paragraph("JUST TEST ...")); // 讀取一個圖片 Image image = Image.getInstance(imgFilePath); float imageHeight = image.getScaledHeight(); float imageWidth = image.getScaledWidth(); int i = 0; while (imageHeight > 500 || imageWidth > 500) { image.scalePercent(100 - i); i++; imageHeight = image.getScaledHeight(); imageWidth = image.getScaledWidth(); System.out.println("imageHeight->" + imageHeight); System.out.println("imageWidth->" + imageWidth); } image.setAlignment(Image.ALIGN_CENTER); // //設置圖片的絕對位置 // image.setAbsolutePosition(0, 0); // image.scaleAbsolute(500, 400); // 插入一個圖片 document.add(image); } catch (DocumentException de) { System.out.println(de.getMessage()); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } document.close(); fos.flush(); fos.close(); return true; } else { return false; } } }