java调用openoffice将office系列文档转换为PDF的示例方法

更新时间:2023-07-20 20:13:07 阅读: 评论:0

java调⽤openoffice将office系列⽂档转换为PDF的⽰例⽅法前导:
发过程中经常会使⽤java将office系列⽂档转换为PDF,⼀般都使⽤微软提供的openoffice+jodconverter 实现转换⽂档。
openoffice既有windows版本也有linux版。不⽤担⼼⽣产环境是linux系统。
1、openoffice依赖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必须依赖slf4j-jdk14必须这个版本,不然源码中⽇志会报错,很low的⼀个问题-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.4.3</version>
</dependency>
2、直接上转换代码,需要监听openoffice应⽤程序8100端⼝即可。
public void convert(File sourceFile, File targetFile) {
深圳婚假多少天
try {
// 1: 打开连接
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
// 2:获取Format
DocumentFormatRegistry factory = new BasicDocumentFormatRegistry();
DocumentFormat inputDocumentFormat = factory
.getFormatByFileExtension(AbsolutePath()));
DocumentFormat outputDocumentFormat = factory
.getFormatByFileExtension(AbsolutePath()));
// 3:执⾏转换
} catch (ConnectException e) {
log.info("⽂档转换PDF失败");
}
}
3、需注意:jodconverter 在转换2007版本以后的xxx.docx⽂档会报错,原因⼤家都明03后缀名xxx.doc  07以后版本xxx.docx
查看jodconverter源码发现documentFormat不⽀持xxx.docx格式BasicDocumentFormatRegistry中public DocumentFormat getFormatByFileExtension(String extension)默认⽀持是使⽤doc格式
BasicDocumentFormatRegistry类源码
//
// JODConverter - Java OpenDocument Converter
// Copyright (C) 2004-2007 - Mirko Nasato <>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesr General Public
// Licen as published by the Free Software Foundation; either
// version 2.1 of the Licen, or (at your option) any later version.
//
// This library is distributed in the hope that it will be uful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesr General Public Licen for more details.
// u/copyleft/lesr.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 = LowerCa();
for (Iterator it = documentFormats.iterator(); it.hasNext();) {
DocumentFormat format = (DocumentFormat) it.next();
if (FileExtension().equals(lowerExtension)) {
return format;
}
}
return null;
}
public DocumentFormat getFormatByMimeType(String mimeType) {
for (Iterator it = documentFormats.iterator(); it.hasNext();) {
DocumentFormat format = (DocumentFormat) it.next();
if (MimeType().equals(mimeType)) {
return format;
}
}
化妆都需要什么
return null;
}普通话朗读
}
BasicDocumentFormatRegistry的默认实现类DefaultDocumentFormatRegistry  中⽀持的⽂件格式如下//
// JODConverter - Java OpenDocument Converter
// Copyright (C) 2004-2007 - Mirko Nasato <>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesr General Public
// Licen as published by the Free Software Foundation; either
// version 2.1 of the Licen, or (at your option) any later version.
//
// This library is distributed in the hope that it will be uful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesr General Public Licen for more details.
// u/copyleft/lesr.html
//
package com.artofsolving.jodconverter;
public class DefaultDocumentFormatRegistry extends BasicDocumentFormatRegistry {
public DefaultDocumentFormatRegistry() {
final DocumentFormat pdf = new DocumentFormat("Portable Document Format", "application/pdf", "pdf");
奶汤蒲菜pdf.tExportFilter(DocumentFamily.DRAWING, "draw_pdf_Export");
pdf.tExportFilter(DocumentFamily.PRESENTATION, "impress_pdf_Export");
pdf.tExportFilter(DocumentFamily.SPREADSHEET, "calc_pdf_Export");
pdf.tExportFilter(DocumentFamily.TEXT, "writer_pdf_Export");
addDocumentFormat(pdf);
final DocumentFormat swf = new DocumentFormat("Macromedia Flash", "application/x-shockwave-flash", "swf");
swf.tExportFilter(DocumentFamily.DRAWING, "draw_flash_Export");
swf.tExportFilter(DocumentFamily.PRESENTATION, "impress_flash_Export");
addDocumentFormat(swf);
final DocumentFormat xhtml = new DocumentFormat("XHTML", "application/xhtml+xml", "xhtml");
xhtml.tExportFilter(DocumentFamily.PRESENTATION, "XHTML Impress File");
xhtml.tExportFilter(DocumentFamily.SPREADSHEET, "XHTML Calc File");
xhtml.tExportFilter(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 Prentation formats
final DocumentFormat html = new DocumentFormat("HTML", DocumentFamily.TEXT, "text/html", "html");
html.tExportFilter(DocumentFamily.PRESENTATION, "impress_html_Export");
html.tExportFilter(DocumentFamily.SPREADSHEET, "HTML (StarCalc)");
html.tExportFilter(DocumentFamily.TEXT, "HTML (StarWriter)");
addDocumentFormat(html);
final DocumentFormat odt = new DocumentFormat("OpenDocument Text", DocumentFamily.TEXT, "application/vnd.", "odt");
odt.tExportFilter(DocumentFamily.TEXT, "writer8");
addDocumentFormat(odt);
final DocumentFormat sxw = new DocumentFormat("OpenOffice 1.0 Text Document", DocumentFamily.TEXT, "application/l.writer", "sxw");
sxw.tExportFilter(DocumentFamily.TEXT, "StarOffice XML (Writer)");
addDocumentFormat(sxw);
final DocumentFormat doc = new DocumentFormat("Microsoft Word", DocumentFamily.TEXT, "application/msword", "doc");
doc.tExportFilter(DocumentFamily.TEXT, "MS Word 97");
addDocumentFormat(doc);
final DocumentFormat rtf = new DocumentFormat("Rich Text Format", DocumentFamily.TEXT, "text/rtf", "rtf");
rtf.tExportFilter(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");
// t FilterName to "Text" to prevent OOo from tryign to display the "ASCII Filter Options" dialog
// alternatively FilterName could be "Text (encoded)" and FilterOptions ud to t encoding if needed
txt.tImportOption("FilterName", "Text");
txt.tExportFilter(DocumentFamily.TEXT, "Text");
addDocumentFormat(txt);
final DocumentFormat wikitext = new DocumentFormat("MediaWiki wikitext", "text/x-wiki", "wiki");
wikitext.tExportFilter(DocumentFamily.TEXT, "MediaWiki");
addDocumentFormat(wikitext);
final DocumentFormat ods = new DocumentFormat("OpenDocument Spreadsheet", DocumentFamily.SPREADSHEET, "application/vnd.oasis.opendocument.spreadsheet", "ods");    ods.tExportFilter(DocumentFamily.SPREADSHEET, "calc8");
addDocumentFormat(ods);
final DocumentFormat sxc = new DocumentFormat("OpenOffice 1.0 Spreadsheet", DocumentFamily.SPREADSHEET, "application/l.calc", "sxc");
sxc.tExportFilter(DocumentFamily.SPREADSHEET, "StarOffice XML (Calc)");
addDocumentFormat(sxc);
final DocumentFormat xls = new DocumentFormat("Microsoft Excel", DocumentFamily.SPREADSHEET, "application/vnd.ms-excel", "xls");
xls.tExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 97");
addDocumentFormat(xls);
final DocumentFormat csv = new DocumentFormat("CSV", DocumentFamily.SPREADSHEET, "text/csv", "csv");
csv.tImportOption("FilterName", "Text - txt - csv (StarCalc)");
csv.tImportOption("FilterOptions", "44,34,0"); // Field Separator: ','; Text Delimiter: '"'
csv.tExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)");
csv.tExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "44,34,0");
addDocumentFormat(csv);
final DocumentFormat tsv = new DocumentFormat("Tab-parated Values", DocumentFamily.SPREADSHEET, "text/tab-parated-values", "tsv");
tsv.tImportOption("FilterName", "Text - txt - csv (StarCalc)");
tsv.tImportOption("FilterOptions", "9,34,0"); // Field Separator: '\t'; Text Delimiter: '"'
tsv.tExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)");
tsv.tExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "9,34,0");
addDocumentFormat(tsv);
final DocumentFormat odp = new DocumentFormat("OpenDocument Prentation", DocumentFamily.PRESENTATION, "application/vnd.oasis.opendocument.prentation", "odp");    odp.tExportFilter(DocumentFamily.PRESENTATION, "impress8");
addDocumentFormat(odp);
final DocumentFormat sxi = new DocumentFormat("OpenOffice 1.0 Prentation", DocumentFamily.PRESENTATION, "application/l.impress", "sxi");
sxi.tExportFilter(DocumentFamily.PRESENTATION, "StarOffice XML (Impress)");
addDocumentFormat(sxi);
final DocumentFormat ppt = new DocumentFormat("Microsoft PowerPoint", DocumentFamily.PRESENTATION, "application/vnd.ms-powerpoint", "ppt");
ppt.tExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 97");
addDocumentFormat(ppt);
final DocumentFormat odg = new DocumentFormat("OpenDocument Drawing", DocumentFamily.DRAWING, "application/vnd.aphics", "odg");
odg.tExportFilter(DocumentFamily.DRAWING, "draw8");
addDocumentFormat(odg);
微博昵称推荐
final DocumentFormat svg = new DocumentFormat("Scalable Vector Graphics", "image/svg+xml", "svg");
svg.tExportFilter(DocumentFamily.DRAWING, "draw_svg_Export");
addDocumentFormat(svg);
}
}
解决⽅法:重写BasicDocumentFormatRegistry类中public DocumentFormat getFormatByFileExtension(String extension)⽅法,只要是后缀名包含doc则使⽤doc的documentFormat⽂档格式
//
// JODConverter - Java OpenDocument Converter
// Copyright (C) 2004-2007 - Mirko Nasato <>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesr General Public
// Licen as published by the Free Software Foundation; either
// version 2.1 of the Licen, or (at your option) any later version.
//
// This library is distributed in the hope that it will be uful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesr General Public Licen for more details.
// u/copyleft/lesr.html
//
package com.artofsolving.jodconverter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 重写 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;
磁性声音}
//将⽂件名后缀统⼀转化
if (extension.indexOf("doc") >= 0) {
extension = "doc";
}
if (extension.indexOf("ppt") >= 0) {
extension = "ppt";
}
if (extension.indexOf("xls") >= 0) {
extension = "xls";
}
String lowerExtension = LowerCa();
for (Iterator it = documentFormats.iterator(); it.hasNext();) {
DocumentFormat format = (DocumentFormat) it.next();
if (FileExtension().equals(lowerExtension)) {
return format;
}
}
怎么把歌曲下载到u盘上return null;怎么设置双面打印
}
public DocumentFormat getFormatByMimeType(String mimeType) {
for (Iterator it = documentFormats.iterator(); it.hasNext();) {
DocumentFormat format = (DocumentFormat) it.next();
if (MimeType().equals(mimeType)) {
return format;
}
}
return null;
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

本文发布于:2023-07-20 20:13:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1089563.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:转换   源码   需要   监听   报错   希望
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图