zoukankan      html  css  js  c++  java
  • java使用jacob将office转pdf

    1.此处代码是把office文档转换成pdf的工具类,在BS架构的产品中,我们可以使用基于JS的pdf插件显示pdf文档,但是前提IE需要按照adobe的pdf软件,对于非IE不用安装。
    2.可以基于flexPager插件显示文档,但是也需要把office转换成swf的文件才可以,实现需要完成转换。
    =========================
    1
    import java.io.File; 2 3 import com.jacob.activeX.ActiveXComponent; 4 import com.jacob.com.Dispatch; 5 6 public class OfficeToPdfTools { 7 8 private static final int wdFormatPDF = 17; 9 private static final int xlsFormatPDF = 0; 10 private static final int pptFormatPDF = 32; 11 private static final int msoTrue = -1; 12 private static final int msofalse = 0; 13 14 15 public static boolean convert2PDF(String inputFile, String pdfFile) { 16 String suffix = getFileSufix(inputFile); 17 File file = new File(inputFile); 18 if (!file.exists()) { 19 System.out.println("文件不存在!"); 20 return false; 21 } 22 if (suffix.equals("pdf")) { 23 System.out.println("PDF not need to convert!"); 24 return false; 25 } 26 if (suffix.equals("doc") || suffix.equals("docx")) { 27 return word2PDF(inputFile, pdfFile); 28 } else if (suffix.equals("ppt") || suffix.equals("pptx")) { 29 return ppt2PDF(inputFile, pdfFile); 30 } else if (suffix.equals("xls") || suffix.equals("xlsx")) { 31 return excel2PDF(inputFile, pdfFile); 32 } else { 33 System.out.println("文件格式不支持转换!"); 34 return false; 35 } 36 } 37 38 private static String getFileSufix(String fileName) { 39 int splitIndex = fileName.lastIndexOf("."); 40 return fileName.substring(splitIndex + 1); 41 } 42 43 private static boolean word2PDF(String inputFile, String pdfFile) { 44 try { 45 ActiveXComponent app = new ActiveXComponent("Word.Application"); 46 app.setProperty("Visible", false); 47 Dispatch docs = app.getProperty("Documents").toDispatch(); 48 Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true) 49 .toDispatch(); 50 File tofile = new File(pdfFile); 51 if (tofile.exists()) { 52 tofile.delete(); 53 } 54 Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF 55 ); 56 Dispatch.call(doc, "Close", false); 57 app.invoke("Quit", 0); 58 return true; 59 } catch (Exception e) { 60 return false; 61 } 62 } 63 64 private static boolean excel2PDF(String inputFile, String pdfFile) { 65 try { 66 ActiveXComponent app = new ActiveXComponent("Excel.Application"); 67 app.setProperty("Visible", false); 68 Dispatch excels = app.getProperty("Workbooks").toDispatch(); 69 Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, 70 true).toDispatch(); 71 File tofile = new File(pdfFile); 72 if (tofile.exists()) { 73 tofile.delete(); 74 } 75 Dispatch.call(excel, "ExportAsFixedFormat", xlsFormatPDF, pdfFile); 76 Dispatch.call(excel, "Close", false); 77 app.invoke("Quit"); 78 return true; 79 } catch (Exception e) { 80 return false; 81 } 82 83 } 84 85 private static boolean ppt2PDF(String inputFile, String pdfFile) { 86 try { 87 ActiveXComponent app = new ActiveXComponent( 88 "PowerPoint.Application"); 89 Dispatch ppts = app.getProperty("Presentations").toDispatch(); 90 Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly 91 true,// Untitled指定文件是否有标题 92 false// WithWindow指定文件是否可见 93 ).toDispatch(); 94 File tofile = new File(pdfFile); 95 if (tofile.exists()) { 96 tofile.delete(); 97 } 98 Dispatch.call(ppt, "SaveAs", pdfFile, pptFormatPDF); 99 Dispatch.call(ppt, "Close"); 100 app.invoke("Quit"); 101 return true; 102 } catch (Exception e) { 103 return false; 104 } 105 } 106 107 108 public static void main(String[] args) { 109 //OfficeToPdfTools.convert2PDF("c:\ppt.pptx", "c:\ppt.pdf"); 110 OfficeToPdfTools.convert2PDF("c:\excel.xls", "c:\excel.pdf"); 111 //OfficeToPdfTools.convert2PDF("c:\word.docx", "c:\word.pdf"); 112 113 } 114

     说明:下载http://www.oschina.net/p/jacob/ 下载jacob包

  • 相关阅读:
    深入浅出进程与线程的基本概念
    python中with的用法
    浮点型数据在内存中存储的表示
    自问问题列表以及网络答案整理
    看java源代码
    【设计模式】工厂方法
    SQL实现递归及存储过程中 In() 参数传递解决方案
    app与server联系
    添加service到SystemService硬件服务
    noproguard.classes-with-local.dex
  • 原文地址:https://www.cnblogs.com/gstsyyb/p/4952190.html
Copyright © 2011-2022 走看看