SpringBoot中Word轉PDF
SpringBoot中Word轉PDF
協議:CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0/
版權聲明:本文為原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
SpringBoot中Word轉PDF
接上一章:
把導出的Word轉化為PDF文檔
第一步:引入架包
配置pom.xml
引入包
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words-jdk16</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
引入: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>
實現方法:
public static boolean getLicense() {
boolean result = false;
try {
File file = ResourceUtils.getFile("classpath:wordtemplate/license.xml");
FileInputStream inputStream = new FileInputStream(file);
License aposeLic = new License();
aposeLic.setLicense(inputStream);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void doc2pdf(String inPath, String outPath) {
if (!getLicense()) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生
return;
}
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一個空白pdf文檔
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是將要被轉化的word文檔
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互轉換
long now = System.currentTimeMillis();
System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉化用時
} catch (Exception e) {
e.printStackTrace();
}
}
調用方法:
doc2pdf("D:\\sts4.10.0_workspace\\test\\test\\src\\main\\resources\\wordtemplate\\templateAAA.docx", "D:\\sts4.10.0_workspace\\test\\test\\src\\main\\resources\\wordtemplate\\xx.pdf");
word轉PDF完畢,生成PDF
aspose-words-15.8.0.zip
所需架包下載: https://www.codepeople.cn/imges/aspose-words-15.8.0.zip
=====================================================================