java調(diào)用openoffice將office系列文檔轉(zhuǎn)換為PDF的示例方法
前導(dǎo):
發(fā)過(guò)程中經(jīng)常會(huì)使用java將office系列文檔轉(zhuǎn)換為PDF, 一般都使用微軟提供的openoffice+jodconverter 實(shí)現(xiàn)轉(zhuǎn)換文檔。
openoffice既有windows版本也有l(wèi)inux版。不用擔(dān)心生產(chǎn)環(huán)境是linux系統(tǒng)。
1、openoffice依賴(lài)jar,以maven為例:
<dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>jurt</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>ridl</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>unoil</artifactId> <version>3.0.1</version> </dependency> <!--jodconverter2.2.1必須依賴(lài)slf4j-jdk14必須這個(gè)版本,不然源碼中日志會(huì)報(bào)錯(cuò),很low的一個(gè)問(wèn)題--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.4.3</version> </dependency>
2、直接上轉(zhuǎn)換代碼,需要監(jiān)聽(tīng)openoffice應(yīng)用程序8100端口即可。
public void convert(File sourceFile, File targetFile) { try { // 1: 打開(kāi)連接 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); // 2:獲取Format DocumentFormatRegistry factory = new BasicDocumentFormatRegistry(); DocumentFormat inputDocumentFormat = factory .getFormatByFileExtension(getExtensionName(sourceFile.getAbsolutePath())); DocumentFormat outputDocumentFormat = factory .getFormatByFileExtension(getExtensionName(targetFile.getAbsolutePath())); // 3:執(zhí)行轉(zhuǎn)換 converter.convert(sourceFile, inputDocumentFormat, targetFile, outputDocumentFormat); } catch (ConnectException e) { log.info("文檔轉(zhuǎn)換PDF失敗"); } }
3、需注意:jodconverter 在轉(zhuǎn)換2007版本以后的xxx.docx文檔會(huì)報(bào)錯(cuò),原因大家都明03后綴名xxx.doc 07以后版本xxx.docx
查看jodconverter源碼發(fā)現(xiàn)documentFormat不支持xxx.docx格式BasicDocumentFormatRegistry中public DocumentFormat getFormatByFileExtension(String extension)默認(rèn)支持是使用doc格式
BasicDocumentFormatRegistry類(lèi)源碼
// // JODConverter - Java OpenDocument Converter // Copyright (C) 2004-2007 - Mirko Nasato <mirko@artofsolving.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // http://www.gnu.org/copyleft/lesser.html // package com.artofsolving.jodconverter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class BasicDocumentFormatRegistry implements DocumentFormatRegistry { private List/*<DocumentFormat>*/ documentFormats = new ArrayList(); public void addDocumentFormat(DocumentFormat documentFormat) { documentFormats.add(documentFormat); } protected List/*<DocumentFormat>*/ getDocumentFormats() { return documentFormats; } /** * @param extension the file extension * @return the DocumentFormat for this extension, or null if the extension is not mapped */ public DocumentFormat getFormatByFileExtension(String extension) { if (extension == null) { return null; } String lowerExtension = extension.toLowerCase(); for (Iterator it = documentFormats.iterator(); it.hasNext();) { DocumentFormat format = (DocumentFormat) it.next(); if (format.getFileExtension().equals(lowerExtension)) { return format; } } return null; } public DocumentFormat getFormatByMimeType(String mimeType) { for (Iterator it = documentFormats.iterator(); it.hasNext();) { DocumentFormat format = (DocumentFormat) it.next(); if (format.getMimeType().equals(mimeType)) { return format; } } return null; } }
BasicDocumentFormatRegistry的默認(rèn)實(shí)現(xiàn)類(lèi)DefaultDocumentFormatRegistry 中支持的文件格式如下
// // JODConverter - Java OpenDocument Converter // Copyright (C) 2004-2007 - Mirko Nasato <mirko@artofsolving.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // http://www.gnu.org/copyleft/lesser.html // package com.artofsolving.jodconverter; public class DefaultDocumentFormatRegistry extends BasicDocumentFormatRegistry { public DefaultDocumentFormatRegistry() { final DocumentFormat pdf = new DocumentFormat("Portable Document Format", "application/pdf", "pdf"); pdf.setExportFilter(DocumentFamily.DRAWING, "draw_pdf_Export"); pdf.setExportFilter(DocumentFamily.PRESENTATION, "impress_pdf_Export"); pdf.setExportFilter(DocumentFamily.SPREADSHEET, "calc_pdf_Export"); pdf.setExportFilter(DocumentFamily.TEXT, "writer_pdf_Export"); addDocumentFormat(pdf); final DocumentFormat swf = new DocumentFormat("Macromedia Flash", "application/x-shockwave-flash", "swf"); swf.setExportFilter(DocumentFamily.DRAWING, "draw_flash_Export"); swf.setExportFilter(DocumentFamily.PRESENTATION, "impress_flash_Export"); addDocumentFormat(swf); final DocumentFormat xhtml = new DocumentFormat("XHTML", "application/xhtml+xml", "xhtml"); xhtml.setExportFilter(DocumentFamily.PRESENTATION, "XHTML Impress File"); xhtml.setExportFilter(DocumentFamily.SPREADSHEET, "XHTML Calc File"); xhtml.setExportFilter(DocumentFamily.TEXT, "XHTML Writer File"); addDocumentFormat(xhtml); // HTML is treated as Text when supplied as input, but as an output it is also // available for exporting Spreadsheet and Presentation formats final DocumentFormat html = new DocumentFormat("HTML", DocumentFamily.TEXT, "text/html", "html"); html.setExportFilter(DocumentFamily.PRESENTATION, "impress_html_Export"); html.setExportFilter(DocumentFamily.SPREADSHEET, "HTML (StarCalc)"); html.setExportFilter(DocumentFamily.TEXT, "HTML (StarWriter)"); addDocumentFormat(html); final DocumentFormat odt = new DocumentFormat("OpenDocument Text", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.text", "odt"); odt.setExportFilter(DocumentFamily.TEXT, "writer8"); addDocumentFormat(odt); final DocumentFormat sxw = new DocumentFormat("OpenOffice.org 1.0 Text Document", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "sxw"); sxw.setExportFilter(DocumentFamily.TEXT, "StarOffice XML (Writer)"); addDocumentFormat(sxw); final DocumentFormat doc = new DocumentFormat("Microsoft Word", DocumentFamily.TEXT, "application/msword", "doc"); doc.setExportFilter(DocumentFamily.TEXT, "MS Word 97"); addDocumentFormat(doc); final DocumentFormat rtf = new DocumentFormat("Rich Text Format", DocumentFamily.TEXT, "text/rtf", "rtf"); rtf.setExportFilter(DocumentFamily.TEXT, "Rich Text Format"); addDocumentFormat(rtf); final DocumentFormat wpd = new DocumentFormat("WordPerfect", DocumentFamily.TEXT, "application/wordperfect", "wpd"); addDocumentFormat(wpd); final DocumentFormat txt = new DocumentFormat("Plain Text", DocumentFamily.TEXT, "text/plain", "txt"); // set FilterName to "Text" to prevent OOo from tryign to display the "ASCII Filter Options" dialog // alternatively FilterName could be "Text (encoded)" and FilterOptions used to set encoding if needed txt.setImportOption("FilterName", "Text"); txt.setExportFilter(DocumentFamily.TEXT, "Text"); addDocumentFormat(txt); final DocumentFormat wikitext = new DocumentFormat("MediaWiki wikitext", "text/x-wiki", "wiki"); wikitext.setExportFilter(DocumentFamily.TEXT, "MediaWiki"); addDocumentFormat(wikitext); final DocumentFormat ods = new DocumentFormat("OpenDocument Spreadsheet", DocumentFamily.SPREADSHEET, "application/vnd.oasis.opendocument.spreadsheet", "ods"); ods.setExportFilter(DocumentFamily.SPREADSHEET, "calc8"); addDocumentFormat(ods); final DocumentFormat sxc = new DocumentFormat("OpenOffice.org 1.0 Spreadsheet", DocumentFamily.SPREADSHEET, "application/vnd.sun.xml.calc", "sxc"); sxc.setExportFilter(DocumentFamily.SPREADSHEET, "StarOffice XML (Calc)"); addDocumentFormat(sxc); final DocumentFormat xls = new DocumentFormat("Microsoft Excel", DocumentFamily.SPREADSHEET, "application/vnd.ms-excel", "xls"); xls.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 97"); addDocumentFormat(xls); final DocumentFormat csv = new DocumentFormat("CSV", DocumentFamily.SPREADSHEET, "text/csv", "csv"); csv.setImportOption("FilterName", "Text - txt - csv (StarCalc)"); csv.setImportOption("FilterOptions", "44,34,0"); // Field Separator: ','; Text Delimiter: '"' csv.setExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)"); csv.setExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "44,34,0"); addDocumentFormat(csv); final DocumentFormat tsv = new DocumentFormat("Tab-separated Values", DocumentFamily.SPREADSHEET, "text/tab-separated-values", "tsv"); tsv.setImportOption("FilterName", "Text - txt - csv (StarCalc)"); tsv.setImportOption("FilterOptions", "9,34,0"); // Field Separator: '\t'; Text Delimiter: '"' tsv.setExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)"); tsv.setExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "9,34,0"); addDocumentFormat(tsv); final DocumentFormat odp = new DocumentFormat("OpenDocument Presentation", DocumentFamily.PRESENTATION, "application/vnd.oasis.opendocument.presentation", "odp"); odp.setExportFilter(DocumentFamily.PRESENTATION, "impress8"); addDocumentFormat(odp); final DocumentFormat sxi = new DocumentFormat("OpenOffice.org 1.0 Presentation", DocumentFamily.PRESENTATION, "application/vnd.sun.xml.impress", "sxi"); sxi.setExportFilter(DocumentFamily.PRESENTATION, "StarOffice XML (Impress)"); addDocumentFormat(sxi); final DocumentFormat ppt = new DocumentFormat("Microsoft PowerPoint", DocumentFamily.PRESENTATION, "application/vnd.ms-powerpoint", "ppt"); ppt.setExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 97"); addDocumentFormat(ppt); final DocumentFormat odg = new DocumentFormat("OpenDocument Drawing", DocumentFamily.DRAWING, "application/vnd.oasis.opendocument.graphics", "odg"); odg.setExportFilter(DocumentFamily.DRAWING, "draw8"); addDocumentFormat(odg); final DocumentFormat svg = new DocumentFormat("Scalable Vector Graphics", "image/svg+xml", "svg"); svg.setExportFilter(DocumentFamily.DRAWING, "draw_svg_Export"); addDocumentFormat(svg); } }
解決方法:重寫(xiě)B(tài)asicDocumentFormatRegistry類(lèi)中public DocumentFormat getFormatByFileExtension(String extension)方法,只要是后綴名包含doc則使用doc的documentFormat文檔格式
// // JODConverter - Java OpenDocument Converter // Copyright (C) 2004-2007 - Mirko Nasato <mirko@artofsolving.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // http://www.gnu.org/copyleft/lesser.html // package com.artofsolving.jodconverter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * 重寫(xiě) BasicDocumentFormatRegistry 文檔格式 * @author HuGuangJun */ public class BasicDocumentFormatRegistry implements DocumentFormatRegistry { private List/* <DocumentFormat> */ documentFormats = new ArrayList(); public void addDocumentFormat(DocumentFormat documentFormat) { documentFormats.add(documentFormat); } protected List/* <DocumentFormat> */ getDocumentFormats() { return documentFormats; } /** * @param extension * the file extension * @return the DocumentFormat for this extension, or null if the extension * is not mapped */ public DocumentFormat getFormatByFileExtension(String extension) { if (extension == null) { return null; } //將文件名后綴統(tǒng)一轉(zhuǎn)化 if (extension.indexOf("doc") >= 0) { extension = "doc"; } if (extension.indexOf("ppt") >= 0) { extension = "ppt"; } if (extension.indexOf("xls") >= 0) { extension = "xls"; } String lowerExtension = extension.toLowerCase(); for (Iterator it = documentFormats.iterator(); it.hasNext();) { DocumentFormat format = (DocumentFormat) it.next(); if (format.getFileExtension().equals(lowerExtension)) { return format; } } return null; } public DocumentFormat getFormatByMimeType(String mimeType) { for (Iterator it = documentFormats.iterator(); it.hasNext();) { DocumentFormat format = (DocumentFormat) it.next(); if (format.getMimeType().equals(mimeType)) { return format; } } return null; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java使用openOffice對(duì)于word的轉(zhuǎn)換及遇到的問(wèn)題解決
- linux環(huán)境下安裝 openOffice 并啟動(dòng)服務(wù) 的方法
- Java利用openoffice將doc、docx轉(zhuǎn)為pdf實(shí)例代碼
- Java實(shí)現(xiàn)在線(xiàn)預(yù)覽的示例代碼(openOffice實(shí)現(xiàn))
- PHP調(diào)用OpenOffice實(shí)現(xiàn)word轉(zhuǎn)PDF的方法
- 解決linux下openoffice word文件轉(zhuǎn)PDF中文亂碼的問(wèn)題
- Java仿文庫(kù)的基本方法(openoffice+swftools+flexPaper)
- java實(shí)現(xiàn)附件預(yù)覽(openoffice+swftools+flexpaper)實(shí)例
- 通過(guò)openOffice將office文件轉(zhuǎn)成pdf
相關(guān)文章
關(guān)于Java中@SuppressWarnings的正確使用方法
這篇文章主要介紹了關(guān)于Java中@SuppressWarnings的正確使用方法,@SuppressWarnings注解主要用在取消一些編譯器產(chǎn)生的警告對(duì)代碼左側(cè)行列的遮擋,有時(shí)候這會(huì)擋住我們斷點(diǎn)調(diào)試時(shí)打的斷點(diǎn),需要的朋友可以參考下2023-05-05詳解Spring學(xué)習(xí)總結(jié)——Spring實(shí)現(xiàn)AOP的多種方式
這篇文章主要介紹了詳解Spring學(xué)習(xí)總結(jié)——Spring實(shí)現(xiàn)AOP的多種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01IDEA x64 exe文件打不開(kāi),bat能打開(kāi)問(wèn)題
這篇文章主要介紹了IDEA x64 exe文件打不開(kāi),bat能打開(kāi)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Java比較器實(shí)現(xiàn)方法項(xiàng)目案例
這篇文章主要介紹了Java比較器實(shí)現(xiàn)方法,結(jié)合具體項(xiàng)目案例形式分析了Java比較器相關(guān)排序操作技巧,需要的朋友可以參考下2019-03-03java調(diào)用js文件的兩種常用方法示例(支持V8引擎)
在Java中調(diào)用JavaScript的方法通常涉及到使用Java的腳本引擎,下面這篇文章主要給大家介紹了關(guān)于java調(diào)用js文件的兩種常用方法(支持V8引擎)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06在SpringBoot中更改默認(rèn)端口的方法總結(jié)
在本文中,小編將帶大家學(xué)習(xí)如何在 Spring Boot 中更改默認(rèn)端口,默認(rèn)情況下,嵌入式 Web 服務(wù)器使用 8080端口來(lái)啟動(dòng) Spring 引導(dǎo)應(yīng)用程序,有幾種方法可以更改該端口,文中介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07聊聊Springboot2.x的session和cookie有效期
這篇文章主要介紹了Springboot2.x的session和cookie有效期,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09SpringMVC JSON數(shù)據(jù)交互及RESTful支持實(shí)現(xiàn)方法
這篇文章主要介紹了SpringMVC JSON數(shù)據(jù)交互及RESTful支持實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06LinkedBlockingQueue鏈?zhǔn)阶枞?duì)列的使用和原理解析
這篇文章主要介紹了LinkedBlockingQueue鏈?zhǔn)阶枞?duì)列的使用和原理解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10