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;
posted @ 2019-12-04 16:45  海角的远方  阅读(1533)  评论(0编辑  收藏  举报