广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >java 将word转为pdf文件的两种方式【spire.doc.free】【documents4j】
  • 376
分享到

java 将word转为pdf文件的两种方式【spire.doc.free】【documents4j】

javawordpdf后端 2023-08-16 20:08:11 376人浏览 安东尼
摘要

场景 如资产证明等场景下,一般要求同时生成Word与pdf两种格式的证明文件,且两者格式需保持一致,可以各自单独生成,但那样可能需要维护两个模板文件,所以也可以仅定义一份word的模板文件,使用模板生成word文件,再将word转换为pdf

场景

如资产证明等场景下,一般要求同时生成Wordpdf两种格式的证明文件,且两者格式需保持一致,可以各自单独生成,但那样可能需要维护两个模板文件,所以也可以仅定义一份word的模板文件,使用模板生成word文件,再将word转换为pdf,这样不仅少维护一个模板,也可以保证word与pdf的格式始终一致。

目标

在保留原word文件格式的情况下,通过java,后台将word文件转换为pdf文件并输出。

差异

documents4j会保留原word文件中更多的样式,如修订模式下的差异化字体颜色、文档右侧修订记录等。
spire.doc.free则不会保留修订模式下的差异。

准备

准备一份word文件(最好带有各种文本格式、表格等,以便校验转为pdf后是否仍保持原样)。
如:Alt

一、spire.doc.free 实现方式

引用pom

    e-iceblue    spire.doc.free    5.2.0

java实现

package com.example.demo.word2pdf;import com.spire.doc.Document;import com.spire.doc.FileFORMat;public class Word2PdfUtil {    public static void main(String[] args) {        // 参考:https:        //www.cnblogs.com/Carina-baby/p/16665310.html        //实例化Document类的对象        Document doc = new Document();        //加载Word        doc.loadFromFile("E:\\test2\\word模板.docx");        //保存为PDF格式        doc.saveToFile("E:\\test2\\word模板转pdf.pdf", FileFormat.PDF);    }}

效果

Alt
Alt

一、documents4j 实现方式

引用pom

    com.documents4j    documents4j-local    1.0.3    com.documents4j    documents4j-transformer-msoffice-word    1.0.3    e-iceblue    spire.doc.free    5.2.0

java

package com.example.demo.word2pdf;import com.documents4j.api.DocumentType;import com.documents4j.api.IConverter;import com.documents4j.job.LocalConverter;import java.io.*;public class Word2Pdf {    public void word2pdf() throws IOException {        // 参考:Https:        //blog.csdn.net/ka3p06/article/details/125476270 通过documents4j实现        InputStream docxInputStream = null;        OutputStream outputStream = null;        try {            // 原word地址            docxInputStream = new FileInputStream("E:\\\\test2\\\\word模板.docx");            // 转换后pdf生成地址            outputStream = new FileOutputStream("E:\\\\test2\\\\word模板转pdf.pdf");            IConverter converter = LocalConverter.builder().build();            converter.convert(docxInputStream)                    .as(DocumentType.DOCX)                    .to(outputStream)                    .as(DocumentType.PDF).execute();            // 关闭            converter.shutDown();            // 关闭            outputStream.close();            // 关闭            docxInputStream.close();        } catch (Exception e) {            System.out.println("[documents4J] word转pdf失败:" + e.toString());        } finally {            if (outputStream != null) {                outputStream.close();            }            if (docxInputStream != null) {                docxInputStream.close();            }        }    }    public static void main(String[] args) throws Exception {        Word2Pdf bean = new Word2Pdf();        bean.word2pdf();    }}

效果

Alt
Alt

来源地址:https://blog.csdn.net/weixin_44005802/article/details/128739910

--结束END--

本文标题: java 将word转为pdf文件的两种方式【spire.doc.free】【documents4j】

本文链接: https://www.lsjlt.com/news/372042.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作