我想使用Apache POI将文件从* .fidus(Fidus Writer)平台转换为* .docx格式,反之亦然。
在* .fidus文件中,有一些属性需要将它们存储为* .docx文件中的扩展或自定义属性,然后在要将其转换回* .fidus时可以检索它们。
因此,我想知道如何使用POI的CustomProperties类或类似的东西向docx文件添加一些属性。还可以通过使用POI在docx文件中的段落中添加自定义属性(扩展属性)吗?
提前致谢。
由于*.docx
文档是XML
基于文档的,因此必须使用POIXMLProperties.CustomProperties
,请参阅http://poi.apache.org/apidocs/org/apache/poi/POIXMLProperties.CustomProperties.html。
例子:
import java.io.*;
import org.apache.poi.*;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
import java.util.GregorianCalendar;
public class DocumentProperties {
public static void main(String[] args) throws IOException {
XWPFDocument document = new XWPFDocument(new FileInputStream("This is a Test.docx"));
POIXMLProperties properties = document.getProperties();
//http://poi.apache.org/apidocs/org/apache/poi/POIXMLProperties.html
//prints the core property Creator:
System.out.println(properties.getCoreProperties().getCreator());
//prints the extendend property Application:
System.out.println(properties.getExtendedProperties().getApplication());
//sets a custom property
POIXMLProperties.CustomProperties customproperties = properties.getCustomProperties();
if (!customproperties.contains("Test")) {
customproperties.addProperty("Test", 123);
}
CTProperty ctproperty = customproperties.getProperty("Test");
System.out.println(ctproperty);
System.out.println(ctproperty.getI4());
//the above customproperties.addProperty() can only set boolean, double, integer or string properties
//the CTProperty contains more possibitities
if (!customproperties.contains("Test Date")) {
customproperties.addProperty("Test Date", 0);
ctproperty = customproperties.getProperty("Test Date");
ctproperty.unsetI4();
ctproperty.setFiletime(new GregorianCalendar(2016,1,13));
}
ctproperty = customproperties.getProperty("Test Date");
System.out.println(ctproperty);
System.out.println(ctproperty.getFiletime());
FileOutputStream out = new FileOutputStream(new File("This is a Test.docx"));
document.write(out);
}
}
在POIXMLProperties.CustomProperties.addProperty()
只能设定布尔,双,整数或字符串的属性,但底层CTProperty
包含更多的可能性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句