本文共 2518 字,大约阅读时间需要 8 分钟。
2021SC@SDUSC
目录
上一篇介绍了officePluginManager对office组件的管理,这一篇继续介绍项目中另外一个类OfficePluginExtendFormatRegistry,原作者对该类的描述重写DefaultDocumentFormatRegistry类的原因是因为要添加一些自定义行为,比如字符编码等。
该类的主要方法都依靠jodconverter提供的接口实现,jodconverter是一种Java OpenDocument转换器,能够转换不同格式的文档,它依赖于Apachea OpenOffice或 LibreOffice ,它为OpenDocument和Microsoft Office提供了最好的免费导入/导出的过滤器。它自动支持OpenOffice和LibreOffice提供的所有转换。jodconverter的应用很广泛,在本个项目中,它作为一个Java类库,嵌入到整体应用程序提供服务。
整个类只有一个初始化方法,public OfficePluginExtendFormatRegistry() {},类中为24种不同的文件格式设置了不同的存储参数,以pdf为例就设计了四种不同的storeProperties。接下来以pdf文件形式、html文件形式和txt形式为例分析一些详细的方法。
pdf文件形式设置
针对pdf设置了四次存储属性,分别是TEXT、SPREADSHEET、PRESENTATION、DRAWING。
DocumentFormat pdf = new DocumentFormat("Portable Document Format", "pdf", "application/pdf"); pdf.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "writer_pdf_Export")); pdf.setStoreProperties(DocumentFamily.SPREADSHEET, Collections.singletonMap("FilterName", "calc_pdf_Export")); pdf.setStoreProperties(DocumentFamily.PRESENTATION, Collections.singletonMap("FilterName", "impress_pdf_Export")); pdf.setStoreProperties(DocumentFamily.DRAWING, Collections.singletonMap("FilterName", "draw_pdf_Export")); addFormat(pdf);
html文件形式设置
针对html文件设置了三次存储属性,分别是TEXT、SPREADSHEET和PRESENTATION、
DocumentFormat html = new DocumentFormat("HTML", "html", "text/html"); html.setInputFamily(DocumentFamily.TEXT); html.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "HTML (StarWriter)")); MaphtmlLoadAndStoreProperties = new LinkedHashMap<>(); htmlLoadAndStoreProperties.put("FilterName", "HTML (StarCalc)"); htmlLoadAndStoreProperties.put("FilterOptions", "utf8"); html.setStoreProperties(DocumentFamily.SPREADSHEET, htmlLoadAndStoreProperties); html.setStoreProperties(DocumentFamily.PRESENTATION, Collections.singletonMap("FilterName", "impress_html_Export")); addFormat(html);
txt文件形式设置
FilterOptions,设置为utf8。这里的意思应该是只对UTF-8格式的文本文件进行转换。
DocumentFormat txt = new DocumentFormat("Plain Text", "txt", "text/plain"); txt.setInputFamily(DocumentFamily.TEXT); MaptxtLoadAndStoreProperties = new LinkedHashMap<>(); txtLoadAndStoreProperties.put("FilterName", "Text (encoded)"); txtLoadAndStoreProperties.put("FilterOptions", "utf8"); txt.setLoadProperties(txtLoadAndStoreProperties); txt.setStoreProperties(DocumentFamily.TEXT, txtLoadAndStoreProperties); addFormat(txt);
转载地址:https://blog.csdn.net/m0_55503427/article/details/121846302 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!