JAVA jacob office转换pdf代码

秒速五厘米 2022-08-21 04:26 291阅读 0赞

1.先下载office2007 能另存为pdf的插件 SaveAsPDFandXPS.exe 安装

2.附件提供了jacob1.9的下载

3.这点很重要也是耗时我最久的就是jacob.dll与jacob.jar的存放位置;位置不对则会出现app.getProperty(“Documents”).toDispatch()这句话无法正常执行;

位置如下文件存放在jdk的 jre 下 例如我这边是
jacob.jar 放在 E:\jdk1.5.0_14\jre\lib\ext
jacob.dll 放在 E:\jdk1.5.0_14\jre\bin
下能正常执行;

按网上说的 放在windows\system32下则会出现错误

  1. static final int wdFormatPDF = 17;// PDF 格式
  2. public void wordToPDF(String sfileName,String toFileName){
  3. System.out.println("启动Word...");
  4. long start = System.currentTimeMillis();
  5. ActiveXComponent app = null;
  6. Dispatch doc = null;
  7. try {
  8. app = new ActiveXComponent("Word.Application");
  9. app.setProperty("Visible", new Variant(false));
  10. Dispatch docs = app.getProperty("Documents").toDispatch();
  11. doc = Dispatch.call(docs, "Open" , sfileName).toDispatch();
  12. System.out.println("打开文档..." + sfileName);
  13. System.out.println("转换文档到PDF..." + toFileName);
  14. File tofile = new File(toFileName);
  15. if (tofile.exists()) {
  16. tofile.delete();
  17. }
  18. Dispatch.call(doc,
  19. "SaveAs",
  20. toFileName, // FileName
  21. wdFormatPDF);
  22. long end = System.currentTimeMillis();
  23. System.out.println("转换完成..用时:" + (end - start) + "ms.");
  24. } catch (Exception e) {
  25. System.out.println("========Error:文档转换失败:" + e.getMessage());
  26. } finally {
  27. Dispatch.call(doc,"Close",false);
  28. System.out.println("关闭文档");
  29. if (app != null)
  30. app.invoke("Quit", new Variant[] {});
  31. }
  32. //如果没有这句话,winword.exe进程将不会关闭
  33. ComThread.Release();
  34. }
  35. public static void main(String[] args) {
  36. Test d = new Test();
  37. d.wordToPDF("E:\\321.doc", "E:\\test_321test.pdf");
  38. }

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读

    相关 OfficePDF

    采用 jacob 插件,调用 WPS 功能实现,需要 Windows 操作系统。这种方式转成 PDF 不会失真,针对 excel 设置 PDF 横向布局。   im