随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

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

 

posted on   Ruthless  阅读(13409)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2017-08-26 Disruptor入门
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示