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);

    }

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值