1. 程式人生 > >Java 處理word轉pdf

Java 處理word轉pdf

直接上程式碼,採用aspose word轉化,方法類如下 ,文末又相關程式碼以及工具破解包

package com.myhexin.ifs.utils; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream;   import org.aspectj.weaver.ast.Test;   import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat;

/**  * @author Administrator  * @version $Id$  * @since  * @see  */ public class WordPdfUtil {

    public static boolean getLicense() {         boolean result = false;         try {             InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml應放在..\WebRoot\WEB-INF\classes路徑下             License aposeLic = new License();             aposeLic.setLicense(is);             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(outPath+"生成共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉化用時         } catch (Exception e) {             e.printStackTrace();         }     }          public static void savedocx(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.DOCX);// 全面支援DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,                                        // EPUB, XPS, SWF 相互轉換           long now = System.currentTimeMillis();           System.out.println(outPath+"生成共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉化用時       } catch (Exception e) {           e.printStackTrace();       }   }     //此外掛不需要安裝office服務但是此外掛加密,如不註冊會有水印     public static void main(String[] args) {     //    doc2pdf("D:/test.doc");         System.out.println(111);         doc2pdf("D:\\test.doc","D:\\test.pdf");     } }