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());
}
暂记录以上三种方式,当然还有不少方式。好多东西都需要我们自己试一试。加油。
正如恐惧,若心中不先害怕,则恐惧不能入袭人心。-- 烟沙九洲
浙公网安备 33010602011771号