zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    spark rdd--分区理解
    2020-05-03 助教一周小结(第十二周)
    2020-04-26 助教一周小结(第十一周)
    2020-04-19 助教一周小结(第十周)
    2020-04-12 助教一周小结(第九周)
    2020-04-05 助教一周小结(第八周)
    2020-03-29 助教一周小结(第七周)
    2020-03-22 助教一周小结(第六周)
    内网渗透思考(实践)
    2020-03-15 助教一周小结(第五周)
  • 原文地址:https://www.cnblogs.com/hunmeng/p/11983882.html
Copyright © 2011-2022 走看看