Java操作word轉pdf


如果轉換后出現亂碼,是doc格式的文檔的話請轉換為docx!!!

下載相關jar包和一個授權到2099年的憑證文件。

鏈接: https://pan.baidu.com/s/1xudkKqR1-TLLO0RPskyVjQ 提取碼: adft

 

下載相關文件后請把文件都導入到項目中。

    /**
     * word轉成pdf
     * 
     * @方法名:wordToPdf
     * @參數 @param source doc路徑
     * @參數 @param target 目標pdf路徑
     * @返回類型 void
     */
    public static void wordToPdf(String source, String target) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            String licensePath = FilePathCache.FilePathMap.get("licensePath");
            InputStream license = new FileInputStream(licensePath);// 憑證文件,請正確設置讀取憑證文件的路徑
//            InputStream license = new FileInputStream("H:\\zc\\nmgzcdemo\\src\\license.xml");// 憑證文件
            License aposeLic = new License();
            aposeLic.setLicense(license);
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);
            
            //WordToPdfUtil.class.getClassLoader().getResource("");
            
            // 注冊
            //License aposeLic = new License();
            //aposeLic.setLicense(new FileInputStream(WordToPdfUtil.class
            //        .getClassLoader().getResource("license.xml").getPath()));
            

            // 轉換
            com.aspose.words.Document doc = new com.aspose.words.Document(fis);
            doc.save(fos, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

導類的相關路徑可以看一下,避免出錯。

import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.theta.system.listener.FilePathCache;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM