jacob—java开发world、excel、ppt转pdf
2021/9/14 17:07:44
本文主要是介绍jacob—java开发world、excel、ppt转pdf,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
项目中之前用的office转pdf的插件是aspose
因为aspose无需world。所有转换操作都是在java虚拟机里边进行的。所以如果有图片特别多的文档转换的时候就会遇到oom。
而jacob是通过本地安装的office插件将文档再本地完成转换的。就不会出现oom的情况。而弊端就是项目就只能部署在windows server服务器上。
1、先安装SaveAsPDFandXPS
2、下载 jacob 解压后存放路径:
(上传的资源还在审核,等审核通过再补上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”
网上这个报错信息的解决方法是:
这个方法对我没用,不过也放在这里供大家参考。
3、 world转pdf
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(); }
4、excel转pdf
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(); } }
5、ppt转pdf
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"); }
执行结果:
这篇关于jacob—java开发world、excel、ppt转pdf的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南