Word转PDF的(com.aspose.words)工具类

本文介绍了如何使用com.aspose.words库将Word文档转换为PDF,包括如何处理评估版的水印问题,以及如何引入和验证license.xml来去除水印。此外,还提供了具体的Java代码示例和调用方法,涉及文件上传和转换流程。

前言

使用com.aspose.words中的方法把word转为pdf 的正式版的jar包是收费的。可以使用评估版,但评估版会在每个生成后的文档最上方插入评估字样的水印。
在这里插入图片描述
可以在网上搜一下破解版的,加上license.xml配置文件即可把水印去掉。
另外Aspose公司旗下有很全的一套office文档管理方案,公司设在澳大利亚。
公司差不多是专做各种文件格式处理插件的,产品系列挺多,有兴趣可以到官网上看看:
https://www.aspose.com/

一、需要导入的jar包

com.aspose.words 可网上找破解版jar 包然后在项目中引入
maven项目引入本地jar包教程

二、工具类代码

package com.test.api.utils;

import com.alibaba.fastjson.JSONObject;
import com.aspose.words.*;
import org.springframework.core.io.ClassPathResource;

import java.io.*;


/**
 * @author Administrator
 * @version $Id$
 * @since
 * @see
 */
public class Word2PdfUtil {
   
   
    public static boolean getLicense() {
   
   
        boolean result = false;
        try {
   
   
            // license.xml应放在..\WebRoot\WEB-INF\classes路径下
//            InputStream is = Word2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
            ClassPathResource classPathResource = new ClassPathResource("license.xml");
            InputStream is = classPathResource.getInputStream();
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
   
   
            e.printStackTrace();
        }
        return result;
    }

    public static void doc2pdf(String inPath, String outPath) {
   
   
        if (!getLicense()) {
   
    // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
   
   
            File file = new File(outPath); // 新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
   
   
            e.printStackTrace(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值