java jacob pdf_java使用jacob将office转pdf

这段代码展示了如何使用Jacob库在Java中将Word、Excel和PowerPoint文档转换为PDF格式。首先检查输入文件是否存在及格式是否支持转换,然后分别调用word2PDF、excel2PDF和ppt2PDF方法进行转换。转换过程中,利用ActiveXComponent与Office应用程序交互,实现文件的读取、转换和保存。
摘要由CSDN通过智能技术生成

1 importjava.io.File;2

3 importcom.jacob.activeX.ActiveXComponent;4 importcom.jacob.com.Dispatch;5

6 public classOfficeToPdfTools {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 booleanconvert2PDF(String inputFile, String pdfFile) {16 String suffix =getFileSufix(inputFile);17 File file = newFile(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 returnword2PDF(inputFile, pdfFile);28 } else if (suffix.equals("ppt") || suffix.equals("pptx")) {29 returnppt2PDF(inputFile, pdfFile);30 } else if (suffix.equals("xls") || suffix.equals("xlsx")) {31 returnexcel2PDF(inputFile, pdfFile);32 } else{33 System.out.println("文件格式不支持转换!");34 return false;35 }36 }37

38 private staticString getFileSufix(String fileName) {39 int splitIndex = fileName.lastIndexOf(".");40 return fileName.substring(splitIndex + 1);41 }42

43 private static booleanword2PDF(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 = newFile(pdfFile);51 if(tofile.exists()) {52 tofile.delete();53 }54 Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF55 );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 booleanexcel2PDF(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 = newFile(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 booleanppt2PDF(String inputFile, String pdfFile) {86 try{87 ActiveXComponent app = newActiveXComponent(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 = newFile(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 voidmain(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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值