解决openoffice的jodconverter 2.2.2之前的版本不支持预览docx格式的问题

进来的各位大佬们帮忙点击一下淘宝链接(帮家人的店铺增加点儿人气),感谢!能收藏一下就更感谢了!!!

进来的各位大佬们帮忙点击一下淘宝链接(帮家人的店铺增加点儿人气),感谢!能收藏一下就更感谢了!!!

进来的各位大佬们帮忙点击一下淘宝链接(帮家人的店铺增加点儿人气),感谢!能收藏一下就更感谢了!!!

进来的各位大佬们帮忙点击一下淘宝链接(帮家人的店铺增加点儿人气),感谢!能收藏一下就更感谢了!!!
原文:https://blog.csdn.net/make_a_difference/article/details/53771136

前导:

  1. 开发过程中经常会使用java将office系列文档转换为PDF, 一般都使用微软提供的openoffice+jodconverter 实现转换文档。
  2. openoffice既有windows版本也有linux版。不用担心生产环境是linux系统。
  3. 关于linux系统安装openoffice软件请参照:待更新…

java使用SWFTools将PDF转成swf并使用flexpaper播放PDF

1、openoffice依赖jar,以maven为例:


    
    
  1. <dependency>
  2. <groupId>com.artofsolving </groupId>
  3. <artifactId>jodconverter </artifactId>
  4. <version>2.2.1 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.openoffice </groupId>
  8. <artifactId>jurt </artifactId>
  9. <version>3.0.1 </version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.openoffice </groupId>
  13. <artifactId>ridl </artifactId>
  14. <version>3.0.1 </version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.openoffice </groupId>
  18. <artifactId>juh </artifactId>
  19. <version>3.0.1 </version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.openoffice </groupId>
  23. <artifactId>unoil </artifactId>
  24. <version>3.0.1 </version>
  25. </dependency>
  26. <!–jodconverter2.2.1必须依赖slf4j-jdk14必须这个版本,不然源码中日志会报错,很low的一个问题–>
  27. <dependency>
  28. <groupId>org.slf4j </groupId>
  29. <artifactId>slf4j-jdk14 </artifactId>
  30. <version>1.4.3 </version>
  31. </dependency>

2、直接上转换代码,需要监听openoffice应用程序8100端口即可。


    
    
  1. public void convert(File sourceFile, File targetFile) {
  2. try {
  3. // 1: 打开连接
  4. OpenOfficeConnection connection = new SocketOpenOfficeConnection( 8100);
  5. connection.connect();
  6. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
  7. // 2:获取Format
  8. DocumentFormatRegistry factory = new BasicDocumentFormatRegistry();
  9. DocumentFormat inputDocumentFormat = factory
  10. .getFormatByFileExtension(getExtensionName(sourceFile.getAbsolutePath()));
  11. DocumentFormat outputDocumentFormat = factory
  12. .getFormatByFileExtension(getExtensionName(targetFile.getAbsolutePath()));
  13. // 3:执行转换
  14. converter.convert(sourceFile, inputDocumentFormat, targetFile, outputDocumentFormat);
  15. } catch (ConnectException e) {
  16. log.info( "文档转换PDF失败");
  17. }
  18. }

3、需注意:jodconverter 在转换2007版本以后的xxx.docx文档会报错,原因大家都明03后缀名xxx.doc  07以后版本xxx.docx

查看jodconverter源码发现documentFormat不支持xxx.docx格式BasicDocumentFormatRegistry中public DocumentFormat getFormatByFileExtension(String extension)默认支持是使用doc格式

BasicDocumentFormatRegistry类源码


    
    
  1. //
  2. // JODConverter - Java OpenDocument Converter
  3. // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. package com.artofsolving.jodconverter;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
  21. private List /*<DocumentFormat>*/ documentFormats = new ArrayList();
  22. public void addDocumentFormat(DocumentFormat documentFormat) {
  23. documentFormats.add(documentFormat);
  24. }
  25. protected List /*<DocumentFormat>*/ getDocumentFormats() {
  26. return documentFormats;
  27. }
  28. /**
  29. * @param extension the file extension
  30. * @return the DocumentFormat for this extension, or null if the extension is not mapped
  31. */
  32. public DocumentFormat getFormatByFileExtension(String extension) {
  33. if (extension == null) {
  34. return null;
  35. }
  36. String lowerExtension = extension.toLowerCase();
  37. for (Iterator it = documentFormats.iterator(); it.hasNext();) {
  38. DocumentFormat format = (DocumentFormat) it.next();
  39. if (format.getFileExtension().equals(lowerExtension)) {
  40. return format;
  41. }
  42. }
  43. return null;
  44. }
  45. public DocumentFormat getFormatByMimeType(String mimeType) {
  46. for (Iterator it = documentFormats.iterator(); it.hasNext();) {
  47. DocumentFormat format = (DocumentFormat) it.next();
  48. if (format.getMimeType().equals(mimeType)) {
  49. return format;
  50. }
  51. }
  52. return null;
  53. }
  54. }
BasicDocumentFormatRegistry的默认实现类DefaultDocumentFormatRegistry  中支持的文件格式如下

    
    
  1. //
  2. // JODConverter - Java OpenDocument Converter
  3. // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. package com.artofsolving.jodconverter;
  17. public class DefaultDocumentFormatRegistry extends BasicDocumentFormatRegistry {
  18. public DefaultDocumentFormatRegistry() {
  19. final DocumentFormat pdf = new DocumentFormat( "Portable Document Format", "application/pdf", "pdf");
  20. pdf.setExportFilter(DocumentFamily.DRAWING, "draw_pdf_Export");
  21. pdf.setExportFilter(DocumentFamily.PRESENTATION, "impress_pdf_Export");
  22. pdf.setExportFilter(DocumentFamily.SPREADSHEET, "calc_pdf_Export");
  23. pdf.setExportFilter(DocumentFamily.TEXT, "writer_pdf_Export");
  24. addDocumentFormat(pdf);
  25. final DocumentFormat swf = new DocumentFormat( "Macromedia Flash", "application/x-shockwave-flash", "swf");
  26. swf.setExportFilter(DocumentFamily.DRAWING, "draw_flash_Export");
  27. swf.setExportFilter(DocumentFamily.PRESENTATION, "impress_flash_Export");
  28. addDocumentFormat(swf);
  29. final DocumentFormat xhtml = new DocumentFormat( "XHTML", "application/xhtml+xml", "xhtml");
  30. xhtml.setExportFilter(DocumentFamily.PRESENTATION, "XHTML Impress File");
  31. xhtml.setExportFilter(DocumentFamily.SPREADSHEET, "XHTML Calc File");
  32. xhtml.setExportFilter(DocumentFamily.TEXT, "XHTML Writer File");
  33. addDocumentFormat(xhtml);
  34. // HTML is treated as Text when supplied as input, but as an output it is also
  35. // available for exporting Spreadsheet and Presentation formats
  36. final DocumentFormat html = new DocumentFormat( "HTML", DocumentFamily.TEXT, "text/html", "html");
  37. html.setExportFilter(DocumentFamily.PRESENTATION, "impress_html_Export");
  38. html.setExportFilter(DocumentFamily.SPREADSHEET, "HTML (StarCalc)");
  39. html.setExportFilter(DocumentFamily.TEXT, "HTML (StarWriter)");
  40. addDocumentFormat(html);
  41. final DocumentFormat odt = new DocumentFormat( "OpenDocument Text", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.text", "odt");
  42. odt.setExportFilter(DocumentFamily.TEXT, "writer8");
  43. addDocumentFormat(odt);
  44. final DocumentFormat sxw = new DocumentFormat( "OpenOffice.org 1.0 Text Document", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "sxw");
  45. sxw.setExportFilter(DocumentFamily.TEXT, "StarOffice XML (Writer)");
  46. addDocumentFormat(sxw);
  47. final DocumentFormat doc = new DocumentFormat( "Microsoft Word", DocumentFamily.TEXT, "application/msword", "doc");
  48. doc.setExportFilter(DocumentFamily.TEXT, "MS Word 97");
  49. addDocumentFormat(doc);
  50. final DocumentFormat rtf = new DocumentFormat( "Rich Text Format", DocumentFamily.TEXT, "text/rtf", "rtf");
  51. rtf.setExportFilter(DocumentFamily.TEXT, "Rich Text Format");
  52. addDocumentFormat(rtf);
  53. final DocumentFormat wpd = new DocumentFormat( "WordPerfect", DocumentFamily.TEXT, "application/wordperfect", "wpd");
  54. addDocumentFormat(wpd);
  55. final DocumentFormat txt = new DocumentFormat( "Plain Text", DocumentFamily.TEXT, "text/plain", "txt");
  56. // set FilterName to "Text" to prevent OOo from tryign to display the "ASCII Filter Options" dialog
  57. // alternatively FilterName could be "Text (encoded)" and FilterOptions used to set encoding if needed
  58. txt.setImportOption( "FilterName", "Text");
  59. txt.setExportFilter(DocumentFamily.TEXT, "Text");
  60. addDocumentFormat(txt);
  61. final DocumentFormat wikitext = new DocumentFormat( "MediaWiki wikitext", "text/x-wiki", "wiki");
  62. wikitext.setExportFilter(DocumentFamily.TEXT, "MediaWiki");
  63. addDocumentFormat(wikitext);
  64. final DocumentFormat ods = new DocumentFormat( "OpenDocument Spreadsheet", DocumentFamily.SPREADSHEET, "application/vnd.oasis.opendocument.spreadsheet", "ods");
  65. ods.setExportFilter(DocumentFamily.SPREADSHEET, "calc8");
  66. addDocumentFormat(ods);
  67. final DocumentFormat sxc = new DocumentFormat( "OpenOffice.org 1.0 Spreadsheet", DocumentFamily.SPREADSHEET, "application/vnd.sun.xml.calc", "sxc");
  68. sxc.setExportFilter(DocumentFamily.SPREADSHEET, "StarOffice XML (Calc)");
  69. addDocumentFormat(sxc);
  70. final DocumentFormat xls = new DocumentFormat( "Microsoft Excel", DocumentFamily.SPREADSHEET, "application/vnd.ms-excel", "xls");
  71. xls.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 97");
  72. addDocumentFormat(xls);
  73. final DocumentFormat csv = new DocumentFormat( "CSV", DocumentFamily.SPREADSHEET, "text/csv", "csv");
  74. csv.setImportOption( "FilterName", "Text - txt - csv (StarCalc)");
  75. csv.setImportOption( "FilterOptions", "44,34,0"); // Field Separator: ','; Text Delimiter: '"'
  76. csv.setExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)");
  77. csv.setExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "44,34,0");
  78. addDocumentFormat(csv);
  79. final DocumentFormat tsv = new DocumentFormat( "Tab-separated Values", DocumentFamily.SPREADSHEET, "text/tab-separated-values", "tsv");
  80. tsv.setImportOption( "FilterName", "Text - txt - csv (StarCalc)");
  81. tsv.setImportOption( "FilterOptions", "9,34,0"); // Field Separator: '\t'; Text Delimiter: '"'
  82. tsv.setExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)");
  83. tsv.setExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "9,34,0");
  84. addDocumentFormat(tsv);
  85. final DocumentFormat odp = new DocumentFormat( "OpenDocument Presentation", DocumentFamily.PRESENTATION, "application/vnd.oasis.opendocument.presentation", "odp");
  86. odp.setExportFilter(DocumentFamily.PRESENTATION, "impress8");
  87. addDocumentFormat(odp);
  88. final DocumentFormat sxi = new DocumentFormat( "OpenOffice.org 1.0 Presentation", DocumentFamily.PRESENTATION, "application/vnd.sun.xml.impress", "sxi");
  89. sxi.setExportFilter(DocumentFamily.PRESENTATION, "StarOffice XML (Impress)");
  90. addDocumentFormat(sxi);
  91. final DocumentFormat ppt = new DocumentFormat( "Microsoft PowerPoint", DocumentFamily.PRESENTATION, "application/vnd.ms-powerpoint", "ppt");
  92. ppt.setExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 97");
  93. addDocumentFormat(ppt);
  94. final DocumentFormat odg = new DocumentFormat( "OpenDocument Drawing", DocumentFamily.DRAWING, "application/vnd.oasis.opendocument.graphics", "odg");
  95. odg.setExportFilter(DocumentFamily.DRAWING, "draw8");
  96. addDocumentFormat(odg);
  97. final DocumentFormat svg = new DocumentFormat( "Scalable Vector Graphics", "image/svg+xml", "svg");
  98. svg.setExportFilter(DocumentFamily.DRAWING, "draw_svg_Export");
  99. addDocumentFormat(svg);
  100. }
  101. }

 解决方法:重写BasicDocumentFormatRegistry类中public DocumentFormat getFormatByFileExtension(String extension)方法,只要是后缀名包含doc则使用doc的documentFormat文档格式


    
    
  1. //
  2. // JODConverter - Java OpenDocument Converter
  3. // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. package com.artofsolving.jodconverter;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. /**
  21. * 重写 BasicDocumentFormatRegistry 文档格式
  22. * @author HuGuangJun
  23. */
  24. public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
  25. private List /* <DocumentFormat> */ documentFormats = new ArrayList();
  26. public void addDocumentFormat(DocumentFormat documentFormat) {
  27. documentFormats.add(documentFormat);
  28. }
  29. protected List /* <DocumentFormat> */ getDocumentFormats() {
  30. return documentFormats;
  31. }
  32. /**
  33. * @param extension
  34. * the file extension
  35. * @return the DocumentFormat for this extension, or null if the extension
  36. * is not mapped
  37. */
  38. public DocumentFormat getFormatByFileExtension(String extension) {
  39. if (extension == null) {
  40. return null;
  41. }
  42. //将文件名后缀统一转化
  43. if (extension.indexOf( "doc") >= 0) {
  44. extension = "doc";
  45. }
  46. if (extension.indexOf( "ppt") >= 0) {
  47. extension = "ppt";
  48. }
  49. if (extension.indexOf( "xls") >= 0) {
  50. extension = "xls";
  51. }
  52. String lowerExtension = extension.toLowerCase();
  53. for (Iterator it = documentFormats.iterator(); it.hasNext();) {
  54. DocumentFormat format = (DocumentFormat) it.next();
  55. if (format.getFileExtension().equals(lowerExtension)) {
  56. return format;
  57. }
  58. }
  59. return null;
  60. }
  61. public DocumentFormat getFormatByMimeType(String mimeType) {
  62. for (Iterator it = documentFormats.iterator(); it.hasNext();) {
  63. DocumentFormat format = (DocumentFormat) it.next();
  64. if (format.getMimeType().equals(mimeType)) {
  65. return format;
  66. }
  67. }
  68. return null;
  69. }
  70. }


            </div>

前导:

  1. 开发过程中经常会使用java将office系列文档转换为PDF, 一般都使用微软提供的openoffice+jodconverter 实现转换文档。
  2. openoffice既有windows版本也有linux版。不用担心生产环境是linux系统。
  3. 关于linux系统安装openoffice软件请参照:待更新…

java使用SWFTools将PDF转成swf并使用flexpaper播放PDF

1、openoffice依赖jar,以maven为例:


  
  
  1. <dependency>
  2. <groupId>com.artofsolving </groupId>
  3. <artifactId>jodconverter </artifactId>
  4. <version>2.2.1 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.openoffice </groupId>
  8. <artifactId>jurt </artifactId>
  9. <version>3.0.1 </version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.openoffice </groupId>
  13. <artifactId>ridl </artifactId>
  14. <version>3.0.1 </version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.openoffice </groupId>
  18. <artifactId>juh </artifactId>
  19. <version>3.0.1 </version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.openoffice </groupId>
  23. <artifactId>unoil </artifactId>
  24. <version>3.0.1 </version>
  25. </dependency>
  26. <!–jodconverter2.2.1必须依赖slf4j-jdk14必须这个版本,不然源码中日志会报错,很low的一个问题–>
  27. <dependency>
  28. <groupId>org.slf4j </groupId>
  29. <artifactId>slf4j-jdk14 </artifactId>
  30. <version>1.4.3 </version>
  31. </dependency>

2、直接上转换代码,需要监听openoffice应用程序8100端口即可。


  
  
  1. public void convert(File sourceFile, File targetFile) {
  2. try {
  3. // 1: 打开连接
  4. OpenOfficeConnection connection = new SocketOpenOfficeConnection( 8100);
  5. connection.connect();
  6. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
  7. // 2:获取Format
  8. DocumentFormatRegistry factory = new BasicDocumentFormatRegistry();
  9. DocumentFormat inputDocumentFormat = factory
  10. .getFormatByFileExtension(getExtensionName(sourceFile.getAbsolutePath()));
  11. DocumentFormat outputDocumentFormat = factory
  12. .getFormatByFileExtension(getExtensionName(targetFile.getAbsolutePath()));
  13. // 3:执行转换
  14. converter.convert(sourceFile, inputDocumentFormat, targetFile, outputDocumentFormat);
  15. } catch (ConnectException e) {
  16. log.info( "文档转换PDF失败");
  17. }
  18. }

3、需注意:jodconverter 在转换2007版本以后的xxx.docx文档会报错,原因大家都明03后缀名xxx.doc  07以后版本xxx.docx

查看jodconverter源码发现documentFormat不支持xxx.docx格式BasicDocumentFormatRegistry中public DocumentFormat getFormatByFileExtension(String extension)默认支持是使用doc格式

BasicDocumentFormatRegistry类源码


  
  
  1. //
  2. // JODConverter - Java OpenDocument Converter
  3. // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. package com.artofsolving.jodconverter;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
  21. private List /*<DocumentFormat>*/ documentFormats = new ArrayList();
  22. public void addDocumentFormat(DocumentFormat documentFormat) {
  23. documentFormats.add(documentFormat);
  24. }
  25. protected List /*<DocumentFormat>*/ getDocumentFormats() {
  26. return documentFormats;
  27. }
  28. /**
  29. * @param extension the file extension
  30. * @return the DocumentFormat for this extension, or null if the extension is not mapped
  31. */
  32. public DocumentFormat getFormatByFileExtension(String extension) {
  33. if (extension == null) {
  34. return null;
  35. }
  36. String lowerExtension = extension.toLowerCase();
  37. for (Iterator it = documentFormats.iterator(); it.hasNext();) {
  38. DocumentFormat format = (DocumentFormat) it.next();
  39. if (format.getFileExtension().equals(lowerExtension)) {
  40. return format;
  41. }
  42. }
  43. return null;
  44. }
  45. public DocumentFormat getFormatByMimeType(String mimeType) {
  46. for (Iterator it = documentFormats.iterator(); it.hasNext();) {
  47. DocumentFormat format = (DocumentFormat) it.next();
  48. if (format.getMimeType().equals(mimeType)) {
  49. return format;
  50. }
  51. }
  52. return null;
  53. }
  54. }
BasicDocumentFormatRegistry的默认实现类DefaultDocumentFormatRegistry  中支持的文件格式如下

  
  
  1. //
  2. // JODConverter - Java OpenDocument Converter
  3. // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. package com.artofsolving.jodconverter;
  17. public class DefaultDocumentFormatRegistry extends BasicDocumentFormatRegistry {
  18. public DefaultDocumentFormatRegistry() {
  19. final DocumentFormat pdf = new DocumentFormat( "Portable Document Format", "application/pdf", "pdf");
  20. pdf.setExportFilter(DocumentFamily.DRAWING, "draw_pdf_Export");
  21. pdf.setExportFilter(DocumentFamily.PRESENTATION, "impress_pdf_Export");
  22. pdf.setExportFilter(DocumentFamily.SPREADSHEET, "calc_pdf_Export");
  23. pdf.setExportFilter(DocumentFamily.TEXT, "writer_pdf_Export");
  24. addDocumentFormat(pdf);
  25. final DocumentFormat swf = new DocumentFormat( "Macromedia Flash", "application/x-shockwave-flash", "swf");
  26. swf.setExportFilter(DocumentFamily.DRAWING, "draw_flash_Export");
  27. swf.setExportFilter(DocumentFamily.PRESENTATION, "impress_flash_Export");
  28. addDocumentFormat(swf);
  29. final DocumentFormat xhtml = new DocumentFormat( "XHTML", "application/xhtml+xml", "xhtml");
  30. xhtml.setExportFilter(DocumentFamily.PRESENTATION, "XHTML Impress File");
  31. xhtml.setExportFilter(DocumentFamily.SPREADSHEET, "XHTML Calc File");
  32. xhtml.setExportFilter(DocumentFamily.TEXT, "XHTML Writer File");
  33. addDocumentFormat(xhtml);
  34. // HTML is treated as Text when supplied as input, but as an output it is also
  35. // available for exporting Spreadsheet and Presentation formats
  36. final DocumentFormat html = new DocumentFormat( "HTML", DocumentFamily.TEXT, "text/html", "html");
  37. html.setExportFilter(DocumentFamily.PRESENTATION, "impress_html_Export");
  38. html.setExportFilter(DocumentFamily.SPREADSHEET, "HTML (StarCalc)");
  39. html.setExportFilter(DocumentFamily.TEXT, "HTML (StarWriter)");
  40. addDocumentFormat(html);
  41. final DocumentFormat odt = new DocumentFormat( "OpenDocument Text", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.text", "odt");
  42. odt.setExportFilter(DocumentFamily.TEXT, "writer8");
  43. addDocumentFormat(odt);
  44. final DocumentFormat sxw = new DocumentFormat( "OpenOffice.org 1.0 Text Document", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "sxw");
  45. sxw.setExportFilter(DocumentFamily.TEXT, "StarOffice XML (Writer)");
  46. addDocumentFormat(sxw);
  47. final DocumentFormat doc = new DocumentFormat( "Microsoft Word", DocumentFamily.TEXT, "application/msword", "doc");
  48. doc.setExportFilter(DocumentFamily.TEXT, "MS Word 97");
  49. addDocumentFormat(doc);
  50. final DocumentFormat rtf = new DocumentFormat( "Rich Text Format", DocumentFamily.TEXT, "text/rtf", "rtf");
  51. rtf.setExportFilter(DocumentFamily.TEXT, "Rich Text Format");
  52. addDocumentFormat(rtf);
  53. final DocumentFormat wpd = new DocumentFormat( "WordPerfect", DocumentFamily.TEXT, "application/wordperfect", "wpd");
  54. addDocumentFormat(wpd);
  55. final DocumentFormat txt = new DocumentFormat( "Plain Text", DocumentFamily.TEXT, "text/plain", "txt");
  56. // set FilterName to "Text" to prevent OOo from tryign to display the "ASCII Filter Options" dialog
  57. // alternatively FilterName could be "Text (encoded)" and FilterOptions used to set encoding if needed
  58. txt.setImportOption( "FilterName", "Text");
  59. txt.setExportFilter(DocumentFamily.TEXT, "Text");
  60. addDocumentFormat(txt);
  61. final DocumentFormat wikitext = new DocumentFormat( "MediaWiki wikitext", "text/x-wiki", "wiki");
  62. wikitext.setExportFilter(DocumentFamily.TEXT, "MediaWiki");
  63. addDocumentFormat(wikitext);
  64. final DocumentFormat ods = new DocumentFormat( "OpenDocument Spreadsheet", DocumentFamily.SPREADSHEET, "application/vnd.oasis.opendocument.spreadsheet", "ods");
  65. ods.setExportFilter(DocumentFamily.SPREADSHEET, "calc8");
  66. addDocumentFormat(ods);
  67. final DocumentFormat sxc = new DocumentFormat( "OpenOffice.org 1.0 Spreadsheet", DocumentFamily.SPREADSHEET, "application/vnd.sun.xml.calc", "sxc");
  68. sxc.setExportFilter(DocumentFamily.SPREADSHEET, "StarOffice XML (Calc)");
  69. addDocumentFormat(sxc);
  70. final DocumentFormat xls = new DocumentFormat( "Microsoft Excel", DocumentFamily.SPREADSHEET, "application/vnd.ms-excel", "xls");
  71. xls.setExportFilter(DocumentFamily.SPREADSHEET, "MS Excel 97");
  72. addDocumentFormat(xls);
  73. final DocumentFormat csv = new DocumentFormat( "CSV", DocumentFamily.SPREADSHEET, "text/csv", "csv");
  74. csv.setImportOption( "FilterName", "Text - txt - csv (StarCalc)");
  75. csv.setImportOption( "FilterOptions", "44,34,0"); // Field Separator: ','; Text Delimiter: '"'
  76. csv.setExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)");
  77. csv.setExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "44,34,0");
  78. addDocumentFormat(csv);
  79. final DocumentFormat tsv = new DocumentFormat( "Tab-separated Values", DocumentFamily.SPREADSHEET, "text/tab-separated-values", "tsv");
  80. tsv.setImportOption( "FilterName", "Text - txt - csv (StarCalc)");
  81. tsv.setImportOption( "FilterOptions", "9,34,0"); // Field Separator: '\t'; Text Delimiter: '"'
  82. tsv.setExportFilter(DocumentFamily.SPREADSHEET, "Text - txt - csv (StarCalc)");
  83. tsv.setExportOption(DocumentFamily.SPREADSHEET, "FilterOptions", "9,34,0");
  84. addDocumentFormat(tsv);
  85. final DocumentFormat odp = new DocumentFormat( "OpenDocument Presentation", DocumentFamily.PRESENTATION, "application/vnd.oasis.opendocument.presentation", "odp");
  86. odp.setExportFilter(DocumentFamily.PRESENTATION, "impress8");
  87. addDocumentFormat(odp);
  88. final DocumentFormat sxi = new DocumentFormat( "OpenOffice.org 1.0 Presentation", DocumentFamily.PRESENTATION, "application/vnd.sun.xml.impress", "sxi");
  89. sxi.setExportFilter(DocumentFamily.PRESENTATION, "StarOffice XML (Impress)");
  90. addDocumentFormat(sxi);
  91. final DocumentFormat ppt = new DocumentFormat( "Microsoft PowerPoint", DocumentFamily.PRESENTATION, "application/vnd.ms-powerpoint", "ppt");
  92. ppt.setExportFilter(DocumentFamily.PRESENTATION, "MS PowerPoint 97");
  93. addDocumentFormat(ppt);
  94. final DocumentFormat odg = new DocumentFormat( "OpenDocument Drawing", DocumentFamily.DRAWING, "application/vnd.oasis.opendocument.graphics", "odg");
  95. odg.setExportFilter(DocumentFamily.DRAWING, "draw8");
  96. addDocumentFormat(odg);
  97. final DocumentFormat svg = new DocumentFormat( "Scalable Vector Graphics", "image/svg+xml", "svg");
  98. svg.setExportFilter(DocumentFamily.DRAWING, "draw_svg_Export");
  99. addDocumentFormat(svg);
  100. }
  101. }

 解决方法:重写BasicDocumentFormatRegistry类中public DocumentFormat getFormatByFileExtension(String extension)方法,只要是后缀名包含doc则使用doc的documentFormat文档格式


  
  
  1. //
  2. // JODConverter - Java OpenDocument Converter
  3. // Copyright (C) 2004-2007 - Mirko Nasato <[email protected]>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. package com.artofsolving.jodconverter;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. /**
  21. * 重写 BasicDocumentFormatRegistry 文档格式
  22. * @author HuGuangJun
  23. */
  24. public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {
  25. private List /* <DocumentFormat> */ documentFormats = new ArrayList();
  26. public void addDocumentFormat(DocumentFormat documentFormat) {
  27. documentFormats.add(documentFormat);
  28. }
  29. protected List /* <DocumentFormat> */ getDocumentFormats() {
  30. return documentFormats;
  31. }
  32. /**
  33. * @param extension
  34. * the file extension
  35. * @return the DocumentFormat for this extension, or null if the extension
  36. * is not mapped
  37. */
  38. public DocumentFormat getFormatByFileExtension(String extension) {
  39. if (extension == null) {
  40. return null;
  41. }
  42. //将文件名后缀统一转化
  43. if (extension.indexOf( "doc") >= 0) {
  44. extension = "doc";
  45. }
  46. if (extension.indexOf( "ppt") >= 0) {
  47. extension = "ppt";
  48. }
  49. if (extension.indexOf( "xls") >= 0) {
  50. extension = "xls";
  51. }
  52. String lowerExtension = extension.toLowerCase();
  53. for (Iterator it = documentFormats.iterator(); it.hasNext();) {
  54. DocumentFormat format = (DocumentFormat) it.next();
  55. if (format.getFileExtension().equals(lowerExtension)) {
  56. return format;
  57. }
  58. }
  59. return null;
  60. }
  61. public DocumentFormat getFormatByMimeType(String mimeType) {
  62. for (Iterator it = documentFormats.iterator(); it.hasNext();) {
  63. DocumentFormat format = (DocumentFormat) it.next();
  64. if (format.getMimeType().equals(mimeType)) {
  65. return format;
  66. }
  67. }
  68. return null;
  69. }
  70. }


            </div>

猜你喜欢

转载自blog.csdn.net/hc_ttxs/article/details/81538644
今日推荐