欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何在Java中使用jacob將word轉(zhuǎn)換成pdf

本文章向大家介紹如何在Java中使用jacob將word轉(zhuǎn)換成pdf,主要包括如何在Java中使用jacob將word轉(zhuǎn)換成pdf的使用實例、應(yīng)用技巧、基本知識點總結(jié)和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。

為建湖等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及建湖網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、建湖網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

Java可以用來干什么

Java主要應(yīng)用于:1. web開發(fā);2. Android開發(fā);3. 客戶端開發(fā);4. 網(wǎng)頁開發(fā);5. 企業(yè)級應(yīng)用開發(fā);6. Java大數(shù)據(jù)開發(fā);7.游戲開發(fā)等。

需要自定的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指定文件是否有標題
 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中國所打開的文檔,返回Documents對象
 Dispatch files = app.getProperty("Presentations").toDispatch();
 // 調(diào)用Documents對象中Open方法打開文檔,并返回打開的文檔對象Document
 Dispatch file = Dispatch.call(files, "open", inputFile, true, true, false).toDispatch();
 // 調(diào)用Document對象的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)試,試驗一下吧。

另外還有一段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();
  } 
 }

到此這篇關(guān)于如何在Java中使用jacob將word轉(zhuǎn)換成pdf的文章就介紹到這了,更多相關(guān)的內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!

分享標題:如何在Java中使用jacob將word轉(zhuǎn)換成pdf
轉(zhuǎn)載來源:http://www.chinadenli.net/article2/ieoooc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計品牌網(wǎng)站制作關(guān)鍵詞優(yōu)化手機網(wǎng)站建設(shè)營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名