300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 通过OpenOffice实现Office文档转换为PDF格式的文档

通过OpenOffice实现Office文档转换为PDF格式的文档

时间:2024-03-19 12:59:15

相关推荐

通过OpenOffice实现Office文档转换为PDF格式的文档

声明

郑重声明本文来自CMDN jim先生 原创网址:[/u011967234/article/details/72760008](/u011967234/article/details/72760008)本人感觉 jim先生写的清楚明了,对自己有用以方便以后使用抄录过来。

环境

下载Windows版本OpenOffice;官网地址:/download/index.html。

代码实现

//main方法测试public static void main(String[] args) {convertWord2Pdf("C:\\Users\\Desktop\\-xxxxx.docx");}/**** @description:将Office格式的文档转换为PDF格式的文档* @param inputFilePath**/public static void convertWord2Pdf(String inputFilePath) {DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();// OpenOffice安装在本地环境的目录String officeHome = "D:\\OpenOffice 4";config.setOfficeHome(officeHome);OfficeManager officeManager = config.buildOfficeManager();officeManager.start();OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);String outputFilePath = getReplaceFileAbsolutePath(inputFilePath, ".pdf");File inputFile = new File(inputFilePath);if (inputFile.exists()) {File outputFile = new File(outputFilePath);if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}// 进行PDF格式的转换converter.convert(inputFile, outputFile);}officeManager.stop();}/**** @description:更改文档后缀为指定的后缀名* @param inputFilePath 输入文档的绝对路径* @param replaceEndWith 指定的后缀名* @return 返回替换指定后缀名的文档的绝对路径**/private static String getReplaceFileAbsolutePath(String inputFilePath, String replaceEndWith) {String replaceFilePath = null;Pattern pattern = pile("(\\.[a-zA-Z]+)");Matcher matcher = pattern.matcher(inputFilePath);String endWith = null;if(matcher.find()) {endWith = matcher.group(1);}if(StringUtil.isEmpty(endWith)) {return null;}replaceFilePath = inputFilePath.replaceAll(endWith, replaceEndWith);return replaceFilePath;}/**** @description:预览PDF文件* @param attathFile(文件流)* @param response**/public static void previewPdf(File attathFile, HttpServletResponse response) {response.setContentType("application/pdf");try {if(attathFile.exists()) {DataOutputStream dataOutputStream = new DataOutputStream(response.getOutputStream());DataInputStream dataInputStream = new DataInputStream(new FileInputStream(attathFile));byte[] buffer = new byte[2048];int len = buffer.length;while((len = dataInputStream.read(buffer, 0, len)) != -1) {dataOutputStream.write(buffer);dataOutputStream.flush();}dataInputStream.close();dataOutputStream.close();}} catch (Exception e) {/*logger.error(e.getMessage(), e);*/e.printStackTrace();}}

需要准备的maven依赖

<!-- 在线预览office文件 start--><!-- /artifact/commons-cli/commons-cli --><dependency><groupId>commons-cli</groupId><artifactId>commons-cli</artifactId><version>1.4</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><!-- /artifact/com.artofsolving/jodconverter --><dependency><groupId>com.artofsolving</groupId><artifactId>jodconverter</artifactId><version>2.2.1</version></dependency><!-- /artifact/org.openoffice/juh --><dependency><groupId>org.openoffice</groupId><artifactId>juh</artifactId><version>4.1.2</version></dependency><!-- /artifact/org.openoffice/jurt --><dependency><groupId>org.openoffice</groupId><artifactId>jurt</artifactId><version>4.1.2</version></dependency><!-- /artifact/org.openoffice/ridl --><dependency><groupId>org.openoffice</groupId><artifactId>ridl</artifactId><version>4.1.2</version></dependency><!-- /artifact/org.slf4j/slf4j-api --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.25</version></dependency><!-- /artifact/org.slf4j/slf4j-jdk14 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-jdk14</artifactId><version>1.7.25</version><scope>test</scope></dependency><!-- /artifact/org.openoffice/unoil --><dependency><groupId>org.openoffice</groupId><artifactId>unoil</artifactId><version>4.1.2</version></dependency><!-- /artifact/com.thoughtworks.xstream/xstream --><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.3.1</version></dependency><dependency><groupId>com.github.livesense</groupId><artifactId>jodconverter-core</artifactId><version>1.0.5</version></dependency><!-- 在线预览office文件 end-->

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。