2

i'am trying to get the summary information from file with JAVA and I can't found anything. I tried with org.apache.poi.hpsf.* .

I need Author, Subject, Comments, Keywords and Title.

       File rep = new File("C:\\Cry_ReportERP006.rpt");


        /* Read a test document <em>doc</em> into a POI filesystem. */
        final POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(rep));
        final DirectoryEntry dir = poifs.getRoot();
        DocumentEntry dsiEntry = null;
        try
        {
            dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
        }
        catch (FileNotFoundException ex)
        {
            /*
             * A missing document summary information stream is not an error
             * and therefore silently ignored here.
             */
        }

        /*
         * If there is a document summry information stream, read it from
         * the POI filesystem.
         */
        if (dsiEntry != null)
        {
            final DocumentInputStream dis = new DocumentInputStream(dsiEntry);
            final PropertySet ps = new PropertySet(dis);
            final DocumentSummaryInformation dsi = new DocumentSummaryInformation(ps);
            final SummaryInformation si = new SummaryInformation(ps);


            /* Execute the get... methods. */
            System.out.println(si.getAuthor());
Alfabravo
  • 7,493
  • 6
  • 46
  • 82
backLF
  • 135
  • 2
  • 3
  • 12

3 Answers3

2

As explained in the POI overview at http://poi.apache.org/overview.html there are more types of file parsers. The following examples extract the Author/Creator from 2003 office files:

public static String parseOLE2FileAuthor(File file) {
String author=null;
try {

    FileInputStream stream = new FileInputStream(file);
    POIFSFileSystem poifs = new POIFSFileSystem(stream);
    DirectoryEntry dir = poifs.getRoot();
    DocumentEntry siEntry =      (DocumentEntry)dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    DocumentInputStream dis = new DocumentInputStream(siEntry);
    PropertySet ps = new PropertySet(dis);
    SummaryInformation si = new SummaryInformation(ps);

    author=si.getAuthor();
    stream.close();

} catch (IOException ex) {
    ex.getStackTrace();
} catch (NoPropertySetStreamException ex) {
    ex.getStackTrace();
} catch (MarkUnsupportedException ex) {
    ex.getStackTrace();
} catch (UnexpectedPropertySetTypeException ex) {
    ex.getStackTrace();
}
return author;

}

For docx,pptx,xlsx the POI has specialized classes. Example for .docx file:

public static String parseDOCX(File file){
String author=null;
FileInputStream stream;
try {
    stream = new FileInputStream(file);
    XWPFDocument docx = new XWPFDocument(stream);
    CoreProperties props = docx.getProperties().getCoreProperties();
    author=props.getCreator();
    stream.close();
} catch (FileNotFoundException ex) {
   ex.printStackTrace();
} catch (IOException ex) {
    ex.printStackTrace();
}


return author;
}

Use for PPTX use XMLSlideShow or XMLWorkbook instead of XMLDocument.

1

Please find the sample code here- Appache POI how to

In brief, you can a listener MyPOIFSReaderListener:

    SummaryInformation si = (SummaryInformation)
             PropertySetFactory.create(event.getStream());
    String title = si.getTitle();
    String Author= si.getLastAuthor();
    ......

and register it as :

    POIFSReader r = new POIFSReader();
    r.registerListener(new MyPOIFSReaderListener(),
                   "\005SummaryInformation");
    r.read(new FileInputStream(filename));
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • Thanks, I'll read these. I have also read this http://stackoverflow.com/questions/4096973/reading-windows-file-summary-properties-title-subject-author-in-java , but I didn't understand. – backLF Oct 12 '12 at 14:32
  • Good. If this is helpful, please don't forget to accept the answer. – Yogendra Singh Oct 12 '12 at 14:38
0

for 2003 office files, you can use classes inherited from POIDocument. here is an example for doc file:

FileInputStream in = new FileInputStream(file);
HWPFDocument doc = new HWPFDocument(in);
author = doc.getSummaryInformation().getAuthor();

and HSLFSlideShowImpl for ppt,
HSSFWorkbook for xls,
HDGFDiagram for vsd.

there are many other file information within the SummaryInformation class.

for 2007 or above office file, see the answer of @Dragos Catalin Trieanu