JAVA使用OpenOffice文件转换

Posted 大萝卜叽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA使用OpenOffice文件转换相关的知识,希望对你有一定的参考价值。

下载jar包

maven中央仓库包不支持docx文件 所以不建议使用。jar包是为了方便链接

下载链接:https://nchc.dl.sourceforge.net/project/jodconverter/JODConverter/2.2.2/jodconverter-2.2.2.zip 

解压后找到:jodconverter-2.2.2\\jodconverter-2.2.2\\lib\\jodconverter-2.2.2.jar

放到本地maven仓库

mvn install:install-file -Dfile=jodconverter-2.2.2.jar -DgroupId=com.artofsolving -DartifactId=jodconverter -Dversion=2.2.2 -Dpackaging=jar
<dependency>
  <groupId>com.artofsolving</groupId>
  <artifactId>jodconverter</artifactId>
  <version>2.2.2</version>
</dependency>

链接代码

       // 文件输入输出流
         FileInputStream in = new FileInputStream(inputFile);//new File("D:\\\\file\\\\c.doxc")
        FileOutputStream out = new FileOutputStream(outputFile);//new File("D:\\\\file\\\\a.pdf")
        // 创建连接
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeHost, openOfficePost);
            connection.connect();
            // 创建转换器
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            // 待转换文件名称及扩展名称
            String officeName = inputFile.getName();
            String officeExt = officeName.substring(officeName.lastIndexOf(".") + 1);
            // 文件格式
            DefaultDocumentFormatRegistry factory = new DefaultDocumentFormatRegistry();
            // 待转换文件 转换文件格式对象
            DocumentFormat officeFormat = factory.getFormatByFileExtension(officeExt);//如果使用的2.2.1的jar包,要转换的word后缀是docx!这里会返回null
            DocumentFormat pdfFormat = factory.getFormatByFileExtension("pdf");
       converter.convert(in, officeFormat, out, pdfFormat);//word转换PDF
      //断开连接
      connection.disconnect();

使用 C# 中的 OpenOffice 转换文件格式

【中文标题】使用 C# 中的 OpenOffice 转换文件格式【英文标题】:Using OpenOffice from C# to convert File Formats 【发布时间】:2009-07-23 18:03:32 【问题描述】:

有没有人使用过来自OpenOffice 的CLI 库,允许在.NET 应用程序中使用它? 我正在尝试通过以下方式将文档保存为 HTML 格式。

您定义一个作为参数传递给 save 方法的属性。 在这种情况下,我找到了将文档另存为 Microsoft Word 97 的示例,并且为属性提供了一个类似“FilterName”的名称和字符串值“swriter: MS Word 97”。

问题是他们没有记录保存为 HTML 文档的字符串值是什么。

代码如下:

 propertyValues[1] = new unoidl.com.sun.star.beans.PropertyValue();
 propertyValues[1].Name = "FilterName";
 propertyValues[1].Value = new uno.Any("swriter: MS Word 97");

 XStorable xStorable = xComponent as XStorable;
 xStorable.storeToURL(PathConverter(FileName), propertyValues);

在哪里可以找到 OpenOffice 的源代码以便追踪它?

【问题讨论】:

【参考方案1】:

使用以下字符串“HTML (StarWriter)”作为过滤器名称。

Value 使导出效果非常好,也发现有用且 PDF 过滤器导出器“writer_pdf_Export”运行良好。

实际上有一个所有出口/进口商的列表(我没有 知道是否一切正常,或者他们是否需要插件才能工作):

'AportisDoc Palm DB',
'BMP - MS Windows',
'CGM - Computer Graphics Metafile',
'DIF',
'DXF - AutoCAD Interchange',
'DocBook File',
'EMF - MS Windows Metafile',
'EPS - Encapsulated PostScript',
'Flat XML File',
'GIF - Graphics Interchange',
'HTML',
'HTML (StarCalc)',
'HTML (StarWriter)',
'JPG - JPEG',
'Lotus',
'MET - OS/2 Metafile',
'MS Excel 4.0',
'MS Excel 4.0 Vorlage/Template',
'MS Excel 5.0/95',
'MS Excel 5.0/95 Vorlage/Template',
'MS Excel 95',
'MS Excel 95 Vorlage/Template',
'MS Excel 97',
'MS Excel 97 Vorlage/Template',
'MS PowerPoint 97',
'MS PowerPoint 97 Vorlage',
'MS WinWord 6.0',
'MS Word 95',
'MS Word 95 Vorlage',
'MS Word 97',
'MS Word 97 Vorlage',
'MathML XML (Math)',
'MathType 3.x',
'Microsoft Word 2003 XML',
'PBM - Portable Bitmap',
'PCT - Mac Pict',
'PCX - Zsoft Paintbrush',
'PGM - Portable Graymap',
'PNG - Portable Network Graphic',
'PPM - Portable Pixelmap',
'PSD - Adobe Photoshop',
'RAS - Sun Rasterfile',
'Rich Text Format',
'Rich Text Format (StarCalc)',
'SGF - StarOffice Writer SGF',
'SGV - StarDraw 2.0',
'SVM - StarView Metafile',
'SYLK',
'StarCalc 1.0',
'StarCalc 3.0',
'StarCalc 3.0 Vorlage/Template',
'StarCalc 4.0',
'StarCalc 4.0 Vorlage/Template',
'StarCalc 5.0',
'StarCalc 5.0 Vorlage/Template',
'StarChart 3.0',
'StarChart 4.0',
'StarChart 5.0',
'StarDraw 3.0',
'StarDraw 3.0 (StarImpress)',
'StarDraw 3.0 Vorlage',
'StarDraw 3.0 Vorlage (StarImpress)',
'StarDraw 5.0',
'StarDraw 5.0 (StarImpress)',
'StarDraw 5.0 Vorlage',
'StarDraw 5.0 Vorlage (StarImpress)',
'StarImpress 4.0',
'StarImpress 4.0 Vorlage',
'StarImpress 5.0',
'StarImpress 5.0 (packed)',
'StarImpress 5.0 Vorlage',
'StarMath 2.0',
'StarMath 3.0',
'StarMath 4.0',
'StarMath 5.0',
'StarOffice XML (Calc)',
'StarOffice XML (Chart)',
'StarOffice XML (Draw)',
'StarOffice XML (Impress)',
'StarOffice XML (Math)',
'StarOffice XML (Writer)',
'StarWriter 1.0',
'StarWriter 2.0',
'StarWriter 3.0',
'StarWriter 3.0 (StarWriter/GlobalDocument)',
'StarWriter 3.0 (StarWriter/Web)',
'StarWriter 3.0 Vorlage/Template',
'StarWriter 4.0',
'StarWriter 4.0 (StarWriter/GlobalDocument)',
'StarWriter 4.0 (StarWriter/Web)',
'StarWriter 4.0 Vorlage/Template',
'StarWriter 4.0/GlobalDocument',
'StarWriter 5.0',
'StarWriter 5.0 (StarWriter/GlobalDocument)',
'StarWriter 5.0 (StarWriter/Web)',
'StarWriter 5.0 Vorlage/Template',
'StarWriter 5.0/GlobalDocument',
'StarWriter DOS',
'StarWriter/Web 4.0 Vorlage/Template',
'StarWriter/Web 5.0 Vorlage/Template',
'TGA - Truevision TARGA',
'TIF - Tag Image File',
'Text',
'Text (StarWriter/Web)',
'Text (encoded)',
'Text (encoded) (StarWriter/GlobalDocument)',
'Text (encoded) (StarWriter/Web)',
'Text - txt - csv (StarCalc)',
'WMF - MS Windows Metafile',
'XBM - X-Consortium',
'XHTML File',
'XPM',
'bmp_Export',
'bmp_Import',
'calc_HTML_WebQuery',
'calc_StarOffice_XML_Calc_Template',
'calc_pdf_Export',
'dBase',
'draw_PCD_Photo_CD_Base',
'draw_PCD_Photo_CD_Base16',
'draw_PCD_Photo_CD_Base4',
'draw_StarOffice_XML_Draw_Template',
'draw_bmp_Export',
'draw_emf_Export',
'draw_eps_Export',
'draw_flash_Export',
'draw_gif_Export',
'draw_html_Export',
'draw_jpg_Export',
'draw_met_Export',
'draw_pbm_Export',
'draw_pct_Export',
'draw_pdf_Export',
'draw_pgm_Export',
'draw_png_Export',
'draw_ppm_Export',
'draw_ras_Export',
'draw_svg_Export',
'draw_svm_Export',
'draw_tif_Export',
'draw_wmf_Export',
'draw_xpm_Export',
'dxf_Import',
'emf_Export',
'emf_Import',
'eps_Export',
'eps_Import',
'gif_Export',
'gif_Import',
'impress_StarOffice_XML_Draw',
'impress_StarOffice_XML_Impress_Template',
'impress_bmp_Export',
'impress_emf_Export',
'impress_eps_Export',
'impress_flash_Export',
'impress_gif_Export',
'impress_html_Export',
'impress_jpg_Export',
'impress_met_Export',
'impress_pbm_Export',
'impress_pct_Export',
'impress_pdf_Export',
'impress_pgm_Export',
'impress_png_Export',
'impress_ppm_Export',
'impress_ras_Export',
'impress_svg_Export',
'impress_svm_Export',
'impress_tif_Export',
'impress_wmf_Export',
'impress_xpm_Export',
'jpg_Export',
'jpg_Import',
'math_pdf_Export',
'met_Export',
'met_Import',
'pbm_Export',
'pbm_Import',
'pcd_Import_Base',
'pcd_Import_Base16',
'pcd_Import_Base4',
'pct_Export',
'pct_Import',
'pcx_Import',
'pgm_Export',
'pgm_Import',
'placeware_Export',
'png_Export',
'png_Import',
'ppm_Export',
'ppm_Import',
'psd_Import',
'ras_Export',
'ras_Import',
'sgf_Import',
'sgv_Import',
'svg_Export',
'svm_Export',
'svm_Import',
'tga_Import',
'tif_Export',
'tif_Import',
'wmf_Export',
'wmf_Import',
'writer_StarOffice_XML_Writer_Template',
'writer_globaldocument_StarOffice_XML_Writer',
'writer_globaldocument_StarOffice_XML_Writer_GlobalDocument',
'writer_globaldocument_pdf_Export',
'writer_pdf_Export',
'writer_web_HTML_help',
'writer_web_StarOffice_XML_Writer',
'writer_web_StarOffice_XML_Writer_Web_Template',
'writer_web_pdf_Export',
'xbm_Import',
'xpm_Export',
'xpm_Import'

【讨论】:

你有没有让这个工作?我正在尝试类似的东西,并尝试使用其中几个的 FilterName,但每次调用 StoreToUrl 或 StoreAtUrl 时都会引发 ErrorCodeIOException。

以上是关于JAVA使用OpenOffice文件转换的主要内容,如果未能解决你的问题,请参考以下文章

JAVA使用OpenOffice文件转换

java 调用OpenOffice将word格式文件转换为pdf格式

java实现附件预览(openoffice+swftools+flexpaper)

使用 jodconverter 和 OpenOffice 将 doc/docx 转换为 pdf

Java实现word文档在线预览,读取office文件

java 使用openoffice 转换文档,成.pdf,实现在线预览效果