Word转Pdf方式

  最近在工作中需要将word文件转换为pdf文件,找了很多种方式。以下简单列一下:

一、Aspose-words(推荐)

  使用Aspose比较方便,转换之后格式这些基本没什么问题。我也使用的此种方式。正版的需要收费,网上有破解版,可以搜索一下。使用方式也很简便,引入依赖之后,几行代码即可。

/**
 * Aspose word转为pdf 推荐使用
 * 
 * @param wordPath word文件存放地址
 * @param pdfPath pdf文件存放地址
 * @throws Exception
 */
public static void word2Pdf(String wordPath, String pdfPath) throws Exception {
    Document doc = new Document(wordPath);
    // 创建 PDF 保存选项对象
    PdfSaveOptions saveOptions = new PdfSaveOptions();
    // 将文档另存为 PDF
    doc.save(pdfPath, saveOptions);
}

二、LibreOffice方式(根据情况使用)

  使用LibreOffice需要安装应用Windows和Linux下有些区别,需要注意。网上教程也比较多,此处不多讲述了。

三、opensagres/xdocreport(根据情况使用)

  使用 opensagres/xdocreport 方式,我进行过转换,格式变了。需要的朋友,可以自行试一下。需要引入依赖,附部分代码:

<dependency>
   <groupId>fr.opensagres.xdocreport</groupId>
   <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
   <version>2.0.1</version>
</dependency>
/**
 * PdfConverter word转为pdf
 * 
 * @param word word文件
 * @param pdf 转换成功的pdf文件
 * @throws IOException
 */
public static void word2Pdf(File word, File pdf) throws IOException {
    // 读取word内容
    XWPFDocument document = new XWPFDocument(Files.newInputStream(word.toPath()));
    // 将word内容写入pdf中
    PdfConverter.getInstance().convert(document, Files.newOutputStream(pdf.toPath()), PdfOptions.create());
}

  暂记录以上三种方式,当然还有不少方式。好多东西都需要我们自己试一试。加油。

正如恐惧,若心中不先害怕,则恐惧不能入袭人心。-- 烟沙九洲

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值