广告
返回顶部
首页 > 资讯 > 精选 >如何在java中使用openoffice将office文档转换为PDF
  • 744
分享到

如何在java中使用openoffice将office文档转换为PDF

javaopenofficeoffice 2023-05-30 20:05:42 744人浏览 薄情痞子
摘要

如何在java中使用openoffice将office文档转换为pdf?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Java的特点有哪些Java的特点有哪些1.

如何在java中使用openoffice将office文档转换为pdf?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Java的特点有哪些

Java的特点有哪些1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。2.Java具有简单性、面向对象、分布式安全性、平台独立与可移植性、动态性等特点。3.使用Java可以编写桌面应用程序、WEB应用程序、分布式系统和嵌入式系统应用程序等。

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>

直接上转换代码,需要监听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失败");   } }

需注意: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 documentFormats = new ArrayList();    public void aDDDocumentFormat(DocumentFormat documentFormat) {     documentFormats.add(documentFormat);   }    protected List getDocumentFormats() {     return documentFormats;   }       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;   public class BasicDocumentFormatRegistry implements DocumentFormatRegistry {    private List documentFormats = new ArrayList();    public void addDocumentFormat(DocumentFormat documentFormat) {     documentFormats.add(documentFormat);   }    protected List getDocumentFormats() {     return documentFormats;   }       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;   } }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网精选频道,感谢您对编程网的支持。

--结束END--

本文标题: 如何在java中使用openoffice将office文档转换为PDF

本文链接: https://www.lsjlt.com/news/220776.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何在java中使用openoffice将office文档转换为PDF
    如何在java中使用openoffice将office文档转换为PDF?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Java的特点有哪些Java的特点有哪些1....
    99+
    2023-05-30
    java openoffice office
  • 使用JAVA怎么将PDF转换为HTML文档
    使用JAVA怎么将PDF转换为HTML文档?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。引入Maven依赖<!-- https://mvnrepositor...
    99+
    2023-06-15
  • 如何在iPhone上将Word文档转换为PDF
    无论是银行对账单还是求职简历,在某些场景下,您都需要在线提交 PDF 格式的文件。大多数文档仍然以Word格式在iPhone上存储的大部分时间。但是iPhone上没有专用的默认应用程序可以直接将Word文档转换为pdf格式,安装任何不安全的...
    99+
    2023-07-12
  • 使用Python将Word文档转换为PDF的方法
    摘要: 文介绍了如何使用Python编程语言将Word文档转换为PDF格式的方法。我们将使用python-docx和pywin32库来实现这个功能,这些库提供了与Microsoft Word应用程序的交互能力。 正文: 在现实生活和工作中,...
    99+
    2023-10-03
    python word2pdf python-docx pywin32
  • JAVA中如何使用openoffice将Excel转PDF再转图片功能
    这篇文章主要为大家展示了“JAVA中如何使用openoffice将Excel转PDF再转图片功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JAVA中如何使用openoffice将Excel转...
    99+
    2023-06-22
  • Java如何将HTML文件转换为PDF文件
    随着互联网时代的到来,网页越来越成为人们获取信息的主要渠道。但是,网页上的信息无法离线保存,有时用户需要在没有网络连接的情况下查看网页内容。这时,转换网页为PDF文件就成为了不错的选择。在众多的软件中,Java有着较为强大的PDF生成能力,...
    99+
    2023-05-14
  • 如何利用python将pdf文档转为word?
    1.前言 有些时候,我们需要将pdf文档转换为word文档进行处理,但市面上的一些pdf软件往往需要付费才能使用。那么作为一名技术人员,如何才能实现pdf转word自由? 2.准备工作 提前安装好py...
    99+
    2023-09-05
    word python pdf
  • 如何使用Java语言将XML转为PDF
    这篇文章主要介绍了如何使用Java语言将XML转为PDF的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何使用Java语言将XML转为PDF文章都会有所收获,下面我们一起来看看吧。可扩展标记语言(XML)文件是...
    99+
    2023-06-29
  • 使用java如何将文字转换为五笔
    使用java如何将文字转换为五笔?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。具体如下:package com.core.utils;public class ...
    99+
    2023-05-31
    java ava
  • 如何在 Java 中将 String 转换为 int?
    问: 如何将 String 转换为 int? "1234" → 1234 答1: huntsbot.com – 高效赚钱,自由工作 String myString = "1234";int ...
    99+
    2023-10-24
    java 开发语言 jvm rxjava boosting
  • 在Java中如何将double转换为int
    小编给大家分享一下在Java中如何将double转换为int,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!在Java编程中,您将有一个double原语值(例如82...
    99+
    2023-06-14
  • 如何在 Java 中将多个 PDF 文件合并为一个 PDF
    如果您正在开发涉及处理 PDF 文件的 Java 项目,则可能需要将多个 PDF 文件合并到一个文档中。在本文中,我们将演示如何使用 Java 编程语言来实现这一目标。我们将从以下两个方面向您展示如何将多个PDF文件合并为一个PDF: 将文...
    99+
    2023-08-18
    java pdf python
  • 在Java项目中如何将对象转换为String
    在Java项目中如何将对象转换为String?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Java中对象转换为String的常用方法:  方法一:String&...
    99+
    2023-05-31
    java string ava
  • 如何使用vbs实现将文件转换为vbs语句
    这篇文章主要介绍如何使用vbs实现将文件转换为vbs语句,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!这个不是exe2vbs 所有类型的文件都可以转化的 不过限于string的大小 文件...
    99+
    2023-06-08
  • 在java项目中如何实现将字符串转换为整数
    这篇文章给大家介绍在java项目中如何实现将字符串转换为整数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。java中字符串转整数       该题虽然和我们正...
    99+
    2023-05-31
    java 字符串 整数
  • 如何使用JavaScript将普通字符串转换为HTML文本
    二、JavaScript中的字符串转HTML在JavaScript中,可以使用正则表达式和字符串替换来将字符串转换为HTML格式。方法一:使用正则表达式以下是将字符串转换为HTML格式的JavaScript代码,使用了正则表达式和字符串替换...
    99+
    2023-05-14
  • 如何使用vbs将HTML或txt文件转换为ASP输出
    这篇文章将为大家详细讲解有关如何使用vbs将HTML或txt文件转换为ASP输出,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。使用方法:手工修改html文件的名称与想要生成的asp的文件名称,然后将下面的...
    99+
    2023-06-08
  • 在java项目中如何将 json字符串转换为JSONObject与JSONArray格式
    在java项目中如何将 json字符串转换为JSONObject与JSONArray格式?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。java json字符串转...
    99+
    2023-05-31
    java jsonobject jsonarray
  • 将.ipynb格式的文件转换为.py格式的文件从而在pycharm中运行使用
    将xxx.ipynb 文件转换为 xxx.py 文件的两种方式 方法一:通过终端指令进行转换 如果你想将 xxx.ipynb 文件转换为 xxx.py 文件,打开终端,在xxx.ipynb文件所在目录...
    99+
    2023-09-29
    pycharm python ide
  • 如何使用golang中的strconv.ParseInt函数将字符串转换为整数
    如何使用golang中的strconv.ParseInt函数将字符串转换为整数,需要具体代码示例在golang中,strconv包提供了一系列将字符串转换为其他类型的函数。其中,strconv.ParseInt函数用于将字符串转换为整数类型...
    99+
    2023-11-18
    Golang strconv ParseInt
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作