java word转pdf

本文介绍了一个Java工具类,用于实现Word文档(DOC, DOCX等格式)到PDF的转换。通过使用aspose-words-19.2.jar库,该工具支持多种文档格式之间的相互转换,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个工具类全面支持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互转换

1. jar包下载

百度云盘 提取码:2ero
在下载的文件夹中使用maven 打包

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

在这里插入图片描述
打包好的依赖放入自己的仓库里

2在项目中进行依赖
 <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>19.2</version>
        </dependency>

import com.aspose.words.Document;

import java.io.File;
import java.io.FileOutputStream;
import java.time.Duration;
import java.time.LocalDateTime;

public class FileUtils {

    /**
     * Word 转换  Pdf
     * @param inPath 文件路径
     * @param outPath 输出pdf的路径
     */
    public static void doc2pdf(String inPath, String outPath) {
        System.out.println(LocalDateTime.now()+"===============Word 转换  Pdf=================");
	         /*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生(15.8版本,已更改为19.2版本,故此不需要验证了)
	                 return;
	         }*/
        try {
            LocalDateTime startDate = LocalDateTime.now();
            File file = new File(outPath); // 新建一个pdf文档
            FileOutputStream os = new FileOutputStream(file);
            /** 这里需要指定Linux服务器上从本地windows上传的字体库,否则生成乱码
             *  【/usr/share/fonts/windowsFonts/】位于Linux服务器上的windows字体,作为word转pdf用
             */
            //FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts/windowsFonts/", true);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            doc.save(os, com.aspose.words.SaveFormat.PDF);// 全面支持DOC, DOCX,
            // OOXML, RTF HTML,
            // OpenDocument,
            // PDF, EPUB, XPS,
            // SWF 相互转换
            LocalDateTime endDate = LocalDateTime.now();
            Duration duration = Duration.between(startDate,endDate);
            System.out.println(endDate+"---PDF转化现在是---------------- 用时:"+duration.toMillis()+" 毫秒");
            os.close();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("--------PDF转换出异常---------");
        }
    }
}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值