当前位置:   article > 正文

基于java技术实现word转pdf_java word转pdf

java word转pdf

前言:

       在现代工作环境中,文档的处理和共享是不可或缺的任务。而将Word文件转换为PDF格式,已经成为了许多工作场景中的常见需求。无论是在商务合同、报告、简历还是其他文档类型中,将其转换为PDF格式都具有诸多优势。

        首先,让我们了解一下为什么在工作中将Word文件转换为PDF格式如此重要。PDF格式具有跨平台、可靠性高、内容保护性强等诸多优势。通过将文档转换为PDF格式,您可以确保文档在不同操作系统和设备上的一致性显示,同时保护文档内容不被随意修改,加密等一系列操作。此外,PDF格式还具有良好的打印和共享性能,使得文档的传递和展示更加方便和专业。

在本文中,我们将使用Java编程语言来实现Word文件转换为PDF格式的功能。

基于java实现word转pdf主要常见的有三种方法(Apache POI,Aspose.Words,LibreOffice和OpenOffice)

下面将先介绍他们的优缺点

一、Apache POI

缺点:

  • 由于Word和PDF的结构和样式差异较大,转换可能存在一定的格式丢失或错误。
  • 对于复杂的Word文档,转换过程可能会比较耗时和复杂。

对于Apache POI这种在实现word转pdf时可能需要大量的代码去定义格式,本文将不在介绍(不推荐使用)

二、Aspose.Words

优点:

  • Aspose.Words是一个功能强大的商业库,具有丰富的特性和性能优化。
  • 它能够直接处理Word文档,支持较高程度的格式保留和准确性。
  • Aspose.Words提供了简单易用的API接口,可灵活地控制转换过程。

缺点:

  • Aspose.Words是商业库,商业用途,要付费购买证书。

三、LibreOffice和OpenOffice 

优点:

  • LibreOffice和OpenOffice是免费的开源软件,可以自由下载和使用。
  • 它们具有丰富的功能,能够处理包括Word在内的多种文档格式。
  • 由于使用成熟的开源项目,它们对于Word转PDF的功能已经被广泛测试和验证。

缺点:

  • LibreOffice和OpenOffice,其安装包较大,可能会占用一定的系统资源。
  • 需要单独启动,可能存在程序掉线问题,对于并发时,性能会有所下降

了解完这三种方法,那么现在就具体实现一下 基于Aspose.Words、libreoffice(openoffice)技术转pdf

 一、Aspose.Words 

依赖:

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aspose</groupId>
  4. <artifactId>aspose-words</artifactId>
  5. <version>版本自己选择</version>
  6. </dependency>
  7. </dependencies>

代码:

  1. package com.atxinxin;
  2. import com.aspose.words.Document;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. /**
  6. * @author weixinxin 2024-01-25
  7. **/
  8. public class WordToPdf {
  9. /**
  10. * @param wordPath word路径
  11. * @param pdfPath pdf输出路径
  12. * @Description word转pdf
  13. * @Date 14:31 2024/01/25
  14. * @author weixinxin
  15. */
  16. public static void TextWordToPdf(String wordPath, String pdfPath) throws Exception {
  17. // if (!getLicense()) {
  18. // return;
  19. // }
  20. FileOutputStream os = null;
  21. try {
  22. long old = System.currentTimeMillis();
  23. File file = new File(pdfPath);
  24. os = new FileOutputStream(file);
  25. Document doc = new Document(wordPath);
  26. doc.save(os, com.aspose.words.SaveFormat.PDF);
  27. long now = System.currentTimeMillis();
  28. System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
  29. }finally {
  30. if(os!=null) {
  31. os.close();
  32. }
  33. }
  34. }
  35. public static void main(String[] args) throws Exception {
  36. String wordPath = "C:\\Users\\weixinxin_ext\\Desktop\\测试word转pdf.docx";
  37. String pdfPath = "C:\\Users\\weixinxin_ext\\Desktop\\测试word转pdf.pdf";
  38. TextWordToPdf(wordPath,pdfPath);
  39. }
  40. }

效果:

结果:

看word转pdf耗时所用时间可以知道很快。

但是转成功的pdf文件内容有Evaluation Only. Created with Aspose.Words. Copyright 2003-2015 Aspose Pty Ltd. 这个其实也可以理解为水印,是因为没有验证证书

看一下验证证书的结果:

验证证书代码:

将上面word转pdf注掉的代码解开,调用此方法,这个license我放到了java文件中,也可以在xml中配置,需要购买证书,将下方代码有???的填上即可

  1. private static boolean getLicense() throws Exception {
  2. boolean result = false;
  3. ByteArrayInputStream is =null;
  4. try {
  5. String licenseXml = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>????</SubscriptionExpiry><LicenseExpiry>????</LicenseExpiry><SerialNumber>?????</SerialNumber></Data><Signature>????</Signature></License>";
  6. is = new ByteArrayInputStream(licenseXml.getBytes());
  7. License license = new License();
  8. license.setLicense(is);
  9. is.close();
  10. result = true;
  11. }finally {
  12. if(is!=null) {
  13. is.close();
  14. }
  15. }
  16. return result;
  17. }

到此就基于Aspos.Words转pdf结束,很好用,简单便捷,但是需要钱

二、libreoffice

基于libreoffice转pdf,需要下载安装

libreoffice下载中文网:https://zh-cn.libreoffice.org/

本次下载是7.5.9版本,linux系统,可根据自己需求变

下载完成:

上传到Lunix系统解压:

tar -xvf LibreOffice_7.5.9_Linux_x86-64_rpm.tar.gz

进入RPMS文件中:

  1. cd LibreOffice_7.5.9.2_Linux_x86-64_rpm/RPMS/
  2. yum localinstall *.rpm

安装过程遇到选择,y,安装完成,会在opt文件目录下生成

启动命令:

nohup libreoffice7.5 --headless --accept="socket,host=0.0.0.0,port=1998;urp;" --nofirststartwizard &

查看进程:

ps -ef|grep office

以上libreoffice就安装完成,准备工作就完成了,下面上代码了

依赖:

  1. <dependency>
  2. <groupId>com.artofsolving</groupId>
  3. <artifactId>jodconverter</artifactId>
  4. <version>2.2.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.artofsolving</groupId>
  8. <artifactId>jodconverter-cli</artifactId>
  9. <version>2.2.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.openoffice</groupId>
  13. <artifactId>juh</artifactId>
  14. <version>4.1.2</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.openoffice</groupId>
  18. <artifactId>jurt</artifactId>
  19. <version>4.1.2</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.openoffice</groupId>
  23. <artifactId>ridl</artifactId>
  24. <version>4.1.2</version>
  25. </dependency>

代码:

  1. /**
  2. * @param wordPath word路径
  3. * @param pdfPath pdf输出路径
  4. * @Description libreoffice word转pdf
  5. * @Date 17:58 2024/01/25
  6. * @author weixinxin
  7. */
  8. public static void libreOfficeWordToPdf(String wordPath, String pdfPath) throws Exception {
  9. long startTime = System.currentTimeMillis();
  10. File inputFile = new File(wordPath);
  11. File outputFile = new File(pdfPath);
  12. OpenOfficeConnection connection = new SocketOpenOfficeConnection("libreoffice安装的系统ip", 1998);//启动指定的端口
  13. connection.connect();
  14. DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
  15. System.out.println("time==" + (System.currentTimeMillis() - startTime));
  16. converter.convert(inputFile, outputFile);
  17. connection.disconnect();
  18. System.out.println("time==" + (System.currentTimeMillis() - startTime));
  19. }
  20. public static void main(String[] args) throws Exception {
  21. String wordPath = "C:\\Users\\weixinxin_ext\\Desktop\\测试word转pdf.docx";
  22. String pdfPath = "C:\\Users\\weixinxin_ext\\Desktop\\测试word转pdf.pdf";
  23. // textWordToPdf(wordPath,pdfPath);
  24. libreOfficeWordToPdf(wordPath, pdfPath);
  25. }

效果:

可能是本地连接的linux系统在连接时时间较长

结果: 

根据上图可以看到时间较长,但是文件没有任何水印,费时,但免费

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/388490
推荐阅读
相关标签
  

闽ICP备14008679号