word转pdf(用正确的license.xml,无水印,doc和docx都可转)
这是引用的jar,aspose-words-15.8.0-jdk16.jar,和文中License匹配。下载jar引用或者maven引用。(可以在下载后直接放入项目或者先添加到maven再用)。maven仓库用http://repo.e-iceblue.cn/repository/maven-public/
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<!-- word转pdf -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<classifier>jdk16</classifier>
</dependency>
下面是代码
package util;
import java.io.*;
import com.aspose.words.SaveFormat;
import com.aspose.words.Document;
import com.aspose.words.License;
public class WordToPdfUtils {
/**
* 验证aspose.word组件是否授权:无授权的文件有水印标记
* 需要使用(aspose-words-15.8.0-jdk16.jar),版本要对应。无水印
*/
public static boolean isWordLicense() {
boolean result = false;
try {
String s = "<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>";
ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes());
License license = new License();
license.setLicense(inputStream);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void wordToPdf(String oldPath, String outputPath) {
//凭证 不然切换后有水印
// 验证License
if (!isWordLicense()) {
return;
}
FileOutputStream os = null;
try {
//生成一个空的PDF文件
File file = new File(oldPath);
os = new FileOutputStream(file);
//要转换的word文件
Document doc = new Document(outputPath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//测试方法
public static void main(String[] args) {
WordToPdfUtils.wordToPdf("word文件地址","生成pdf地址");
}
}