Java实现将word转化为PDF - Go语言中文社区

Java实现将word转化为PDF


这个工具类全面支持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互转换

转化时需要的jar包和pom:

jar包地址如下:链接:https://pan.baidu.com/s/1w1aMvTYqMKRhrRtum88HbQ 提取码:y1qg

<!-- https://mvnrepository.com/artifact/com.aspose/aspose-words -->
    <dependency>
      <groupId>com.aspose</groupId>
      <artifactId>aspose-words</artifactId>
      <version>19.2</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/lib/aspose-words-19.2-jdk16.jar</systemPath>
    </dependency>

1.word转PDF的工具类代码如下:

public class FileUtils {

    /**
     * Word 转换  Pdf
     * @param inPath
     * @param outPath
     */
    public static void doc2pdf(String inPath, String outPath) {
        System.out.println("===============Word 转换  Pdf=================");
	         /*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生(15.8版本,已更改为19.2版本,故此不需要验证了)
	                 return;
	         }*/
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个pdf文档
            FileOutputStream os = new FileOutputStream(file);
            /** 这里需要指定Linux服务器上从本地windows上传的字体库,否则生成乱码
             *  【/usr/share/fonts/windowsFonts/】位于Linux服务器上的windows字体,作为word转pdf用
             */
            //FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts/windowsFonts/", true);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            System.out.println("doc"+doc);
            doc.save(os, com.aspose.words.SaveFormat.PDF);// 全面支持DOC, DOCX,
            // OOXML, RTF HTML,
            // OpenDocument,
            // PDF, EPUB, XPS,
            // SWF 相互转换
            long now = System.currentTimeMillis();
            // 将时间对象格式化为字符串
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
            String strDate = format.format(date);
            System.out.println("---PDF转化现在是--------"+strDate+"------------");
            os.close();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("--------PDF转换出异常---------");
        }
    }
}

调用的样例:

public static void main( String[] args ) {
        String addr1 = "E:\NYQX_BQD.docx";
        String addr2 = "E:\NYQX_BQD.pdf";
        FileUtils.doc2pdf(addr1,addr2);

    }

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_34896199/article/details/106807083
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-16 10:28:03
  • 阅读 ( 772 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢