设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间20 现在时间20 缓存数据 和聪明人交流,和靠谱的人恋爱,和进取的人共事,和幽默的人随行。晚安!

和聪明人交流,和靠谱的人恋爱,和进取的人共事,和幽默的人随行。晚安!

查看: 802|回复: 1

SpringBoot实现Word转PDF和TXT的实践分享

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:204
  • 打卡月天数:0
  • 打卡总奖励:3414
  • 最近打卡:2023-08-27 07:14:33
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
397
主题
365
精华
0
金钱
4601
积分
792
注册时间
2022-12-25
最后登录
2025-8-30

发表于 2024-9-26 20:59:08 | 显示全部楼层 |阅读模式
目录
  • 背景
  • 实践
    • 1、下载和引入Jar包
    • 2、代码实现
    • 3、测试
  • 后记

    背景

    研发工作中难免会遇到一些奇奇怪怪的需求,就比如最近,客户提了个新需求:上传一个WORD文档,要求通过系统把该文档转换成PDF和TXT。客户的需求是没得商量的,必须实现!承载着客户的期望,我开始在网上找相关的资料。没曾想,还真有开源的依赖专门处理这类问题,咱们一起来看看吧!

    实践

    1、下载和引入Jar包

    要实现WORD到PDF/TXT的转换,需要引入以下几个Jar包:

    1. <dependency>
    2. <groupId>com.aspose</groupId>
    3. <artifactId>aspose-words</artifactId>
    4. <version>19.1</version>
    5. <scope>system</scope>
    6. <systemPath>${pom.basedir}/src/main/resources/lib/aspose-words-19.1.jar</systemPath>
    7. </dependency>
    8. <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools -->
    9. <dependency>
    10. <groupId>org.apache.pdfbox</groupId>
    11. <artifactId>pdfbox</artifactId>
    12. <version>3.0.3</version>
    13. </dependency>
    复制代码

    其中,aspose-words包不太好找,在阿里云镜像库中都没有,需要在网上下载后,上传到本地的私 服库,或者用上文中的方式直接在lib中加载。我在网上找了这个地址,可以查看和下载相关包:Aspose.Words 24.4

    1.jpeg

    2、代码实现

    将依赖包引入之后,编写以下Java代码:

    1. package com.leixi.fileTrans.utils;
    2. import com.aspose.words.SaveFormat;
    3. import java.io.BufferedWriter;
    4. import java.io.File;
    5. import java.io.FileInputStream;
    6. import java.io.FileOutputStream;
    7. import java.io.OutputStreamWriter;
    8. import com.aspose.words.Document;
    9. import org.apache.pdfbox.Loader;
    10. import org.apache.pdfbox.pdmodel.PDDocument;
    11. import org.apache.pdfbox.text.PDFTextStripper;
    12. /**
    13. *
    14. * @author leixiyueqi
    15. * @since 2024/08/26 19:39
    16. */
    17. public class FileTransUtils {
    18. public static void main(String[] args) throws Exception {
    19. File file = new File("D:\\upload\\SAAS.docx");
    20. String output = "D:\\upload\\SAAS.pdf";
    21. doc2pdf(file, output);
    22. System.out.println("测度结束");
    23. }
    24. public static void doc2pdf(File file, String outPath) throws Exception{
    25. FileInputStream fis = new FileInputStream(file);
    26. Document document = new Document(fis);
    27. if (!checkDirectory(outPath)) {
    28. throw new Exception("创建目录失败");
    29. }
    30. document.save(outPath, SaveFormat.PDF);
    31. System.out.println(String.format("WORD转换Pdf成功: %s", outPath));
    32. document.save(outPath.replace(".pdf", ".txt"), SaveFormat.TEXT);
    33. System.out.println(String.format("WORD转换Txt成功: %s", outPath.replace(".pdf", ".txt")));
    34. document.save(outPath.replace(".pdf", ".html"), SaveFormat.HTML);
    35. System.out.println(String.format("WORD转换html成功: %s", outPath.replace(".pdf", ".html")));
    36. pdfToTxt(new File(outPath), new File(outPath.replace(".pdf", "ByPdf.txt")));
    37. System.out.println(String.format("通过Pdf转换Txt成功: %s", outPath.replace(".pdf", "ByPdf.txt")));
    38. }
    39. public static boolean checkDirectory(String filePath) {
    40. File file = new File(filePath);
    41. if (file.isDirectory()) {
    42. return true;
    43. } else {
    44. File dir = file.getParentFile();
    45. if (dir != null && !dir.isDirectory() && !dir.mkdirs()) {
    46. System.out.println(String.format("创建目录%s失败:", dir.getAbsolutePath()));
    47. return false;
    48. } else {
    49. return true;
    50. }
    51. }
    52. }
    53. public static void pdfToTxt(File input, File output) {
    54. BufferedWriter wr = null;
    55. try {
    56. PDDocument pd = Loader.loadPDF(input);
    57. pd.save("CopyOf" + input.getName().split("\\.")[0] + ".pdf");
    58. PDFTextStripper stripper = new PDFTextStripper();
    59. wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
    60. stripper.writeText(pd, wr);
    61. if (pd != null) {
    62. pd.close();
    63. }
    64. wr.close();
    65. } catch (Exception e) {
    66. e.printStackTrace();
    67. }finally {
    68. System.out.println("PDF转换Txt成功");
    69. }
    70. }
    71. }
    复制代码

    3、测试

    先创建一个WORD文件,放在d:\upload\文件夹下:

    2.jpeg

    然后执行Java代码中的main方法,结果如下:

    3.jpeg

    4.jpeg

    5.jpeg

    从结果来看,咱们的转换测试是非常成功的。

    后记

    这次的实践的成果还是十分有价值的,它不仅可以用于项目中,还可以应用于工作生活中,比如博主平常习惯看电子书,在网上收集到的很多资料都是PDF格式的,怎么办?用程序一转换就行了。

    但不得不说的是,这只是一个非常初级的,学习性的Demo,实际在项目中,要想实现PDF转换为TXT或其他文件,其实十分麻烦。要针对PDF文件是文字居多,还是图片/表格居多,采用不同的办法;转换的时候,还要计算图片的偏转角度,去除水印,去除格式字符等诸多操作,十分繁琐。

    以上就是SpringBoot实现Word转PDF和TXT的实践分享的详细内容,更多关于SpringBoot Word转PDF/TXT的资料请关注晓枫资讯其它相关文章!


    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
    晓枫资讯-科技资讯社区-免责声明
    免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
          1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
          2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
          3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
          4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
          5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

      离线 

    TA的专栏

    等级头衔

    等級:晓枫资讯-列兵

    在线时间
    0 小时

    积分成就
    威望
    0
    贡献
    0
    主题
    0
    精华
    0
    金钱
    21
    积分
    22
    注册时间
    2022-12-24
    最后登录
    2022-12-24

    发表于 7 天前 | 显示全部楼层
    顶顶更健康!!!
    http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
    严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    1楼
    2楼

    手机版|晓枫资讯--科技资讯社区 本站已运行

    CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

    晓枫资讯--科技资讯社区

    本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

    如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

    Powered by Discuz! X3.5

    快速回复 返回顶部 返回列表