Java中幾種office文檔轉pdf的方式


最近公司要做office的文檔,搜集了幾種office文檔轉pdf的方式,簡單的做下總結

我主要嘗試了三種方式:openoffice,aspose,jacob

對他們進行了大文件,小文件,在linux,在windows,轉換txt,excel,word,ppt的測試。

一、aspose:這種方式在目前來看應該是最好的,無論是轉換的速度還是成功的概率,還支持的文件類型。

(1)使用:

這種方式使用很簡單,引入jar包就可以直接使用

代碼:

源碼,jar包在最后提供

package aspose;

import java.io.*;

import javax.servlet.http.HttpServletRequest;

import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.words.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class AsposeUtil {

    //校驗license
    private static boolean judgeLicense() {
        boolean result = false;
        try {
            InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    // 轉換
    public static void trans(String filePath, String pdfPath, String type) {
        if (!judgeLicense()) {
            System.out.println("license錯誤");
        }
        try {
            System.out.println("as開始:" + filePath);
            long old = System.currentTimeMillis();
            File file = new File(pdfPath);
            toPdf(file, filePath, type);
            long now = System.currentTimeMillis();
            System.out.println("完成:" + pdfPath);
            System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

    private static void toPdf(File file, String filePath, String type) {
        if ("word".equals(type) || "txt".equals(type)) {
            wordofpdf(file, filePath);
        } else if ("excel".equals(type)) {
            exceOfPdf(file, filePath);
        } else if ("ppt".equals(type)) {
            pptofpdf(file, filePath);
        }else{
            System.out.println("暫不支持該類型:"+type);
        }
    }

    private static void wordofpdf(File file, String filePath) {
        FileOutputStream os = null;
        Document doc;
        try {
            os = new FileOutputStream(file);
            doc = new Document(filePath);
            doc.save(os, com.aspose.words.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void exceOfPdf(File file, String filePath) {
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            Workbook wb = new Workbook(filePath);
            wb.save(os, com.aspose.cells.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void pptofpdf(File file, String filePath) {
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            Presentation pres = new Presentation(filePath);// 輸入pdf路徑
            pres.save(os, SaveFormat.Pdf);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

測試代碼:

 這里面每一種類型的文檔都至少取了三個,主要是為了測試他們對大文件,小文件的支持,從而能在不同的場景更好的選擇使用的方式,在本篇文章的最后會將測試的結果貼上

package openoffice;

import aspose.AsposeUtil;

/**
 * @author sonyan
 * @version 2019年9月25日 下午1:12:21
 * @desc
 */
public class Test {

    private static void testWord(String path_word, String pafpath) throws Exception {
        String word1 = path_word + "01正方數字.docx";
        String word2 = path_word + "02正方數字.docx";
        String word3 = path_word + "03正方數字.doc";
        String word4 = path_word + "04正方數字.doc";
        String word5 = path_word + "05正方數字.docx";
        String word6 = path_word + "06正方數字.doc";

        OpenOfficeUtils.toPdf(word1, pafpath + "Open-word-01測試.pdf");
        OpenOfficeUtils.toPdf(word2, pafpath + "Open-word-02測試.pdf");
        OpenOfficeUtils.toPdf(word3, pafpath + "Open-word-03測試.pdf");
        OpenOfficeUtils.toPdf(word4, pafpath + "Open-word-04測試.pdf");
        OpenOfficeUtils.toPdf(word5, pafpath + "Open-word-05測試.pdf");
        OpenOfficeUtils.toPdf(word6, pafpath + "Open-word-06測試.pdf");

    }

    private static void testWord2(String path_word, String pafpath) throws Exception {
        String word1 = path_word + "01.docx";
        String word2 = path_word + "02.docx";
        String word3 = path_word + "03.doc";
        String word4 = path_word + "04.doc";
        String word5 = path_word + "05.docx";
        String word6 = path_word + "06.doc";

        OpenOfficeUtils.toPdf(word1, pafpath + "Open-word-01.pdf");
        OpenOfficeUtils.toPdf(word2, pafpath + "Open-word-02.pdf");
        OpenOfficeUtils.toPdf(word3, pafpath + "Open-word-03.pdf");
        OpenOfficeUtils.toPdf(word4, pafpath + "Open-word-04.pdf");
        OpenOfficeUtils.toPdf(word5, pafpath + "Open-word-05.pdf");
        OpenOfficeUtils.toPdf(word6, pafpath + "Open-word-06.pdf");

    }

    private static void testTxt(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01jvm.txt";
        String txt2 = path_word + "02jvm.txt";
        String txt3 = path_word + "03jvm.txt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-txt-01測試.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-txt-02測試.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-txt-03測試.pdf");
    }

    private static void testTxt2(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01jvm.txt";
        String txt2 = path_word + "02jvm.txt";
        String txt3 = path_word + "03jvm.txt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-txt-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-txt-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-txt-03.pdf");
    }

    private static void testExcel(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01部門開發任務管理.xlsx";
        String txt2 = path_word + "02部門開發任務管理.xlsx";
        String txt3 = path_word + "03部門開發任務管理.xlsx";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-excel-01測試.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-excel-02測試.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-excel-03測試.pdf");
    }

    private static void testExcel2(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01.xlsx";
        String txt2 = path_word + "02.xlsx";
        String txt3 = path_word + "03.xlsx";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-excel-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-excel-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-excel-03.pdf");
    }

    private static void testPPt(String path_ppt, String pafpath) throws Exception {
        String txt1 = path_ppt + "01jquery培訓.pptx";
        String txt2 = path_ppt + "02jquery培訓.pptx";
        String txt3 = path_ppt + "03jquery培訓.ppt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-ppt-01測試.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-ppt-02測試.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-ppt-03測試.pdf");
    }

    private static void testPPt2(String path_ppt, String pafpath) throws Exception {
        String txt1 = path_ppt + "01jquery.pptx";
        String txt2 = path_ppt + "02jquery.pptx";
        String txt3 = path_ppt + "03jquery培訓.ppt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-ppt-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-ppt-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-ppt-03.pdf");
    }

    public static void LinuxTest() throws Exception {
        String path_word = "/software/songyan/hah/01word/";
        String path_txt = "/software/songyan/hah/02txt/";
        String path_excel = "/software/songyan/hah/03excel/";
        String path_ppt = "/software/songyan/hah/04ppt/";
        String pafpath = "/software/songyan/hah/pdf/";

        System.out.println("************************");
        testTxt(path_txt, pafpath);
        System.out.println("************************");
        testExcel(path_excel, pafpath);
        System.out.println("************************");
        testPPt(path_ppt, pafpath);
        System.out.println("************************");
        testWord(path_word, pafpath);
    }

    public static void LinuxTest2() throws Exception {
        String path_word = "/software/songyan/hah/01word/";
        String path_txt = "/software/songyan/hah/02txt/";
        String path_excel = "/software/songyan/hah/03excel/";
        String path_ppt = "/software/songyan/hah/04ppt/";
        String pafpath = "/software/songyan/hah/pdf/";

        System.out.println("************************");
        testTxt2(path_txt, pafpath);
        System.out.println("************************");
        testExcel2(path_excel, pafpath);
        System.out.println("************************");
        testPPt2(path_ppt, pafpath);
        System.out.println("************************");
        testWord2(path_word, pafpath);
    }

    public static void winTest() throws Exception {
        String path_word = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/01word/";
        String path_txt = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/02txt/";
        String path_excel = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/03excel/";
        String path_ppt = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/04ppt/";
        String pafpath = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/pdf/";

        System.out.println("************************");
        testWord(path_word, pafpath);
        System.out.println("************************");
        testTxt(path_txt, pafpath);
        System.out.println("************************");
        testExcel(path_excel, pafpath);
        System.out.println("************************");
        testPPt(path_ppt, pafpath);
    }
    
    public static void main(String[] args) throws Exception {
        winTest();
    }
}
View Code

 

 (2)初次使用遇到的問題:

  1)我在這種方式的初次嘗試中走的最大的一個坑就是:jar包的引入

  

   

  

上面這三個分別是轉換word/txt,excel,ppt的關鍵代碼,在網上找的很多代碼可能都是對一種方式的轉換,所以SaveFormat也就不會加上包名,所以我在開始就以為是同一個類,怎么調試都不對,最后也是不記得看了哪個博友的博客才注意到這,一定要加上包名,這三個不同包下的類來自三個不同的jar包

 

 2)還有一個很大的坑就是在linux系統上部署測試的時候報了下面的錯

 這個問題就查到了一個結果,官方給出的解釋是:

 

也就是缺少jar包,可是我檢查后發現jar包引入的也沒有錯誤,在第二天找同事幫忙的時候發現,同樣的war包使用他的工具可以正常執行轉換

很不可思議,我們是在同一台服務器上上傳的相同的war包,不一樣的就是使用的工具不一樣,我用的xshell,他用的sshClient。

我有又測試了一下,第一次訪問的時候都會有這個提示

 要求安裝xmanager,然后就試了試,安裝之后果然就可以了,原因現在也沒搞明白,,知道的留言吧(我們經理說可能是依賴xmanager里面的包,只是一種猜測)

3)還有個小問題就是:在linux上使用的時候需要安裝字體,不安裝會中文亂碼,這里不細說,百度很多,操作很簡單

4)在測試的過程中有個ppt文件轉換失敗,目前還沒有找到原因,轉換失敗的文件在最后也會提供在網盤中。。

5)這種方式不支持中文路徑,如果轉換的文件里面有中文,就會報文件找不到的錯誤

二,openoffice方式:這種方式在轉小文件的時候還是很好用的,在調研的過程中也發現不少公司在實際開發中就是使用的這種方式,我們公司在之前也是用的這種,但是在文件太大的時候轉換的時間就會特別的慢,幾十分鍾的那種。而且這種方式需要依賴openoffice

(1)代碼

注:這種方式需要安裝openoffice,使用前需要開啟openoffice,在轉換大文件的時候速度特別的慢。

使用步驟

(1)安裝openoffice

(2)開啟openoffice

(3)調用代碼

package openoffice; 

import java.io.File;
import java.net.ConnectException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;


public class OpenOfficeUtils {

    static final String host = "192.168.11.3";//openoffice服務地址
    static final int post = 8100;//openoffice端口號
    
    //計時
      public static void toPdf(String filePath, String pdfPath) {
          try {
              long old = System.currentTimeMillis();
              System.out.println("open-開始:" + filePath);
              File wordFile = new File(filePath);
            File PDFFile = new File(pdfPath);
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(host, post);
            try {
                connection.connect();
                DocumentConverter converter = getConverter(host, connection);
                converter.convert(wordFile, PDFFile);
                long now = System.currentTimeMillis();
                  System.out.println("open-完成:" + filePath);
                  System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒");
            } catch (ConnectException e) {
                System.out.println("獲取OpenOffice連接失敗...");
                e.printStackTrace();
            }
            finally{
                connection.disconnect();
            }
          } catch (Exception e) {
              System.out.println("轉換失敗:"+filePath);
              e.printStackTrace();
          }
      }
    private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
        DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
                || "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
                        : new StreamOpenOfficeDocumentConverter(connection);
        return converter;
    }

}

測試:

package openoffice;

import aspose.AsposeUtil;

/**
 * @author sonyan
 * @version 2019年9月25日 下午1:12:21
 * @desc
 */
public class Test {

    private static void testWord(String path_word, String pafpath) throws Exception {
        String word1 = path_word + "01正方數字.docx";
        String word2 = path_word + "02正方數字.docx";
        String word3 = path_word + "03正方數字.doc";
        String word4 = path_word + "04正方數字.doc";
        String word5 = path_word + "05正方數字.docx";
        String word6 = path_word + "06正方數字.doc";

        OpenOfficeUtils.toPdf(word1, pafpath + "Open-word-01測試.pdf");
        OpenOfficeUtils.toPdf(word2, pafpath + "Open-word-02測試.pdf");
        OpenOfficeUtils.toPdf(word3, pafpath + "Open-word-03測試.pdf");
        OpenOfficeUtils.toPdf(word4, pafpath + "Open-word-04測試.pdf");
        OpenOfficeUtils.toPdf(word5, pafpath + "Open-word-05測試.pdf");
        OpenOfficeUtils.toPdf(word6, pafpath + "Open-word-06測試.pdf");

    }

    private static void testWord2(String path_word, String pafpath) throws Exception {
        String word1 = path_word + "01.docx";
        String word2 = path_word + "02.docx";
        String word3 = path_word + "03.doc";
        String word4 = path_word + "04.doc";
        String word5 = path_word + "05.docx";
        String word6 = path_word + "06.doc";

        OpenOfficeUtils.toPdf(word1, pafpath + "Open-word-01.pdf");
        OpenOfficeUtils.toPdf(word2, pafpath + "Open-word-02.pdf");
        OpenOfficeUtils.toPdf(word3, pafpath + "Open-word-03.pdf");
        OpenOfficeUtils.toPdf(word4, pafpath + "Open-word-04.pdf");
        OpenOfficeUtils.toPdf(word5, pafpath + "Open-word-05.pdf");
        OpenOfficeUtils.toPdf(word6, pafpath + "Open-word-06.pdf");

    }

    private static void testTxt(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01jvm.txt";
        String txt2 = path_word + "02jvm.txt";
        String txt3 = path_word + "03jvm.txt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-txt-01測試.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-txt-02測試.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-txt-03測試.pdf");
    }

    private static void testTxt2(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01jvm.txt";
        String txt2 = path_word + "02jvm.txt";
        String txt3 = path_word + "03jvm.txt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-txt-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-txt-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-txt-03.pdf");
    }

    private static void testExcel(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01部門開發任務管理.xlsx";
        String txt2 = path_word + "02部門開發任務管理.xlsx";
        String txt3 = path_word + "03部門開發任務管理.xlsx";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-excel-01測試.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-excel-02測試.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-excel-03測試.pdf");
    }

    private static void testExcel2(String path_word, String pafpath) throws Exception {
        String txt1 = path_word + "01.xlsx";
        String txt2 = path_word + "02.xlsx";
        String txt3 = path_word + "03.xlsx";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-excel-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-excel-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-excel-03.pdf");
    }

    private static void testPPt(String path_ppt, String pafpath) throws Exception {
        String txt1 = path_ppt + "01jquery培訓.pptx";
        String txt2 = path_ppt + "02jquery培訓.pptx";
        String txt3 = path_ppt + "03jquery培訓.ppt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-ppt-01測試.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-ppt-02測試.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-ppt-03測試.pdf");
    }

    private static void testPPt2(String path_ppt, String pafpath) throws Exception {
        String txt1 = path_ppt + "01jquery.pptx";
        String txt2 = path_ppt + "02jquery.pptx";
        String txt3 = path_ppt + "03jquery培訓.ppt";

        OpenOfficeUtils.toPdf(txt1, pafpath + "Open-ppt-01.pdf");
        OpenOfficeUtils.toPdf(txt2, pafpath + "Open-ppt-02.pdf");
        OpenOfficeUtils.toPdf(txt3, pafpath + "Open-ppt-03.pdf");
    }

    public static void LinuxTest() throws Exception {
        String path_word = "/software/songyan/hah/01word/";
        String path_txt = "/software/songyan/hah/02txt/";
        String path_excel = "/software/songyan/hah/03excel/";
        String path_ppt = "/software/songyan/hah/04ppt/";
        String pafpath = "/software/songyan/hah/pdf/";

        System.out.println("************************");
        testTxt(path_txt, pafpath);
        System.out.println("************************");
        testExcel(path_excel, pafpath);
        System.out.println("************************");
        testPPt(path_ppt, pafpath);
        System.out.println("************************");
        testWord(path_word, pafpath);
    }

    public static void LinuxTest2() throws Exception {
        String path_word = "/software/songyan/hah/01word/";
        String path_txt = "/software/songyan/hah/02txt/";
        String path_excel = "/software/songyan/hah/03excel/";
        String path_ppt = "/software/songyan/hah/04ppt/";
        String pafpath = "/software/songyan/hah/pdf/";

        System.out.println("************************");
        testTxt2(path_txt, pafpath);
        System.out.println("************************");
        testExcel2(path_excel, pafpath);
        System.out.println("************************");
        testPPt2(path_ppt, pafpath);
        System.out.println("************************");
        testWord2(path_word, pafpath);
    }

    public static void winTest() throws Exception {
        String path_word = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/01word/";
        String path_txt = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/02txt/";
        String path_excel = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/03excel/";
        String path_ppt = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/04ppt/";
        String pafpath = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/pdf/";

        System.out.println("************************");
        testWord(path_word, pafpath);
        System.out.println("************************");
        testTxt(path_txt, pafpath);
        System.out.println("************************");
        testExcel(path_excel, pafpath);
        System.out.println("************************");
        testPPt(path_ppt, pafpath);
    }
    
    public static void main(String[] args) throws Exception {
        winTest();
    }
}
View Code

 

 (2)首次使用遇到的坑

1)看下面的代碼,需要提供的openoffice服務器的安裝地址以及端口號,需要注ip(有的需要寫內網的ip,有的需要寫成127.0.0.1,也有的需要寫成localhost),具體寫成什么形式根據電腦中一個文件的配置,具體的自行百度

2)開啟openoffice服務,如果在轉換的過程中強制性殺進程,在連接會出現連接拒絕的問題(我在windows上啟動的openoffice,使用linux連接時,有一次連接成功,可以正常的轉換,但是當時強制性的殺掉了進程,之后再重啟,重裝都會連接失敗,而且linux嘗試連接后,本機再去請求連接也會被拒絕,具體的原因還沒找到,這種方式被很多公司正式的使用,應該有解決方案),最后是在linux服務器安裝了openoffice,在linux也連接自己的服務完成的測試

三、第三種方式是jacob:這種方式使用起來很方便,把dll文件放到jre的bin目錄,引入jar包就可以使用,但是它只支持windows系統,而且轉換的速度相對aspose方式來講要慢一些,對於在部署在windows上,對轉換速度要求不高的可以使用這種方式。

(1)代碼:

1.下載jacob.jar,將jacob.jar導入程序中,把對應的jcob.dll拷貝到system32/SysWOW64目錄下,同時也拷貝到對應的jre的bin目錄下。

2.編寫轉換代碼

package jacob;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JacobUtil {
    //計時
    public static void trans(String filePath, String pdfPath,String type) {
          try {
              long old = System.currentTimeMillis();
              System.out.println("jav-轉換開始:" + filePath);
              toPdf(filePath,pdfPath, type);
              System.out.println("完成:" + pdfPath);
              long now = System.currentTimeMillis();
              System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒");
          } catch (Exception e) {
              System.out.println("轉換失敗:" + filePath);
              e.printStackTrace();
          }
      }

    //轉換
    private static void toPdf(String filePath, String pdfPath,String type) {
        if("word".equals(type)){
            word2PDF(filePath,pdfPath);
        }else if("excel".equals(type)){
            excel2PDF(filePath,pdfPath);
        }else if("ppt".equals(type)){
            ppt2PDF(filePath,pdfPath);
        }
    }
    
    private static void word2PDF(String inputFile, String pdfFile) {
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        try {
            app.setProperty("Visible", false);
            Dispatch docs = app.getProperty("Documents").toDispatch();
            Dispatch doc = Dispatch.call(docs, "Open", new Object[]{inputFile, false, true}).toDispatch();
            Dispatch.call(doc, "ExportAsFixedFormat", new Object[]{pdfFile, 17});
            Dispatch.call(doc, "Close", new Object[]{false});
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("轉換出錯:"+pdfFile);
        }finally {
            app.invoke("Quit");
        }
    }

    private static void excel2PDF(String inputFile, String pdfFile) {
        ComThread.InitSTA(true);
        ActiveXComponent app = new ActiveXComponent("Excel.Application");
        try {
            app.setProperty("Visible", false);
            app.setProperty("AutomationSecurity", new Variant(3));
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            Dispatch excel = Dispatch.invoke(excels, "Open", 1, new Object[]{inputFile, new Variant(false), new Variant(false)}, new int[9]).toDispatch();
            Dispatch.invoke(excel, "ExportAsFixedFormat", 1, new Object[]{new Variant(0), pdfFile, new Variant(0)}, new int[1]);
            Dispatch.call(excel, "Close", new Object[]{false});
            if (app != null) {
                app.invoke("Quit", new Variant[0]);
                app = null;
            }
            ComThread.Release();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("轉換出錯:"+pdfFile);
        }finally {
            app.invoke("Quit");
        }
    }

    private static void ppt2PDF(String inputFile, String pdfFile) {
        ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
        try {
            Dispatch ppts = app.getProperty("Presentations").toDispatch();
            Dispatch ppt = Dispatch.call(ppts, "Open", new Object[]{inputFile, true, true, false}).toDispatch();
            Dispatch.call(ppt, "SaveAs", new Object[]{pdfFile, 32});
            Dispatch.call(ppt, "Close");
            app.invoke("Quit");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("轉換出錯:"+inputFile);
        }finally {
            app.invoke("Quit");
        }
    }
    
   
  
}

測試:

package jacob;

/**
 * @author sonyan
 * @version 2019年9月25日 下午1:12:21
 * @desc
 */
public class Test {

    private static void testWord(String path_word, String pdfPath) throws Exception {
        String word1 = path_word + "01正方數字.docx";
        String word2 = path_word + "02正方數字.docx";
        String word3 = path_word + "03正方數字.doc";
        String word4 = path_word + "04正方數字.doc";
        String word5 = path_word + "05正方數字.docx";
        String word6 = path_word + "06正方數字.doc";

        JacobUtil.trans(word1, pdfPath + "jac-word-01測試.pdf", "word");
        JacobUtil.trans(word2, pdfPath + "jac-word-02測試.pdf", "word");
        JacobUtil.trans(word3, pdfPath + "jac-word-03測試.pdf", "word");
        JacobUtil.trans(word4, pdfPath + "jac-word-04測試.pdf", "word");
        JacobUtil.trans(word5, pdfPath + "jac-word-05測試.pdf", "word");
        JacobUtil.trans(word6, pdfPath + "jac-word-06測試.pdf", "word");

    }

    private static void testWord1(String path_word, String pdfPath) throws Exception {
        String word1 = path_word + "01.docx";
        String word2 = path_word + "02.docx";
        String word3 = path_word + "03.doc";
        String word4 = path_word + "04.doc";
        String word5 = path_word + "05.docx";
        String word6 = path_word + "06.doc";

        JacobUtil.trans(word1, pdfPath + "jac-word-01.pdf", "word");
        JacobUtil.trans(word2, pdfPath + "jac-word-02.pdf", "word");
        JacobUtil.trans(word3, pdfPath + "jac-word-03.pdf", "word");
        JacobUtil.trans(word4, pdfPath + "jac-word-04.pdf", "word");
        JacobUtil.trans(word5, pdfPath + "jac-word-05.pdf", "word");
        JacobUtil.trans(word6, pdfPath + "jac-word-06.pdf", "word");

    }

    private static void testTxt(String path_txt, String pdfPath) throws Exception {
        String txt1 = path_txt + "01jvm.txt";
        String txt2 = path_txt + "02jvm.txt";
        String txt3 = path_txt + "03jvm.txt";

        JacobUtil.trans(txt1, pdfPath + "jac-txt-01測試.pdf", "word");
        JacobUtil.trans(txt2, pdfPath + "jac-txt-02測試.pdf", "word");
        JacobUtil.trans(txt3, pdfPath + "jac-txt-03測試.pdf", "word");
    }

    private static void testTxt1(String path_txt, String pdfPath) throws Exception {
        String txt1 = path_txt + "01jvm.txt";
        String txt2 = path_txt + "02jvm.txt";
        String txt3 = path_txt + "03jvm.txt";

        JacobUtil.trans(txt1, pdfPath + "jac-txt-01.pdf", "word");
        JacobUtil.trans(txt2, pdfPath + "jac-txt-02.pdf", "word");
        JacobUtil.trans(txt3, pdfPath + "jac-txt-03.pdf", "word");
    }

    private static void testExcel(String path_excel, String pdfPath) throws Exception {

        String txt1 = path_excel + "01部門開發任務管理.xlsx";
        String txt2 = path_excel + "02部門開發任務管理.xlsx";
        String txt3 = path_excel + "03部門開發任務管理.xlsx";

        JacobUtil.trans(txt1, pdfPath + "jac-excel-01測試.pdf", "excel");
        JacobUtil.trans(txt2, pdfPath + "jac-excel-02測試.pdf", "excel");
        JacobUtil.trans(txt3, pdfPath + "jac-excel-03測試.pdf", "excel");
    }

    private static void testExcel1(String path_excel, String pdfPath) throws Exception {

        String txt1 = path_excel + "01.xlsx";
        String txt2 = path_excel + "02.xlsx";
        String txt3 = path_excel + "03.xlsx";

        JacobUtil.trans(txt1, pdfPath + "jac-excel-01.pdf", "excel");
        JacobUtil.trans(txt2, pdfPath + "jac-excel-02.pdf", "excel");
        JacobUtil.trans(txt3, pdfPath + "jac-excel-03.pdf", "excel");
    }

    private static void testPPt(String path_ppt, String pdfPath) throws Exception {

        String txt1 = path_ppt + "01jquery培訓.pptx";
        String txt2 = path_ppt + "02jquery培訓.pptx";
        String txt3 = path_ppt + "03jquery培訓.ppt";

        JacobUtil.trans(txt1, pdfPath + "jac-ppt-01測試.pdf", "ppt");
        JacobUtil.trans(txt2, pdfPath + "jac-ppt-02測試.pdf", "ppt");
        JacobUtil.trans(txt3, pdfPath + "jac-ppt-03測試.pdf", "ppt");
    }

    private static void testPPt1(String path_ppt, String pdfPath) throws Exception {
        String txt1 = path_ppt + "01jquery.pptx";
        String txt2 = path_ppt + "02jquery.pptx";
        String txt3 = path_ppt + "03jquery.ppt";

        JacobUtil.trans(txt1, pdfPath + "jac-ppt-01.pdf", "ppt");
        JacobUtil.trans(txt2, pdfPath + "jac-ppt-02.pdf", "ppt");
        JacobUtil.trans(txt3, pdfPath + "jac-ppt-03.pdf", "ppt");
    }
    
    public static void winTest() throws Exception {
        String path_word = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/01word/";
        String path_txt = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/02txt/";
        String path_excel = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/03excel/";
        String path_ppt = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/轉換前文檔/04ppt/";
        String pdfPath = "C:/Users/Administrator.DESKTOP-QN9A3AA/Desktop/office/測試文檔/pdf/";
        System.out.println("word測試");
        testWord(path_word, pdfPath);
        System.out.println("txt測試");
        testTxt(path_txt, pdfPath);
        System.out.println("ppt測試");
        testPPt(path_ppt, pdfPath);
        System.out.println("excel測試");
        testExcel(path_excel, pdfPath);
    }

    public static void LinuxTest() throws Exception {
        String path_word = "/software/songyan/hah/01word/";
        String path_txt = "/software/songyan/hah/02txt/";
        String path_excel = "/software/songyan/hah/03excel/";
        String path_ppt = "/software/songyan/hah/04ppt/";
        String pdfPath = "/software/songyan/hah/pdf/";
        
        System.out.println("word測試");
        testWord(path_word, pdfPath);
        System.out.println("txt測試");
        testTxt(path_txt, pdfPath);
        System.out.println("ppt測試");
        testPPt(path_ppt, pdfPath);
        System.out.println("excel測試");
        testExcel(path_excel, pdfPath);
    }

    public static void LinuxTest2() throws Exception {
        String path_word = "/software/songyan/hah/01word/";
        String path_txt = "/software/songyan/hah/02txt/";
        String path_excel = "/software/songyan/hah/03excel/";
        String path_ppt = "/software/songyan/hah/04ppt/";
        String pdfPath = "/software/songyan/hah/pdf/";
        
        System.out.println("word測試");
        testWord1(path_word, pdfPath);
        System.out.println("txt測試");
        testTxt1(path_txt, pdfPath);
        System.out.println("ppt測試");
        testPPt1(path_ppt, pdfPath);
        System.out.println("excel測試");
        testExcel1(path_excel, pdfPath);
    }
    
    public static void main(String[] args) throws Exception {
        winTest();
    }
    
}
View Code

 

四、測試結果

 五、項目,jar包,測試材料

鏈接:https://pan.baidu.com/s/17-dyFq7PM9wE9rMVHVawBw
提取碼:0oq5 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM