码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • java 使用documents4j将XML转为pdf文件的方式


     1.背景:

    通过spire.doc.free将word转换成PDF时存在缺陷:只能获取前3页。获取全文另外需支付费用。

    2.解决办法

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

    3.具体步骤

    1.引入Pom

    1. <dependency>
    2.     <groupId>com.documents4j</groupId>
    3.     <artifactId>documents4j-local</artifactId>
    4.     <version>1.0.3</version>
    5. </dependency>
    6. <dependency>
    7.     <groupId>com.documents4j</groupId>
    8.     <artifactId>documents4j-transformer-msoffice-word</artifactId>
    9.     <version>1.0.3</version>
    10. </dependency>

    2.  xml2pdf方法如下,xmlpath是xml文件地址,pdfPath是生成的pdf地址。

    1. public void xml2pdf(String xmlPath,String pdfPath) throws IOException {
    2. // 参考:https:
    3. //blog.csdn.net/ka3p06/article/details/125476270 通过documents4j实现
    4. InputStream docxInputStream = null;
    5. OutputStream outputStream = null;
    6. try {
    7. // 原word地址
    8. docxInputStream = new FileInputStream(xmlPath);
    9. // 转换后pdf生成地址
    10. outputStream = new FileOutputStream(pdfPath);
    11. IConverter converter = LocalConverter.builder().build();
    12. converter.convert(docxInputStream)
    13. .as(DocumentType.XML)
    14. .to(outputStream)
    15. .as(DocumentType.PDF).execute();
    16. // 关闭
    17. converter.shutDown();
    18. // 关闭
    19. outputStream.close();
    20. // 关闭
    21. docxInputStream.close();
    22. } catch (Exception e) {
    23. System.out.println("[documents4J] word转pdf失败:" + e.toString());
    24. } finally {
    25. if (outputStream != null) {
    26. outputStream.close();
    27. }
    28. if (docxInputStream != null) {
    29. docxInputStream.close();
    30. }
    31. }
    32. }

    4. documents4j也可以把word转为pdf

    只需改如下

    converter.convert(docxInputStream)
                        .as(DocumentType.DOCX)
                        .to(outputStream)
                        .as(DocumentType.PDF).execute();
  • 相关阅读:
    nginx 部署前端资源的最佳方案
    Git - 版本控制系统
    如何在控制台显示MyBatis的SQL语句
    一些java笔试题解
    微服务17:微服务治理之异常驱逐
    2023年终旺季正式拉开帷幕,赛盈分销盘点亚马逊秋季Prime Day热销款式!
    Linux 磁盘进入自检模式的解决方法
    超链接标签,列表标签,布局标签,表单标签,表单项标签
    开源大数据工具整理
    Nginx反向代理,header参数带下换线(_),导致接口调用不通,丢失header丢失的问题
  • 原文地址:https://blog.csdn.net/zhaolulu916/article/details/136188868
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号