标签:word aspose import System new pdf JAVA
引入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) + "秒"); } }
标签:word,aspose,import,System,new,pdf,JAVA 来源: https://www.cnblogs.com/pxblog/p/14617688.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。