public void wordToPdf2(String sourcePath, String destPdfPath) {
getLicense();
File file = new File(destPdfPath);
try (FileOutputStream os = new FileOutputStream(file)) {
if (!(System.getProperty("os.name").contains("Windows") || System.getProperty("os.name").contains("windows"))) {
// linux环境指定字体目录
String fontsFolder = File.separator.concat("usr")
.concat(File.separator).concat("share")
.concat(File.separator).concat("fonts")
.concat(File.separator).concat("zh_CN");
FontSettings.setFontsFolder(fontsFolder, true);
}
Document doc = new Document(sourcePath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
logger.info("word转pdf异常,cause:{}, msg:{}", e.getCause(), e.getMessage());
// e.printStackTrace();
}
logger.info("word转pdf成功");
}
private static void getLicense() {
try (InputStream is = WordUtil.class.getClassLoader().getResourceAsStream("License.xml")) {
License license = new License();
license.setLicense(is);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
License.xml
放在项目resources目录下
<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>
pom.xml
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>