利用aspose-words 实现 java中word转pdf文件

利用aspose-words  实现 java中word转pdf文件

首先下载aspose-words-15.8.0-jdk16.jar包

引入jar包,编写Java代码

 1 package test;
 2 
 3 import java.io.File;  
 4 import java.io.FileOutputStream;  
 5 import java.io.InputStream;  
 6   
 7 import org.aspectj.weaver.ast.Test;  
 8   
 9 import com.aspose.words.Document;  
10 import com.aspose.words.License;  
11 import com.aspose.words.SaveFormat;  
12   
13   
14 /** 
15  * @author Administrator 
16  * @version $Id$ 
17  * @since 
18  * @see 
19  */  
20 public class wordToPdf_aspose {  
21   
22     public static void main(String[] args) {  
23         doc2pdf("C:/Users/Administrator/Desktop/wordToPdf/xxx.docx","C:/Users/Administrator/Desktop/wordToPdf/xx.pdf");  
24     }  
25   
26     public static boolean getLicense() {  
27         boolean result = false;  
28         try {  
29             InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下  
30             License aposeLic = new License();  
31             aposeLic.setLicense(is);  
32             result = true;  
33         } catch (Exception e) {  
34             e.printStackTrace();  
35         }  
36         return result;  
37     }  
38   
39     public static void doc2pdf(String inPath, String outPath) {  
40         if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生  
41             return;  
42         }  
43         try {  
44             long old = System.currentTimeMillis();  
45             File file = new File(outPath); // 新建一个空白pdf文档  
46             FileOutputStream os = new FileOutputStream(file);  
47             Document doc = new Document(inPath); // Address是将要被转化的word文档  
48             doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,  
49                                          // EPUB, XPS, SWF 相互转换  
50             long now = System.currentTimeMillis();  
51             System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时  
52         } catch (Exception e) {  
53             e.printStackTrace();  
54         }  
55     }  
56 }  

license.xml(复制下面一段破解码,保存为license.xml文件,该文件破解Aspose.Words,使得生产的pdf没有自带的水印):

 1 <License>  
 2   <Data>  
 3     <Products>  
 4       <Product>Aspose.Total for Java</Product>  
 5       <Product>Aspose.Words for Java</Product>  
 6     </Products>  
 7     <EditionType>Enterprise</EditionType>  
 8     <SubscriptionExpiry>20991231</SubscriptionExpiry>  
 9     <LicenseExpiry>20991231</LicenseExpiry>  
10     <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>  
11   </Data>  
12   <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>  
13 </License> 

猜你喜欢

转载自www.cnblogs.com/haw2106/p/9089973.html