失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Java 使用wps将word文件转换pdf文件

Java 使用wps将word文件转换pdf文件

时间:2019-08-12 03:20:14

相关推荐

Java 使用wps将word文件转换pdf文件

public class WordFileToPdf {private static final int wdFormatPDF = 17;private static final int xlTypePDF = 0;private static final int ppSaveAsPDF = 32;private static final String PATH = "D:";/**** 判断需要转化文件的类型(Excel、Word、ppt)** @param inputFile*/public String convertToPDF(String inputFile) {//判断原文件是否存在File file = new File(PATH + inputFile);if (file.exists()) {String kind = getFileSufix(inputFile);if (kind.equals("pdf")) {return inputFile;//原文件就是PDF文件}String pdfFile = inputFile.substring(0, inputFile.lastIndexOf(".")) + ".pdf";if (kind.equals("doc")||kind.equals("docx")||kind.equals("txt")) {wordToPDF(PATH+inputFile, PATH+pdfFile);}else if (kind.equals("ppt")||kind.equals("pptx")||kind.equals("pptm")||kind.equals("ppsx")) {pptToPDF(PATH+inputFile, PATH+pdfFile);}else if(kind.equals("xls")||kind.equals("xlsx")){ExToPDF(PATH+inputFile, PATH+pdfFile);}else{return inputFile;//原文件是其它格式文件}//返回创建的pdfreturn pdfFile;} else {System.out.println("原文件不存在!");return inputFile;}}/**** 判断文件类型** @param fileName* @return*/public static String getFileSufix(String fileName) {int splitIndex = fileName.lastIndexOf(".");return fileName.substring(splitIndex + 1);}/***** Word转PDF** @param inputFile* @param pdfFile* @return*/private void wordToPDF(String inputFile, String pdfFile) {try {ComThread.InitSTA(true);// 打开Word应用程序ActiveXComponent app = new ActiveXComponent("KWPS.Application");// 设置Word不可见app.setProperty("Visible", new Variant(false));// 禁用宏app.setProperty("AutomationSecurity", new Variant(3));// 获得Word中所有打开的文档,返回documents对象Dispatch docs = app.getProperty("Documents").toDispatch();// 调用Documents对象中Open方法打开文档,并返回打开的文档对象DocumentDispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();// word保存为pdf格式宏,值为17Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// 关闭文档Dispatch.call(doc, "Close", false);// 关闭Word应用程序app.invoke("Quit", 0);ComThread.Release();} catch (Exception e) {e.printStackTrace();}}/***** Excel转化成PDF** @param inputFile* @param pdfFile* @return*/private void ExToPDF(String inputFile, String pdfFile) {try {ComThread.InitSTA(true);ActiveXComponent ax = new ActiveXComponent("KET.Application");ax.setProperty("Visible", false);ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏Dispatch excels = ax.getProperty("Workbooks").toDispatch();Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method,new Object[] { inputFile, new Variant(false), new Variant(false) }, new int[9]).toDispatch();// 转换格式Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] { new Variant(0),pdfFile, new Variant(xlTypePDF)}, new int[1]);Dispatch.call(excel, "Close", new Variant(false));if (ax != null) {ax.invoke("Quit", new Variant[] {});}ComThread.Release();} catch (Exception e) {e.printStackTrace();}}/**** ppt转化成PDF** @param inputFile* @param pdfFile* @return*/private void pptToPDF(String inputFile, String pdfFile) {try {ComThread.InitSTA(true);ActiveXComponent app = new ActiveXComponent("KWPP.Application");Dispatch ppts = app.getProperty("Presentations").toDispatch();Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,false).toDispatch();Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[]{pdfFile,new Variant(ppSaveAsPDF)},new int[1]);Dispatch.call(ppt, "Close");app.invoke("Quit");} catch (Exception e) {e.printStackTrace();}}}

下载jacob,将其中的jacob.jar添加到项目的lib中并构建。

将jacob-1.18-x64.dll添加到C:\Program Files\Java\jdk1.8.0_72\bin、C:\Program Files\Java\jdk1.8.0_72\jre\bin、C:\WINDOWS\system32 中(我只在jdk中添加的)。

如果觉得《Java 使用wps将word文件转换pdf文件》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。