注意:Linux系统转换后会乱码,原因是缺少字体,把Windows C:\Windows\Fonts 下的字体文件拷贝到Linux /usr/share/fonts 下
public class FormatConverterUtils {
/**
* @param wordPath 需要被转换的word全路径带文件名
* @param pdfPath 转换之后pdf的全路径带文件名
*/
public static void doc2pdf(String wordPath, String pdfPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
File file = new File(pdfPath); //新建一个pdf文档
try (
FileOutputStream os = new FileOutputStream(file)
) {
Document doc = new Document(wordPath); //Address是将要被转化的word文档
doc.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
} catch (Exception e) {
e.printStackTrace();
log.error("word转PDF失败!");
}
}
private static boolean getLicense() {
boolean result = false;
try (
InputStream is = FormatConverterUtils.class.getClassLoader().getResourceAsStream("license.xml");
) {
License asposeLic = new License();
asposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
log.error("license.xml文件导入失败!");
}
return result;
}
}
在resource下新建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>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
resource下新建lib文件夹,jar包复制进lib文件夹
jar包链接:https://pan.baidu.com/s/1Lj5itlQxofZHbWF2Jqf4iA?pwd=jjjj
提取码:jjjj