引入jar包
下載地址:https://yvioo.lanzous.com/iezpdno3mob
然后打開下載的目錄打開cmd執行
mvn install:install-file -Dfile=aspose-words-15.8.0-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar
這是把jar包安裝到本地倉庫中
這樣在pom文件里引入
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>15.8.0</version> </dependency>
當然也可以直接使用jar包
然后在項目根目錄中創建一個文件(SpringBoot項目直接在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>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber> </Data> <Signature> sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= </Signature> </License>
使用工具類
AsposeUtil.java
import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; /** * @author yvioo。 */ public class AsposeUtil { /** * 驗證License 若不驗證則轉化出的pdf文檔會有水印產生 * @return */ public boolean getLicense() { boolean result = false; try { InputStream is =this.getClass().getClassLoader().getResourceAsStream("license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public static void main(String[] args) throws Exception { AsposeUtil bean = new AsposeUtil(); bean.word2Pdf2("C:\\1.docx","D:\\TEST.pdf"); } /** * word轉pdf * inpath: 輸入word的路徑 * outpath: 輸出pdf的路徑 */ public void word2Pdf2(String inpath,String outpath) throws Exception { if (!getLicense()) { System.out.println("非法------------"); return; } long old = System.currentTimeMillis(); File file = new File(outpath); FileOutputStream os = new FileOutputStream(file); //解決亂碼 //如果是windows執行,不需要加這個 //TODO 如果是linux執行,需要添加這個***** //FontSettings.setFontsFolder("/usr/share/fonts",true); Document doc = new Document(inpath); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換 doc.save(os, SaveFormat.PDF); long now = System.currentTimeMillis(); System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒"); } /** * word轉pdf * @param path pdf輸出路徑 * @param wordInput word輸入流 * @param wordName word文檔的名稱 */ public void word2pdf(String path, InputStream wordInput, String wordName) throws FileNotFoundException { if (!getLicense()) { System.out.println("非法"); return; } //新建一個空白pdf文檔 long old = System.currentTimeMillis(); File file = new File(path + wordName + ".pdf"); FileOutputStream os = new FileOutputStream(file); //Address是將要被轉化的word文檔 Document doc = null; try { doc = new Document(wordInput); } catch (Exception e) { e.printStackTrace(); } try { //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換 doc.save(os, SaveFormat.PDF); } catch (Exception e) { e.printStackTrace(); } long now = System.currentTimeMillis(); //轉化用時 System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒"); } }