Java利用jacob实现wps转换pdf

  实现文档格式之间的转换,我使用的是jacob-1.7版本,需要jacob.jar来调用activex控件,本机需安装WPS/office,还需要jacob.jar以及jacob.dll 

其中:
    jacob.dll 需要放置在系统system32下,如果系统是c盘:C://windows/system32/下面 

    jacob.dll放在类似这样的目录下,D:\Java\jre1.8.0_31\bin

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class PDFUtil {  
  2.     private static final int wdFormatPDF = 17;  
  3.     private static final int xlTypePDF = 0;  
  4.     private static final int ppSaveAsPDF = 32;  
  5.   
  6.     public static void main(String[] args) {  
  7.         PDFUtil.word2PDF("D:/20161202184309.doc""D:/20161202184309.pdf");  
  8.         System.out.println("转换完成!");  
  9.     }  
  10.     public boolean convert2PDF(String inputFile, String pdfFile) {  
  11.         String suffix = getFileSufix(inputFile);  
  12.         File file = new File(inputFile);  
  13.         if (!file.exists()) {  
  14.             return false;  
  15.         }  
  16.         if (suffix.equals("pdf")) {  
  17.             return false;  
  18.         }  
  19.         if (suffix.equals("doc") || suffix.equals("docx")  
  20.                 || suffix.equals("txt")) {  
  21.             return word2PDF(inputFile, pdfFile);  
  22.         } else if (suffix.equals("ppt") || suffix.equals("pptx")) {  
  23.             return ppt2PDF(inputFile, pdfFile);  
  24.         } else if (suffix.equals("xls") || suffix.equals("xlsx")) {  
  25.             return excel2PDF(inputFile, pdfFile);  
  26.         } else {  
  27.             return false;  
  28.         }  
  29.     }  
  30.     public static String getFileSufix(String fileName) {  
  31.         int splitIndex = fileName.lastIndexOf(".");  
  32.         return fileName.substring(splitIndex + 1);  
  33.     }  
  34.     // word转换为pdf  
  35.     public static boolean word2PDF(String inputFile, String pdfFile) {  
  36.         try {  
  37.             // 打开word应用程序  
  38.             ActiveXComponent app = new ActiveXComponent("Word.Application");  
  39.             // 设置word不可见  
  40.             app.setProperty("Visible"false);  
  41.             // 获得word中所有打开的文档,返回Documents对象  
  42.             Dispatch docs = app.getProperty("Documents").toDispatch();  
  43.             // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document  
  44.             Dispatch doc = Dispatch.call(docs, "Open", inputFile, falsetrue)  
  45.                     .toDispatch();  
  46.             // 调用Document对象的SaveAs方法,将文档保存为pdf格式  
  47.             /* 
  48.              * Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF 
  49.              * //word保存为pdf格式宏,值为17 ); 
  50.              */  
  51.             Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17  
  52.             // 关闭文档  
  53.             Dispatch.call(doc, "Close"false);  
  54.             // 关闭word应用程序  
  55.             app.invoke("Quit"0);  
  56.             return true;  
  57.         } catch (Exception e) {  
  58.             return false;  
  59.         }  
  60.     }  
  61.     // excel转换为pdf  
  62.     public static boolean excel2PDF(String inputFile, String pdfFile) {  
  63.         try {  
  64.             ActiveXComponent app = new ActiveXComponent("Excel.Application");  
  65.             app.setProperty("Visible"false);  
  66.             Dispatch excels = app.getProperty("Workbooks").toDispatch();  
  67.             Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,  
  68.                     true).toDispatch();  
  69.             Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);  
  70.             Dispatch.call(excel, "Close"false);  
  71.             app.invoke("Quit");  
  72.             return true;  
  73.         } catch (Exception e) {  
  74.             return false;  
  75.         }  
  76.     }  
  77.     // ppt转换为pdf  
  78.     public static boolean ppt2PDF(String inputFile, String pdfFile) {  
  79.         try {  
  80.             ActiveXComponent app = new ActiveXComponent(  
  81.                     "PowerPoint.Application");  
  82.             // app.setProperty("Visible", msofalse);  
  83.             Dispatch ppts = app.getProperty("Presentations").toDispatch();  
  84.   
  85.             Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly  
  86.                     true,// Untitled指定文件是否有标题  
  87.                     false// WithWindow指定文件是否可见  
  88.                     ).toDispatch();  
  89.   
  90.             Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);  
  91.   
  92.             Dispatch.call(ppt, "Close");  
  93.   
  94.             app.invoke("Quit");  
  95.             return true;  
  96.         } catch (Exception e) {  
  97.             return false;  
  98.         }  
  99.     }  
  100. }  

按照上述操作即可完成文档之间格式的转换,但在我实际操作的时候真是各种bug各种奇葩啊!其中有一件事着实让人无语,在进行word转换为pdf的时候,提示转换失败,当时我一直认为是自己程序的问题,但是在认真地检查之后并没有发现有任何错误,最后还是在大神的提点下才恍然大悟,原来自己在发布到服务器的时候,由于服务器并没有安装office,所以才一直提示转化失败!哎~万万没想到竟然是这个问题......
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值