背景
研发工作中难免会遇到一些奇奇怪怪的需求,就比如最近,客户提了个新需求:上传一个word文档,要求通过系统把该文档转换成pdf和txt。客户的需求是没得商量的,必须实现!承载着客户的期望,我开始在网上找相关的资料。没曾想,还真有开源的依赖专门处理这类问题,咱们一起来看看吧!
实践
1、下载和引入jar包
要实现word到pdf/txt的转换,需要引入以下几个jar包:
<dependency> <groupid>com.aspose</groupid> <artifactid>aspose-words</artifactid> <version>19.1</version> <scope>system</scope> <systempath>${pom.basedir}/src/main/resources/lib/aspose-words-19.1.jar</systempath> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools --> <dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>pdfbox</artifactid> <version>3.0.3</version> </dependency>
其中,aspose-words包不太好找,在阿里云镜像库中都没有,需要在网上下载后,上传到本地的私 服库,或者用上文中的方式直接在lib中加载。我在网上找了这个地址,可以查看和下载相关包:aspose.words 24.4
2、代码实现
将依赖包引入之后,编写以下java代码:
package com.leixi.filetrans.utils; import com.aspose.words.saveformat; import java.io.bufferedwriter; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.outputstreamwriter; import com.aspose.words.document; import org.apache.pdfbox.loader; import org.apache.pdfbox.pdmodel.pddocument; import org.apache.pdfbox.text.pdftextstripper; /** * * @author leixiyueqi * @since 2024/08/26 19:39 */ public class filetransutils { public static void main(string[] args) throws exception { file file = new file("d:\\upload\\saas.docx"); string output = "d:\\upload\\saas.pdf"; doc2pdf(file, output); system.out.println("测度结束"); } public static void doc2pdf(file file, string outpath) throws exception{ fileinputstream fis = new fileinputstream(file); document document = new document(fis); if (!checkdirectory(outpath)) { throw new exception("创建目录失败"); } document.save(outpath, saveformat.pdf); system.out.println(string.format("word转换pdf成功: %s", outpath)); document.save(outpath.replace(".pdf", ".txt"), saveformat.text); system.out.println(string.format("word转换txt成功: %s", outpath.replace(".pdf", ".txt"))); document.save(outpath.replace(".pdf", ".html"), saveformat.html); system.out.println(string.format("word转换html成功: %s", outpath.replace(".pdf", ".html"))); pdftotxt(new file(outpath), new file(outpath.replace(".pdf", "bypdf.txt"))); system.out.println(string.format("通过pdf转换txt成功: %s", outpath.replace(".pdf", "bypdf.txt"))); } public static boolean checkdirectory(string filepath) { file file = new file(filepath); if (file.isdirectory()) { return true; } else { file dir = file.getparentfile(); if (dir != null && !dir.isdirectory() && !dir.mkdirs()) { system.out.println(string.format("创建目录%s失败:", dir.getabsolutepath())); return false; } else { return true; } } } public static void pdftotxt(file input, file output) { bufferedwriter wr = null; try { pddocument pd = loader.loadpdf(input); pd.save("copyof" + input.getname().split("\\.")[0] + ".pdf"); pdftextstripper stripper = new pdftextstripper(); wr = new bufferedwriter(new outputstreamwriter(new fileoutputstream(output))); stripper.writetext(pd, wr); if (pd != null) { pd.close(); } wr.close(); } catch (exception e) { e.printstacktrace(); }finally { system.out.println("pdf转换txt成功"); } } }
3、测试
先创建一个word文件,放在d:\upload\文件夹下:
然后执行java代码中的main方法,结果如下:
从结果来看,咱们的转换测试是非常成功的。
后记
这次的实践的成果还是十分有价值的,它不仅可以用于项目中,还可以应用于工作生活中,比如博主平常习惯看电子书,在网上收集到的很多资料都是pdf格式的,怎么办?用程序一转换就行了。
但不得不说的是,这只是一个非常初级的,学习性的demo,实际在项目中,要想实现pdf转换为txt或其他文件,其实十分麻烦。要针对pdf文件是文字居多,还是图片/表格居多,采用不同的办法;转换的时候,还要计算图片的偏转角度,去除水印,去除格式字符等诸多操作,十分繁琐。
以上就是springboot实现word转pdf和txt的实践分享的详细内容,更多关于springboot word转pdf/txt的资料请关注代码网其它相关文章!
发表评论