Java實現word轉pdf


1.依賴引入

word轉pdf需要 com.aspose 包,由於這個包不是在阿里雲倉庫公開的,我們不能直接通過maven下載,我們需要手動進行下載安裝

可以從如下網盤進行下載該jar包

鏈接:https://pan.baidu.com/s/1axuvPzf7mOBG4kbddFegdQ
提取碼:t70t

依賴下載下來后,我們需要手動進行依賴的安裝,首先確保maven環境變量已配置好

1)配置MAVEN_HOME。在系統變量中新建,變量名MAVEN_HOME,變量值,maven文件夾路徑;

2)配置path,找到path系統變量,點開,新建,輸入%MAVEN_HOME%\bin

 

 

 

安裝依賴

將依賴移動到D盤下,然后cmd切換到D盤目錄,執行如下指令即可完成安裝,

mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar -Dfile=aspose-words-15.8.0-jdk16.jar

最后在pom.xml中將依賴進行引入即可

        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.8.0</version>
        </dependency>

2.添加配置文件

在resource目錄下添加,license.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

3.編寫工具類實現轉換

package com.kw.vehicle.controller;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Word2PdfAsposeUtil {


    public static boolean getLicense() {
        boolean result = false;
        InputStream is = null;
        try {
            Resource resource = new ClassPathResource("license.xml");
            is = resource.getInputStream();
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static boolean doc2pdf(String inPath, String outPath) {
        if (!getLicense()) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生
            return false;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一個空白pdf文檔
            os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是將要被轉化的word文檔
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互轉換
            long now = System.currentTimeMillis();
            System.out.println("pdf轉換成功,共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉化用時
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    public static void main(String[] arg){
        String docPath = "C:\\Users\\DELL\\Desktop\\接口文檔\\騎行信息數據說明.doc";
        String pdfPath = "C:\\Users\\DELL\\Desktop\\接口文檔\\騎行信息數據說明.pdf";
        Word2PdfAsposeUtil.doc2pdf(docPath,pdfPath);
    }
}

4.測試

執行main方法即可實現轉換成功

 

 

 


免責聲明!

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



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