欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 11:36:28
前导: 发过程中经常会使用java将office系列文档转换为pdf, 一般都使用微软提供的openoffice+jodconverter 实现转换文档。 op...

前导:

发过程中经常会使用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); 
    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:执行转换 
    converter.convert(sourcefile, inputdocumentformat, targetfile, outputdocumentformat); 
  } 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 <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的默认实现类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); 
  } 
} 

 解决方法:重写basicdocumentformatregistry类中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; 
 
/** 
 * 重写 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 = 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; 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。