Java 將word轉為pdf jacob方式

來源:互聯網
上載者:User

標籤:off   需要   位置   x64   ice   component   sys   under   app   

package com.doctopdf;import java.io.File;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;/** * 效果最好的一種方法,但是需要 window 環境,而且速度是最慢的需要安裝 msofficeWord  *  * :http://www.microsoft.com/zh-cn/download/details.aspx?id=7  * jacob 包:http://sourceforge.net/projects/jacob-project/ * 將解壓出來的檔案 * jacob.jar 放在 E:\jdk1.8.0_14\jre\lib\ext   這裡使用自己的jdk安裝位置 * jacob.dll 放在 E:\jdk1.8.0_14\jre\bin       這裡使用自己的jdk安裝位置    * 如果沒放或者放錯位置,將出現報錯   * 注: x86是指32位系統 *    x64是指64位系統 *  * @author u Y * 支援doc  docx檔案轉pdf */public class DocToPdf {    private static final int wdFormatPDF = 17; // PDF 格式    17表示word轉pdf 44表示execl轉html    public void wordToPDF(String sfileName, String toFileName) {        System.out.println("啟動 Word...");        long start = System.currentTimeMillis();//轉換開始時間        ActiveXComponent app = null;        Dispatch doc = null;        try {            app = new ActiveXComponent("Word.Application");            app.setProperty("Visible", new Variant(false));            Dispatch docs = app.getProperty("Documents").toDispatch();            doc = Dispatch.call(docs, "Open", sfileName).toDispatch();            System.out.println("開啟文檔..." + sfileName);            System.out.println("轉換文檔到 PDF..." + toFileName);            File tofile = new File(toFileName);            if (tofile.exists()) {                tofile.delete();            }            // 作為 html 格式儲存到臨時檔案: 17表示word轉pdf 44表示execl轉html            Dispatch.call(doc, "SaveAs", toFileName, // FileName                    wdFormatPDF);                      long end = System.currentTimeMillis();//轉換結束時間            System.out.println("轉換完成..用時:" + (end - start) + "ms.");        } catch (Exception e) {            System.out.println("========Error:文檔轉換失敗:" + e.getMessage());        } finally {            Dispatch.call(doc, "Close", false);            System.out.println("關閉文檔");            if (app != null)                app.invoke("Quit", new Variant[] {});        }        // 如果沒有這句話,winword.exe進程將不會關閉        ComThread.Release();    }    public static void main(String[] args) {        DocToPdf d = new DocToPdf();        d.wordToPDF("C:\\Users\\Administrator\\Desktop\\畢業設計表結構設計.docx", "C:\\Spring.pdf");    }}

需要添加    jacob.jar

Java 將word轉為pdf jacob方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.