我这里使用的包是aspose-words-15.8.0-jdk16.jar和文件license.xml(用于去水印,放在项目的resources下)
license.xml
<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>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
</Data>
<Signature>
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
</Signature>
</License>
Java代码
/**
* doc转pdf
* @param sourceFileName doc文档的路径 如:C:\Users\weipc\Desktop\html\询问笔录.doc
* @param newFileName 将要生成的pdf路径 如:C:\Users\weipc\Desktop\html\询问笔录.pdf
* @throws Exception
*/
public static void docTurnPdf(String sourceFileName,String newFileName) throws Exception {
if (!getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
File file = new File(newFileName); //新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(sourceFileName);//Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
os.close();
//删除doc文件,用不到就删掉
File f = new File(sourceFileName);
if(f.exists()){
f.delete();
}
}
public static boolean getLicense(){
boolean result = false;
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); //Test要替换成当前类名 license.xml应放在..\WebRoot\WEB-INF\classes路径下
License aposeLic = new License();
aposeLic.setLicense(is);
is.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
注:如果转成pdf文件中出现白色小方框,多数出现在Linux系统中,因为Linux系统中缺少doc对应的字体,需将C:\Windows\Fonts里的字体拷到Linux系统就好了。