由于*.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
更多可能性。
CTProperty
参见http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/officeDocument/x2006/customProperties/CTProperty.java#CTProperty。_