3

I want to use Apache POI to convert files from *.fidus (Fidus Writer) platform to *.docx format and vice versa.

In *.fidus files there are some properties that I need to store them as extended or customised properties in *.docx file, then I can retrieve them when I want to convert it back to *.fidus.

Thus I like to know how can I use the class CustomProperties of POI or something like this to be add some properties to docx file. Also is it possible to add custom properties (extended properties) to a paragraph in docx files by using POI?

Thanks in advance.

centic
  • 15,565
  • 9
  • 68
  • 125
MJBZA
  • 4,796
  • 8
  • 48
  • 97

1 Answers1

3

Since *.docx documents are XML based, we must use POIXMLProperties.CustomProperties, see http://poi.apache.org/apidocs/org/apache/poi/POIXMLProperties.CustomProperties.html.

Example:

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);
 }
}

The POIXMLProperties.CustomProperties.addProperty() can only set boolean, double, integer or string properties but the underlying CTProperty contains more possibilities.

For CTProperty see http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/officeDocument/x2006/customProperties/CTProperty.java#CTProperty.

Axel Richter
  • 56,077
  • 6
  • 60
  • 87