最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用jacob將微軟office中word、excel、ppt轉(zhuǎn)成pdf

 更新時(shí)間:2018年12月19日 16:29:46   作者:jesionsly  
這篇文章主要為大家詳細(xì)介紹了Java使用jacob將微軟office中word、excel、ppt轉(zhuǎn)成pdf,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java使用jacob將微軟office文檔轉(zhuǎn)成pdf的具體代碼,供大家參考,具體內(nèi)容如下

在使用jacb前,我們需要去下載 jacob.jar 和 jacob-1.18-x64.dll

其次,我們需要將jacob-1.18-x64.dll放入到j(luò)dk的bin目錄下才可以使用

第三,使用jacb之前,我們需要確保office能正常使用

如果你現(xiàn)在使用的是maven工程,那么不好意思,現(xiàn)在還沒有發(fā)布正式的jacb資源文件,我們需要自定的maven依賴,如下:

<dependency>
 <groupId>com.jacob</groupId>
 <artifactId>jacob</artifactId>
 <version>1.7</version>
 <scope>system</scope>
 <systemPath>${basedir}/../fileConvertApp/src/main/webapp/WEB-INF/lib/jacob.jar</systemPath>
</dependency>

然后需要注意的是jar的地址,需要根據(jù)自己的情況修改

接下來我們貼一下具體使用的代碼片段

import java.io.File;
 
import org.apache.log4j.Logger;
 
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
 
/**
 * Converter Util
 * 
 * @author Jason
 *
 */
public class OfficeConverterUtil {
 
 /**
 * log
 */
 private static Logger logger = Logger.getLogger(OfficeConverterUtil.class);
 private static final int WDFO_RMATPDF = 17;
 private static final int XLTYPE_PDF = 0;
 private static final int PPT_SAVEAS_PDF = 32;
 public static final int WORD_HTML = 8;
 public static final int WORD_TXT = 7;
 public static final int EXCEL_HTML = 44;
 public static final int PPT_SAVEAS_JPG = 17;
 // private static final int msoTrue = -1;
 // private static final int msofalse = 0;
 
 /**
 * @param argInputFilePath
 * @param argPdfPath
 * @return
 */
 public static boolean officeFileConverterToPdf(String argInputFilePath, String argPdfPath) {
 if (argInputFilePath.isEmpty() || argPdfPath.isEmpty() || getFileSufix(argInputFilePath).isEmpty()) {
 logger.debug("輸入或輸出文件路徑有誤!");
 return false;
 }
 
 String suffix = getFileSufix(argInputFilePath);
 
 File file = new File(argInputFilePath);
 if (!file.exists()) {
 logger.debug("文件不存在!");
 return false;
 }
 
 // PDF如果不存在則創(chuàng)建文件夾
 file = new File(getFilePath(argPdfPath));
 if (!file.exists()) {
 file.mkdir();
 }
 
 // 如果輸入的路徑為PDF 則生成失敗
 if (suffix.equals("pdf")) {
 System.out.println("PDF not need to convert!");
 return false;
 }
 
 if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {
 return wordToPDF(argInputFilePath, argPdfPath);
 } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
 return excelToPdf(argInputFilePath, argPdfPath);
 } else if (suffix.equals("ppt") || suffix.equals("pptx")) {
 return pptToPdf(argInputFilePath, argPdfPath);
 // return ppt2PDF(argInputFilePath, argPdfPath);
 }
 
 return false;
 }
 
 /**
 * converter word to pdf
 * 
 * @param wordPath
 * @param pdfPath
 * @return
 */
 public static boolean wordToPDF(String wordPath, String pdfPath) {
 ActiveXComponent msWordApp = new ActiveXComponent("Word.Application");
 msWordApp.setProperty("Visible", new Variant(false));
 
 Dispatch docs = Dispatch.get(msWordApp, "Documents").toDispatch();
 // long pdfStart = System.currentTimeMillis();
 Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { wordPath, new Variant(false), new Variant(true) }, new int[1]).toDispatch();
 
 deletePdf(pdfPath);
 
 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { pdfPath, new Variant(WDFO_RMATPDF) }, new int[1]);
 // long pdfEnd = System.currentTimeMillis();
 logger.debug(wordPath + ",pdf轉(zhuǎn)換完成..");
 if (null != doc) {
 Dispatch.call(doc, "Close", false);
 }
 return true;
 }
 
 /**
 * excel to pdf
 * 
 * @param inputFile
 * @param pdfFile
 * @return
 */
 public static boolean excelToPdf(String inputFile, String pdfFile) {
 ActiveXComponent activeXComponent = new ActiveXComponent("Excel.Application");
 activeXComponent.setProperty("Visible", false);
 
 deletePdf(pdfFile);
 
 Dispatch excels = activeXComponent.getProperty("Workbooks").toDispatch();
 Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
 Dispatch.call(excel, "ExportAsFixedFormat", XLTYPE_PDF, pdfFile);
 Dispatch.call(excel, "Close", false);
 activeXComponent.invoke("Quit");
 return true;
 }
 
 /**
 * ppt to pdf
 * 
 * @param inputFile
 * @param pdfFile
 * @return
 */
 public static boolean pptToPdf(String inputFile, String pdfFile) {
// ComThread.InitSTA();
 ActiveXComponent activeXComponent = new ActiveXComponent("PowerPoint.Application");
// activeXComponent.setProperty("Visible", new Variant(false));
 Dispatch ppts = activeXComponent.getProperty("Presentations").toDispatch();
 
 deletePdf(pdfFile);
 
 Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, false, // ReadOnly
 true, // Untitled指定文件是否有標(biāo)題
 true// WithWindow指定文件是否可見
 ).toDispatch();
 
// Dispatch ppt = Dispatch.invoke(ppts, "Open", Dispatch.Method, new Object[] { inputFile, new Variant(false), new Variant(true) }, new int[1]).toDispatch();
 
// Dispatch.call(ppt, "SaveAs", pdfFile, PPT_SAVEAS_PDF);
// Dispatch.call(ppt, "SaveAs", pdfFile, new Variant(PPT_SAVEAS_PDF));
// Dispatch.call(ppt, "SaveAs", pdfFile, new Variant(PPT_SAVEAS_PDF));
// Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[] { pdfFile, PPT_SAVEAS_PDF }, new int[1]);
// Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[] { new Variant(PPT_SAVEAS_PDF) }, new int[1]);
 Dispatch.callN(ppt, "SaveAs", new Variant(pdfFile));
 
 Dispatch.call(ppt, "Close");
 
 activeXComponent.invoke("Quit");
// ComThread.Release();
 return true;
 }
 
 /**
 * ppt to img
 * 
 * @param inputFile
 * @param imgFile
 * @return
 */
 public static boolean pptToImg(String inputFile, String imgFile) {
 // 打開word應(yīng)用程序
 ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
 // 設(shè)置word不可見,office可能有限制
 // app.setProperty("Visible", false);
 // 獲取word中國(guó)所打開的文檔,返回Documents對(duì)象
 Dispatch files = app.getProperty("Presentations").toDispatch();
 // 調(diào)用Documents對(duì)象中Open方法打開文檔,并返回打開的文檔對(duì)象Document
 Dispatch file = Dispatch.call(files, "open", inputFile, true, true, false).toDispatch();
 // 調(diào)用Document對(duì)象的SaveAs方法,將文檔保存為pdf格式
 // Dispatch.call(doc, "ExportAsFixedFormat", outputFile,
 // PPT_TO_PDF);
 Dispatch.call(file, "SaveAs", imgFile, PPT_SAVEAS_JPG);
 // 關(guān)閉文檔
 // Dispatch.call(file, "Close", false);
 Dispatch.call(file, "Close");
 // 關(guān)閉word應(yīng)用程序
 // app.invoke("Quit", 0);
 app.invoke("Quit");
 return true;
 }
 
 /**
 * get file extension
 * 
 * @param argFilePath
 * @return
 */
 public static String getFileSufix(String argFilePath) {
 int splitIndex = argFilePath.lastIndexOf(".");
 return argFilePath.substring(splitIndex + 1);
 }
 
 /**
 * subString file path
 * 
 * @param argFilePath
 *   file path
 * @return filePaths
 */
 public static String getFilePath(String argFilePath) {
 int pathIndex = argFilePath.lastIndexOf("/");
 return argFilePath.substring(0, pathIndex);
 }
 
 /**
 * 如果PDF存在則刪除PDF
 * 
 * @param pdfPath
 */
 private static void deletePdf(String pdfPath) {
 File pdfFile = new File(pdfPath);
 if (pdfFile.exists()) {
 pdfFile.delete();
 }
 }
 
}

根據(jù)自己的調(diào)試,試驗(yàn)一下吧。

另外還有一段WPS轉(zhuǎn)PDF,也貼一下,供大家參考一下

public void wps2PDF(String inputFile,String pdfFile) { 
  File sFile = new File(inputFile);
  File tFile = new File(pdfFile);
  ActiveXComponent wps = null;
  try {
   ComThread.InitSTA();
   wps = new ActiveXComponent("wps.application");
   ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", new Variant(sFile.getAbsolutePath())); 
   doc.invoke("ExportPdf", new Variant(tFile.getAbsolutePath())); 
   doc.invoke("Close"); 
   doc.safeRelease(); 
  } catch (Exception e) { 
   System.out.println(e.getMessage()); 
  } finally { 
   if (wps != null) { 
    wps.invoke("Terminate"); 
    wps.safeRelease(); 
   }
   ComThread.Release();
  } 
 } 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼

    Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼

    這篇文章主要介紹了Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Java程序執(zhí)行時(shí)間的2種簡(jiǎn)單方法

    Java程序執(zhí)行時(shí)間的2種簡(jiǎn)單方法

    這篇文章介紹了Java程序執(zhí)行時(shí)間的2種簡(jiǎn)單方法,有需要的朋友可以參考一下
    2013-09-09
  • java 如何實(shí)現(xiàn)日志追蹤MDC

    java 如何實(shí)現(xiàn)日志追蹤MDC

    這篇文章主要介紹了java 實(shí)現(xiàn)日志追蹤MDC方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java實(shí)現(xiàn)俄羅斯方塊小游戲源碼

    Java實(shí)現(xiàn)俄羅斯方塊小游戲源碼

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)俄羅斯方塊小游戲源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 關(guān)于@Component注解的含義說明

    關(guān)于@Component注解的含義說明

    這篇文章主要介紹了關(guān)于@Component注解的含義說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java開發(fā)崗位面試被問到泛型怎么辦

    Java開發(fā)崗位面試被問到泛型怎么辦

    泛型在java中有很重要的地位,在面向?qū)ο缶幊碳案鞣N設(shè)計(jì)模式中有非常廣泛的應(yīng)用。java泛型知識(shí)點(diǎn)也是Java開發(fā)崗位必問的一個(gè)話題,今天小編就給大家普及下Java泛型常見面試題,感興趣的朋友一起看看吧
    2021-07-07
  • Spring Cloud如何切換Ribbon負(fù)載均衡模式

    Spring Cloud如何切換Ribbon負(fù)載均衡模式

    這篇文章主要介紹了Spring Cloud如何切換Ribbon負(fù)載均衡模式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java進(jìn)程間通信之消息隊(duì)列

    Java進(jìn)程間通信之消息隊(duì)列

    這篇文章主要為大家詳細(xì)介紹了Java進(jìn)程間通信之消息隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 查看import的類是出自哪個(gè)jar包的方法

    查看import的類是出自哪個(gè)jar包的方法

    下面小編就為大家?guī)硪黄榭磇mport的類是出自哪個(gè)jar包的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • C#使用MySQLConnectorNet和MySQLDriverCS操作MySQL的方法

    C#使用MySQLConnectorNet和MySQLDriverCS操作MySQL的方法

    這篇文章主要介紹了C#使用MySQLConnectorNet和MySQLDriverCS操作MySQL的方法,相比普通方法能夠在Windows下簡(jiǎn)化很多操作步驟,需要的朋友可以參考下
    2016-04-04

最新評(píng)論

林州市| 车致| 龙陵县| 年辖:市辖区| 凉山| 抚宁县| 昂仁县| 仙桃市| 驻马店市| 浦城县| 隆尧县| 南华县| 新邵县| 浦城县| 南雄市| 贵州省| 蓬溪县| 界首市| 清镇市| 康马县| 平乡县| 北安市| 普陀区| 集贤县| 白朗县| 井研县| 金川县| 西平县| 信丰县| 闻喜县| 太保市| 阿拉善盟| 修武县| 建始县| 翁牛特旗| 泾源县| 海安县| 卢龙县| 敦化市| 新和县| 乐平市|