Java word转pdf方法

本文介绍了如何在Java项目中引入破解版Aspose.Word 18.10 jar包,以避免在Word转PDF过程中出现水印。首先,需要在项目resource文件夹下添加许可文件license.xml,然后在代码中设置许可并调用Aspose库进行转换。提供的代码示例展示了具体的转换方法。

1、引入破解版Aspose Word 18.10 jar
若不是破解版的Jar包,word转成pdf之后,pdf中会有水印。可以自己在网上找破解的Jar包资源。

lib:存放Jar包的位置。
source:存放许可文件,格式为xml。

2、在项目resource文件夹下添加license.xml许可文件

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>29991231</SubscriptionExpiry>
    <LicenseExpiry>29991231</LicenseExpiry>
    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
  </Data>
  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

2、word转pdf工具类
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;

import com.aspose.words.License;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;

public class WordToPdf {
public static String convert(String filePath, String fileName, String urlRoot) throws Exception {
setLicense();
String name = fileName.substring(0, fileName.lastIndexOf("."));
String pdfPath = String.format("%s\%s.pdf", filePath, name);
String wordPath = String.format("%s\%s", filePath, fileName);

    File file = new File(pdfPath);
    FileOutputStream stream = new FileOutputStream(file);

    Document doc = new Document(wordPath);
    doc.save(stream, SaveFormat.PDF);
    stream.close();

    String url = String.format("%s\\%s", urlRoot, getRelativePath(pdfPath));
    return Helper.normalizeUrl(url);
}

private static String getRelativePath(String path) {
    String truncatePath = path.substring(path.lastIndexOf("uploads"));
    return truncatePath.substring(truncatePath.indexOf("\\"));
}

private static void setLicense() throws Exception {
    InputStream stream = WordToPdf.class.getClassLoader().getResourceAsStream("static/aspose/license.xml");
    License license = new License();
    license.setLicense(stream);
}

}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值