• 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;
  • 相关阅读:
    Eclipse报错:An internal error has occurred. Widget is disposed
    服务器端解决跨域问题的三种方法
    为什么要使用NoSQL
    Mybatis 中$与#的区别
    maven 打包命令,跳过测试
    maven打包跳过测试命令
    Vue 扩展插件
    node 文件打开状态
    node buffer缓冲区
    npm 命令
  • 原文地址:https://www.cnblogs.com/hunmeng/p/11983882.html
Copyright © 2020-2023  润新知