java进行文档类型转换PDF
生活随笔
收集整理的這篇文章主要介紹了
java进行文档类型转换PDF
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用jacob進行文檔類型轉(zhuǎn)換支持PPT、Excel、Word轉(zhuǎn)為PDF模式
本方法對Windows部署的項目友好,最后需要在jdk/bin目錄下導(dǎo)入與jar包版本一致的.dll文件
文件地址
鏈接:https://pan.baidu.com/s/1rSCC8RgwL3DN0Fz0pkCARQ
提取碼:a065
jar包
<!-- 文檔轉(zhuǎn)換 --><dependency><groupId>net.sf.jacob-project</groupId><artifactId>jacob</artifactId><version>1.14.3</version></dependency>文檔轉(zhuǎn)換工具類
package com.shouzhi.service.common;import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch;import java.io.File;/*** 文檔類型轉(zhuǎn)換為PDF預(yù)覽* @author Mr* @date 2021/4/2 14:02*/ public class PDFUtil {private static final int wdFormatPDF = 17;private static final int xlTypePDF = 0;private static final int ppSaveAsPDF = 32;//判斷文檔類型public static boolean change2PDF(String inputFile, String pdfFile) {String suffix = getFileSufix(inputFile);File file = new File(inputFile);if (!file.exists()) {return false;}if (suffix.equals("pdf")) {return false;}if (suffix.equals("doc") || suffix.equals("docx")) {return wordToPDF(inputFile, pdfFile);} else if (suffix.equals("ppt") || suffix.equals("pptx")) {return pptToPDF(inputFile, pdfFile);} else if (suffix.equals("xls") || suffix.equals("xlsx")) {return excelToPDF(inputFile, pdfFile);} else {return false;}}public static String getFileSufix(String fileName) {int splitIndex = fileName.lastIndexOf(".");return fileName.substring(splitIndex + 1);}// word轉(zhuǎn)換為pdfpublic static boolean wordToPDF(String inputFile, String pdfFile) {try {// 打開word應(yīng)用程序ActiveXComponent app = new ActiveXComponent("Word.Application");// 設(shè)置word不可見app.setProperty("Visible", false);// 獲得word中所有打開的文檔,返回Documents對象Dispatch docs = app.getProperty("Documents").toDispatch();// 調(diào)用Documents對象中Open方法打開文檔,并返回打開的文檔對象DocumentDispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word保存為pdf格式宏,值為17// 關(guān)閉文檔Dispatch.call(doc, "Close", false);// 關(guān)閉word應(yīng)用程序app.invoke("Quit", 0);return true;} catch (Exception e) {return false;}}// excel轉(zhuǎn)換為pdfpublic static boolean excelToPDF(String inputFile, String pdfFile) {try {ActiveXComponent app = new ActiveXComponent("Excel.Application");app.setProperty("Visible", false);Dispatch excels = app.getProperty("Workbooks").toDispatch();Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,true).toDispatch();Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);Dispatch.call(excel, "Close", false);app.invoke("Quit");return true;} catch (Exception e) {return false;}}// ppt轉(zhuǎn)換為pdfpublic static boolean pptToPDF(String inputFile, String pdfFile) {try {ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");// app.setProperty("Visible", msofalse);Dispatch ppts = app.getProperty("Presentations").toDispatch();Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnlytrue,// Untitled指定文件是否有標(biāo)題false// WithWindow指定文件是否可見).toDispatch();Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);Dispatch.call(ppt, "Close");app.invoke("Quit");return true;} catch (Exception e) {return false;}} }總結(jié)
以上是生活随笔為你收集整理的java进行文档类型转换PDF的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现全国组织机构代码校验
- 下一篇: 可视化财务报表不会做?那是你没用对报表工