项目中之前用的office转pdf的插件是aspose
因为aspose无需world。所有转换操作都是在java虚拟机里边进行的。所以如果有图片特别多的文档转换的时候就会遇到oom。
而jacob是通过本地安装的office插件将文档再本地完成转换的。就不会出现oom的情况。而弊端就是项目就只能部署在windows server服务器上。
(上传的资源还在审核,等审核通过再补上jacob的链接)
jacob.jar 放在 C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext目录下
jacob.dll 放在 C:\Program Files\Java\jdk1.8.0_171\jre\bin 目录下
注意:所选版本要跟jdk一致,例如jdk使用的是32位的,jacob.dll也要选择32位的。要不然会报错
“Could not initialize class com.jacob.com.Dispatch”
网上这个报错信息的解决方法是:
这个方法对我没用,不过也放在这里供大家参考。
private static final int WORLD_TO_PDF_OPERAND = 17; // PDF 格式 public void wordToPDF(String sfileName, String toFileName) { 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(); 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 WORLD_TO_PDF_OPERAND); 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(); }
private static final Integer EXCEL_TO_PDF_OPERAND = 0; public void excelToPDF(String inFilePath, String outFilePath) { System.out.println("启动 Excel..."); long start = System.currentTimeMillis(); //pdf存在情况会出现转换错误 File file = new File(outFilePath); file.delete(); ActiveXComponent ax = null; Dispatch excel = null; try { ComThread.InitSTA(); ax = new ActiveXComponent("Excel.Application"); ax.setProperty("Visible", new Variant(false)); ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏 Dispatch excels = ax.getProperty("Workbooks").toDispatch(); Object[] obj = new Object[]{ inFilePath, new Variant(false), new Variant(false) }; excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch(); // 转换格式 Object[] obj2 = new Object[]{ new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0 outFilePath, new Variant(0) //0=标准 (生成的PDF图片不会变模糊) ; 1=最小文件 }; Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]); long end = System.currentTimeMillis(); System.out.println("转换完成..用时:" + (end - start) + "ms."); } catch (Exception es) { es.printStackTrace(); throw es; } finally { if (excel != null) { Dispatch.call(excel, "Close", new Variant(false)); } if (ax != null) { ax.invoke("Quit", new Variant[]{}); ax = null; } ComThread.Release(); } }
private static final Integer PPT_TO_PDF_OPERAND = 32; public void ppt2pdf(String inFilePath, String outFilePath){ System.out.println("启动 Excel..."); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch ppt = null; try { ComThread.InitSTA(); app = new ActiveXComponent("KWPP.Application"); Dispatch ppts = app.getProperty("Presentations").toDispatch(); /* * call param 4: ReadOnly param 5: Untitled指定文件是否有标题 param 6: WithWindow指定文件是否可见 */ ppt = Dispatch.call(ppts, "Open", inFilePath, true, true, false).toDispatch(); Dispatch.call(ppt, "SaveAs",outFilePath,PPT_TO_PDF_OPERAND); // ppSaveAsPDF为特定值32 // Dispatch.callN(ppt, "SaveAs", new Variant(pdfFilePath)); long end = System.currentTimeMillis(); System.out.println("转换完成..用时:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (ppt != null) { Dispatch.call(ppt, "Close"); } if (app != null) { app.invoke("Quit"); } ComThread.Release(); } }
写main方法测试一下:
public static void main(String[] args) { Excel2PdfModel d = new Excel2PdfModel(); d.wordToPDF("G:\\LYX\\f\\11111111111.doc", "G:\\LYX\\f\\world2pdf.pdf"); }
执行结果: