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





