使用Java实现Word文件转PDF文件
创始人
2025-01-09 18:07:25
0

1.场景

  不知大家有没有注意到,在开发工作中,我们或多或少都会接触到文档,不管是开发文档,还是用户手册,都会跟文件打交道,对于使用系统的用户来说,文件要下载再打开会比较繁琐,而且会比较耗磁盘空间,很多时候用户直观地想预览这个文件,Word不支持预览,这个就需要将Word转换为PDF格式才能进行预览,但市面上很多转换的都是商业软件,但这拦不到一个想搞技术的程序猿,在此我分享一个我常用的Word转PDF的方法,希望对你们有用。

2.环境准备

  这提供几个转换的Jar包,用来转换pdf的(我使用的是springboot框架,因此用上了Maven仓库,把jar包已经导入了Maven仓库,在下面我会将jar导入到Maven中的命令写出来)

		             aspose             aspose-words             15.8.0-jdk16                               aspose             aspose-slides             19.3          

  jar包的网盘链接和提取码

链接:https://pan.baidu.com/s/1J1BPC7J5_KG_OEx8w98V2w?pwd=7284  提取码:7284  --来自百度网盘超级会员V6的分享 

  如何将jar包导入本地Maven仓库,命令如下:

mvn install:install-file -Dfile=刚下载的jar包的位置 -DgroupId=依赖里写的groupId -DartifactId=依赖里写的artifactId -Dversion=上面的version -Dpackaging=jar 示例: mvn install:install-file -Dfile=F:\项目\文件预览\lib\aspose-words-15.8.0-jdk16.jar -DgroupId=aspose -DartifactId=aspose-words -Dversion=15.8.0-jdk16 -Dpackaging=jar 

  准备工作已完成,开始上代码。

3.项目代码

  引入springboot依赖和aspose依赖:

		             aspose             aspose-words             15.8.0-jdk16                               aspose             aspose-slides             19.3                                org.springframework.boot             spring-boot-starter-web          

  创建一个工具类,封装word转pdf的操作方法,业务代码去调用这个接口即可:

import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat;  import java.io.*;  /**  * word转pdf工具类  *  * @author shmily  */ public class Word2PdfUtil {      /**      * 许可证字符串(可以放到resource下的xml文件中也可)      */     private static final String LICENSE = "" +             "" +             "Aspose.Total for JavaAspose.Words for Java" +             "Enterprise" +             "20991231" +             "20991231" +             "8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7" +             "" +             "sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=" +             "";       /**      * 设置 license 去除水印      */     private static void setLicense() {         ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(LICENSE.getBytes());         License license = new License();         try {             license.setLicense(byteArrayInputStream);         } catch (Exception e) {             e.printStackTrace();         }     }       /**      * word 转 pdf 生成至指定路径,pdf为空则上传至word同级目录      *      * @param wordPath word文件路径      * @param pdfPath  pdf文件路径      */     public static void wordConvertPdfFile(String wordPath, String pdfPath) {         FileOutputStream fileOutputStream = null;         try {             pdfPath = pdfPath == null ? getPdfFilePath(wordPath) : pdfPath;             setLicense();             File file = new File(pdfPath);             fileOutputStream = new FileOutputStream(file);             Document doc = new Document(wordPath);             doc.save(fileOutputStream, SaveFormat.PDF);         } catch (Exception e) {             e.printStackTrace();         } finally {             try {                 assert fileOutputStream != null;                 fileOutputStream.close();             } catch (IOException e) {                 e.printStackTrace();             }          }     }       /**      * word 转 pdf 生成byte字节流      *      * @param wordPath word所在的目录地址      * @return      */     public static byte[] wordConvertPdfByte(String wordPath) {         ByteArrayOutputStream fileOutputStream = null;         try {             setLicense();             fileOutputStream = new ByteArrayOutputStream();             Document doc = new Document(wordPath);             doc.save(fileOutputStream, SaveFormat.PDF);             return fileOutputStream.toByteArray();         } catch (Exception e) {             e.printStackTrace();         } finally {             try {                 assert fileOutputStream != null;                 fileOutputStream.close();             } catch (IOException e) {                 e.printStackTrace();             }          }         return null;     }        /**      * 获取 生成的 pdf 文件路径,默认与源文件同一目录      *      * @param wordPath word文件      * @return 生成的 pdf 文件      */     private static String getPdfFilePath(String wordPath) {         int lastIndexOfPoint = wordPath.lastIndexOf(".");         String pdfFilePath = "";         if (lastIndexOfPoint > -1) {             pdfFilePath = wordPath.substring(0, lastIndexOfPoint);         }         return pdfFilePath + ".pdf";     }  }  

  调用这个工具的方法,将word转换为pdf,以下是示例代码:

import alp.starcode.utils.Word2PdfUtil; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;  @RestController public class FileController {      @GetMapping("word2Pdf")     public void word2Pdf() {         Word2PdfUtil.wordConvertPdfFile("D:\\code\\pdf\\word.docx","D:\\code\\pdf\\success.pdf");     }      @GetMapping("word2PdfByte")     public byte[] word2PdfByte() {         return Word2PdfUtil.wordConvertPdfByte("D:\\code\\pdf\\word.docx");     }       } 

  下图是转换后的效果
在这里插入图片描述
在这里插入图片描述
  word文件转pdf文件完成,后续将更新excel转pdf示例。

相关内容

热门资讯

第2分钟操作!圣游科技辅助器,... 第2分钟操作!圣游科技辅助器,随意玩房卡辅助器透视(辅助)本来有挂平台(哔哩哔哩)1、随意玩房卡辅助...
五分钟手筋!好友赣南新版本挂,... 您好,好友赣南新版本挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
五分钟手筋!贰柒拾智能辅助柒,... 五分钟手筋!贰柒拾智能辅助柒,新广西老友辅助(辅助)其实存在有插件(哔哩哔哩)进入游戏-大厅左侧-新...
7分钟举措!丫丫衡阳字牌3辅助... 7分钟举措!丫丫衡阳字牌3辅助,樱花之盛辅助器下载(辅助)一贯是真的平台(哔哩哔哩)1、7分钟举措!...
第3分钟教程书!掌电竞技辅助器... 第3分钟教程书!掌电竞技辅助器,悟空大厅辅助器(辅助)一贯是有辅助器(哔哩哔哩)1)掌电竞技辅助器辅...
第2分钟法子!哈糖大菠萝可以开... 第2分钟法子!哈糖大菠萝可以开挂吗,悟空大厅辅助器下载(辅助)确实真的是有软件(哔哩哔哩)1、进入到...
第五分钟机巧!闲逸软件可以控制... 第五分钟机巧!闲逸软件可以控制的吗,新众亿挂机(辅助)都是是有神器(哔哩哔哩)第五分钟机巧!闲逸软件...
1分钟绝活!悠闲卡五星辅助,来... 1分钟绝活!悠闲卡五星辅助,来来拼十辅助(辅助)本来存在有下载(哔哩哔哩)1、悠闲卡五星辅助免费脚本...
第4分钟技法!边锋干瞪眼小程序... 第4分钟技法!边锋干瞪眼小程序辅助,道游互娱辅助免费版(辅助)本来真的有辅助器(哔哩哔哩)1、玩家可...
脚本曝光!hhpoker俱乐部... 脚本曝光!hhpoker俱乐部是干嘛的,hhpoker辅助软件(透视)切实真的有神器(哔哩哔哩)1、...