Java使用Aspose-Words實現Word轉換Pdf



1、Maven命令安裝jar包到本地倉庫

mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar eg、mvn install:install-file -Dfile=C:\Users\admin\Desktop\doc\aspose-words-15.8.0-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar

 

2、引入Aspose-Words依賴

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

 

3、license.xml文件內容(放在/src/main/resources目錄下)

<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>

 

4、WordPdfUtil.java——封裝工具類

package com.lynch.asap.config.util; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; /** * Word 轉 Pdf 幫助類 * * @author admin * */
public class WordPdfUtil { private static boolean license = false; public static void main(String[] args) { // doc2pdf("C:\\Users\\admin\\Desktop\\doc\\111.docx","C:\\Users\\admin\\Desktop\\doc\\aaa.pdf"); // doc2pdf("C:\\Users\\admin\\Desktop\\doc\\222.docx","C:\\Users\\admin\\Desktop\\doc\\bbb.pdf"); // doc2pdf("C:\\Users\\admin\\Desktop\\doc\\333.docx","C:\\Users\\admin\\Desktop\\doc\\ccc.pdf");
        doc2pdf("/root/micro/333.docx","/root/micro/ccc.pdf"); } static { try { // license.xml放在src/main/resources文件夾下
            InputStream is = WordPdfUtil.class.getClassLoader().getResourceAsStream("license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); license = true; } catch (Exception e) { license = false; System.out.println("License驗證失敗..."); e.printStackTrace(); } } /** * doc轉pdf * * @param wordPath * @param pdfPath */
    public static void doc2pdf(String wordPath, String pdfPath) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生
        if (!license) { System.out.println("License驗證不通過..."); return; } try { long old = System.currentTimeMillis(); //新建一個pdf文檔
            File file = new File(pdfPath); FileOutputStream os = new FileOutputStream(file); //Address是將要被轉化的word文檔
            Document doc = new Document(wordPath); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換
 doc.save(os, SaveFormat.PDF); long now = System.currentTimeMillis(); os.close(); //轉化用時
            System.out.println("Word 轉 Pdf 共耗時:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) { System.out.println("Word 轉 Pdf 失敗..."); e.printStackTrace(); } } }

 

該段代碼如果想要在Linux服務器上完美運行,需要給Linux服務器安裝中文字體庫,否則所有中文字符將會亂碼,點擊此處查看解決方案

 


免責聲明!

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



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