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:UsersadminDesktopdocaspose-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服务器安装中文字体库,否则所有中文字符将会乱码,点击此处查看解决方案

 

原文地址:https://www.cnblogs.com/linjiqin/p/13563957.html