java使用jacob将office转pdf
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java使用jacob将office转pdf,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3478字,纯文字阅读大概需要5分钟。
内容图文
![java使用jacob将office转pdf](/upload/InfoBanner/zyjiaocheng/1331/d86ed131b31b4f0886d8df527516facc.jpg)
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; 9privatestaticfinalint xlsFormatPDF = 0; 10privatestaticfinalint pptFormatPDF = 32; 11privatestaticfinalint msoTrue = -1; 12privatestaticfinalint msofalse = 0; 13 14 15publicstaticboolean convert2PDF(String inputFile, String pdfFile) { 16 String suffix = getFileSufix(inputFile); 17 File file = new File(inputFile); 18if (!file.exists()) { 19 System.out.println("文件不存在!"); 20returnfalse; 21 } 22if (suffix.equals("pdf")) { 23 System.out.println("PDF not need to convert!"); 24returnfalse; 25 } 26if (suffix.equals("doc") || suffix.equals("docx")) { 27return word2PDF(inputFile, pdfFile); 28 } elseif (suffix.equals("ppt") || suffix.equals("pptx")) { 29return ppt2PDF(inputFile, pdfFile); 30 } elseif (suffix.equals("xls") || suffix.equals("xlsx")) { 31return excel2PDF(inputFile, pdfFile); 32 } else { 33 System.out.println("文件格式不支持转换!"); 34returnfalse; 35 } 36 } 37 38privatestatic String getFileSufix(String fileName) { 39int splitIndex = fileName.lastIndexOf("."); 40return fileName.substring(splitIndex + 1); 41 } 42 43privatestaticboolean word2PDF(String inputFile, String pdfFile) { 44try { 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); 51if (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); 58returntrue; 59 } catch (Exception e) { 60returnfalse; 61 } 62 } 63 64privatestaticboolean excel2PDF(String inputFile, String pdfFile) { 65try { 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, 70true).toDispatch(); 71 File tofile = new File(pdfFile); 72if (tofile.exists()) { 73 tofile.delete(); 74 } 75 Dispatch.call(excel, "ExportAsFixedFormat", xlsFormatPDF, pdfFile); 76 Dispatch.call(excel, "Close", false); 77 app.invoke("Quit"); 78returntrue; 79 } catch (Exception e) { 80returnfalse; 81 } 82 83 } 84 85privatestaticboolean ppt2PDF(String inputFile, String pdfFile) { 86try { 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 91true,// Untitled指定文件是否有标题 92false// WithWindow指定文件是否可见 93 ).toDispatch(); 94 File tofile = new File(pdfFile); 95if (tofile.exists()) { 96 tofile.delete(); 97 } 98 Dispatch.call(ppt, "SaveAs", pdfFile, pptFormatPDF); 99 Dispatch.call(ppt, "Close"); 100 app.invoke("Quit"); 101returntrue; 102 } catch (Exception e) { 103returnfalse; 104 } 105 } 106107108publicstaticvoid 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");112113 } 114
说明:下载http://www.oschina.net/p/jacob/ 下载jacob包
原文:http://www.cnblogs.com/gstsyyb/p/4952190.html
内容总结
以上是互联网集市为您收集整理的java使用jacob将office转pdf全部内容,希望文章能够帮你解决java使用jacob将office转pdf所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。