Search code examples
javametadataapache-tikafileinfo

Missing many metadata Key-value pairs with Apache Tika


I am trying to get metadata of a file in JAVA using Apache Tika.The code for getting that is given below,

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;

import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.sax.BodyContentHandler;

import org.xml.sax.SAXException;

public class tika {

public static void main(final String[] args) throws IOException, SAXException, TikaException {
      File file = new File("C:\\HTML\\viewer\\files\\doc.doc");

      AutoDetectParser parser = new AutoDetectParser();
      BodyContentHandler handler = new BodyContentHandler();
      Metadata metadata = new Metadata();
      FileInputStream inputstream = new FileInputStream(file);
      ParseContext context = new ParseContext();
      parser.parse(inputstream, handler, metadata, context);

      String[] metadataNames = metadata.names();
      System.out.println(metadataNames.length);
        for (String key : metadataNames) {
            String value = metadata.get(key);
            System.out.println(key + ": " + value);
        }
      
   }
}

The problem is I am getting only two key-value pairs which are given below,

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2
X-TIKA:Parsed-By: org.apache.tika.parser.EmptyParser
Content-Type: application/x-tika-msoffice

I am getting a warning from SLF4J which I think is not a problem for getting the desired output and If so please tell me.

Its a maven project and the dependencies I have are given below,

<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-core</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>org.apache.tika</groupId>
  <artifactId>tika-parsers</artifactId>
  <version>2.3.0</version>
  <type>pom</type>
</dependency>

I am getting only the 'X-TIKA:Parsed-By' and 'Content-Type' key-values. I want more metadata values like the author, comments, size, etc.


Solution

  • You have to use a specific parser for retrieving the Microsoft Document properties.

    First add the following dependencies:

    <dependency>
      <groupId>org.apache.tika</groupId>
      <artifactId>tika-core</artifactId>
      <version>2.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tika</groupId>
      <artifactId>tika-parsers-standard-package</artifactId>
      <version>2.3.0</version>
    </dependency>
    

    Next, you can do something like this:

    BodyContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    FileInputStream inputstream = new FileInputStream(new File("C:\\HTML\\viewer\\files\\doc.doc"));
    ParseContext pcontext = new ParseContext();
    
    // Specific parser
    OOXMLParser msOfficeParser = new OOXMLParser();
    msOfficeParser .parse(inputstream, handler, metadata, pcontext);
    
    System.out.println("Contents of the document:" + handler.toString());
    System.out.println("Metadata of the document:");
    
    String[] metadataNames = metadata.names();
    
    for (String name : metadataNames) {
      System.out.println(name + ": " + metadata.get(name));
    }
    

    You will obtain something like this:

    Metadata of the document: cp:revision: 3
    extended-properties:AppVersion: 16.0000 meta:paragraph-count: 1
    meta:word-count: 83 extended-properties:Application: Microsoft Office
    Word meta:last-author: Microsoft Office User

    References: