Wiki

Clone wiki

Aspose Java for Docx4j / Accessing-Document-Properties

Aspose.Words

Document doc = new Document("data/document.doc");

System.out.println("============ Built-in Properties ============");
for (DocumentProperty prop : doc.getBuiltInDocumentProperties())
    System.out.println(MessageFormat.format("{0} : {1}", prop.getName(), prop.getValue()));

System.out.println("============ Custom Properties ============");
for (DocumentProperty prop : doc.getCustomDocumentProperties())
    System.out.println(MessageFormat.format("{0} : {1}", prop.getName(), prop.getValue()));

FileFormatInfo info = FileFormatUtil.detectFileFormat("data/document.doc");
System.out.println("The document format is: " + FileFormatUtil.loadFormatToExtension(info.getLoadFormat()));
System.out.println("Document is encrypted: " + info.isEncrypted());
System.out.println("Document has a digital signature: " + info.hasDigitalSignature());

Docx4j

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));

// Let's look at the core properties
org.docx4j.openpackaging.parts.DocPropsCorePart docPropsCorePart = wordMLPackage.getDocPropsCorePart();
org.docx4j.docProps.core.CoreProperties coreProps = (org.docx4j.docProps.core.CoreProperties)docPropsCorePart.getJaxbElement();

// Title of the document
// Note: Word for Mac 2010 doesn't set title
String title = "Missing";
List<String> list = coreProps.getTitle().getValue().getContent();
if (list.size() > 0) 
{
    title = list.get(0);
}
System.out.println("'dc:title' is " + title);

// Extended properties
org.docx4j.openpackaging.parts.DocPropsExtendedPart docPropsExtendedPart = wordMLPackage.getDocPropsExtendedPart();
org.docx4j.docProps.extended.Properties extendedProps = (org.docx4j.docProps.extended.Properties)docPropsExtendedPart.getJaxbElement(); 

// Document creator Application
System.out.println("'Application' is " + extendedProps.getApplication() + " v." + extendedProps.getAppVersion());

// Custom properties
org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = wordMLPackage.getDocPropsCustomPart();
if(docPropsCustomPart==null)
{
    System.out.println("No Document Custom Properties.");
} 
else 
{
    org.docx4j.docProps.custom.Properties customProps = (org.docx4j.docProps.custom.Properties)docPropsCustomPart.getJaxbElement();

    for (org.docx4j.docProps.custom.Properties.Property prop: customProps.getProperty() ) 
    {
        // At the moment, you need to know what sort of value it has.
        // Could create a generic Object getValue() method.
        if (prop.getLpwstr()!=null) 
        {
            System.out.println(prop.getName() + " = " + prop.getLpwstr());
        } 
        else 
        {
            System.out.println(prop.getName() + ": \n " + XmlUtils.marshaltoString(prop, true, Context.jcDocPropsCustom));
        }
    }
}

Download Source Code

Updated