You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Yury Batrakov <ba...@gmail.com> on 2008/03/03 16:58:42 UTC

Processing word documents recursively

Hi all!

I'm trying to write a code to get a text from the word file and all
it's embedded word files. As I'm newbie in both Java and POI, there
are no any resul :)

Here is my code (modified version of ReadCustomPropertySets.java by
Rainer Klute) that fails with
Exception in thread "main" java.lang.RuntimeException: Unable to
create word extractor from stream "\WordDocument":
java.io.IOException: Invalid header signature; read
295372954022290924, expected -2226271756974174256
	at org.blah.blah.ReadCustomPropertySets$MyPOIFSReaderListener.processPOIFSReaderEvent(ReadCustomPropertySets.java:115)
	at org.apache.poi.poifs.eventfilesystem.POIFSReader.processProperties(POIFSReader.java:261)
	at org.apache.poi.poifs.eventfilesystem.POIFSReader.read(POIFSReader.java:97)
	at ru.mera.nbes.ReadCustomPropertySets.main(ReadCustomPropertySets.java:74)

code:
// ...
public class ReadCustomPropertySets
{
    public static void main(final String[] args)
        throws IOException
    {
        final String filename = args[0];
        POIFSReader r = new POIFSReader();

        r.registerListener(new MyPOIFSReaderListener());
        r.read(new FileInputStream(filename));
    }

    static class MyPOIFSReaderListener implements POIFSReaderListener
    {
        public void processPOIFSReaderEvent(final POIFSReaderEvent event)
        {
            if(event.getName().endsWith("WordDocument"))
            {
                try
                {
                    WordExtractor extractor = new
WordExtractor(event.getStream());
                    System.out.println(extractor.getText());
                }
                catch (Exception ex)
                {
                    //...
                }
            }
        }
    }

I'd also explored POI's code to get POIFSFileSystem from
POIFSReaderEvent to call HWPFDocument(POIFSFileSystem pfilesystem),
but I'd failed in this too.

Thank's in advance!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by Yury Batrakov <ba...@gmail.com>.
done with bugzilla entry:
https://issues.apache.org/bugzilla/show_bug.cgi?id=44524

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by Nick Burch <ni...@torchbox.com>.
On Tue, 4 Mar 2008, Yury Batrakov wrote:
> How do you think, if HWPFDocument or some other class cycled through 
> document content, extracted it's embedded documents and provided an 
> iterator to them, would it fit POI design? Is it ok to submit bugzilla's 
> issue for that?

It might be, but certainly something to make it easier to get at embeded 
documents ought to be

I'd suggest opening a bugzilla entry, and uploading your sample file. If 
you happen to have any other kinds of embeded documents (eg word 
containing excel or something like that), upload that too, so we've got a 
few different examples to work with. Hopefully we'll be able to figure out 
a sensible solution from looking at a few of those

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by Yury Batrakov <ba...@gmail.com>.
> I'd suggest that when you find a WordDocument stream, and then identify
> its matching table entry, you knock up a new POIFSFileSystem for them.
> Give that the two streams, then call WordExtractor on that

Thank you, Nick! I'm pleased to know that my investigation is in right
way, but I also feel a little bit disappointed that there is no
out-of-box solution for my problem :))

How do you think, if HWPFDocument or some other class cycled through
document content, extracted it's embedded documents and provided an
iterator to them, would it fit POI design? Is it ok to submit
bugzilla's issue for that?

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by Yury Batrakov <ba...@gmail.com>.
Looks like the problem is solved and all HWPF attachments are now
being processed ok. Whould I finish all document types, I'll provide
my patches.


On 3/27/08, Yury Batrakov <ba...@gmail.com> wrote:
> Hello Nick and all!
>
>  I've started implementing a feature to process embedded OLE2 documents.
>  I've slightly modified HWPFDocument(POIFSFileSystem pfilesystem) to
>  accept OLE directory name to look all desired streams there. An idea
>  is to open document, get POIFSFileSystem for it, then get  OLE
>  directory that contains embedded doc and feed it to constructor.  I
>  try me changes, but there the following exception have occured when
>  constructor was executed:
>
>  Directory: ObjectPool\_1262435934
>  Exception in thread "main" java.io.IOException: The text piece table
>  is corrupted
>         at org.apache.poi.hwpf.model.ComplexFileTable.<init>(ComplexFileTable.java:53)
>         at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:316)
>         at ru.mera.ofa.ReadOLE1.main(ReadOLE1.java:28)
>
>
>  Please, help me to deal with this!
>
>  My patches for poi-src-3.0.2-FINAL-20080204 attached (poi-3.0.2-FINAL.patch)
>  sample code (ReadOLE1.java) and word file (word2003.doc) are in attachment too.
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by Nick Burch <ni...@torchbox.com>.
On Mon, 14 Apr 2008, Yury Batrakov wrote:
> Oops. I've almost prepared all the patches and already imagined myself
> written down in history of POI, but you finished it before :))

Sorry about that... Someone at apachecon asked me about support for it, 
so I coded it up between two sessions for them!

It's worth checking a recent svn checkout / nightly, to check I did add in 
all the support you had, and your code works nicely with it. If not, do 
please submit patches / bug reports :)

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by Yury Batrakov <ba...@gmail.com>.
Oops. I've almost prepared all the patches and already imagined myself
written down in history of POI, but you finished it before :)) Waiting
for 3.0.3

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by amp834 <am...@rqinc.com>.
I'm looking for the code mentioned below (I have downloaded poi-3.6).  Has it
made it there?  If not, is there a way I can get it?

I would like to save all embedded files (or "attachments") that are
contained in a .doc file.  (Also in .xls and other office files also, if
there are simple uniform ways of doing so).

Also, some binary files have the "ole package" wrapper, I have a hack from
an old post to unwrap them (haven't tested it yet), but if there is a
routine or library for it, it would make it easier!



On Thu, 27 Mar 2008, Yury Batrakov wrote:
> I've started implementing a feature to process embedded OLE2 documents. 
> I've slightly modified HWPFDocument(POIFSFileSystem pfilesystem) to 
> accept OLE directory name to look all desired streams there.

I've put something similar to this into trunk now. It works for 
HSSFWorkbook, HWPFDocument and HSLFSlideShow, and there's some information 
on the site under /poifs/.

I've also written some new unit tests for the handling of embeded 
documents, which all pass, so hopefully you'll be able to use this to read 
your embeded documents without any problems

Nick

-- 
View this message in context: http://old.nabble.com/Processing-word-documents-recursively-tp15806930p27614853.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by Nick Burch <ni...@torchbox.com>.
On Thu, 27 Mar 2008, Yury Batrakov wrote:
> I've started implementing a feature to process embedded OLE2 documents. 
> I've slightly modified HWPFDocument(POIFSFileSystem pfilesystem) to 
> accept OLE directory name to look all desired streams there.

I've put something similar to this into trunk now. It works for 
HSSFWorkbook, HWPFDocument and HSLFSlideShow, and there's some information 
on the site under /poifs/.

I've also written some new unit tests for the handling of embeded 
documents, which all pass, so hopefully you'll be able to use this to read 
your embeded documents without any problems

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Processing word documents recursively

Posted by Yury Batrakov <ba...@gmail.com>.
Hello Nick and all!

I've started implementing a feature to process embedded OLE2 documents.
I've slightly modified HWPFDocument(POIFSFileSystem pfilesystem) to
accept OLE directory name to look all desired streams there. An idea
is to open document, get POIFSFileSystem for it, then get  OLE
directory that contains embedded doc and feed it to constructor.  I
try me changes, but there the following exception have occured when
constructor was executed:

Directory: ObjectPool\_1262435934
Exception in thread "main" java.io.IOException: The text piece table
is corrupted
	at org.apache.poi.hwpf.model.ComplexFileTable.<init>(ComplexFileTable.java:53)
	at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:316)
	at ru.mera.ofa.ReadOLE1.main(ReadOLE1.java:28)


Please, help me to deal with this!

My patches for poi-src-3.0.2-FINAL-20080204 attached (poi-3.0.2-FINAL.patch)
sample code (ReadOLE1.java) and word file (word2003.doc) are in attachment too.

On 3/3/08, Nick Burch <ni...@torchbox.com> wrote:
>  Looking at your code, I think I can see why. You open the POIFSFileSystem,
>  find interesting looking streams, then try to open these. Unfortunately,
>  word files are made up of a few different streams, and the streams don't
>  have the outer poifs header on them.
>
>  I'd suggest that when you find a WordDocument stream, and then identify
>  its matching table entry, you knock up a new POIFSFileSystem for them.
>  Give that the two streams, then call WordExtractor on that
>
>  See the HWPFDocument(POIFSFileSystem pfilesystem) constructor for details
>  of what the streams need to be called
>
>  Nick
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>  For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Processing word documents recursively

Posted by Nick Burch <ni...@torchbox.com>.
On Mon, 3 Mar 2008, Yury Batrakov wrote:
> Exception in thread "main" java.lang.RuntimeException: Unable to
> create word extractor from stream "\WordDocument":
> java.io.IOException: Invalid header signature; read
> 295372954022290924, expected -2226271756974174256

That error means that the stream you passed into POIFSFileSystem wasn't an 
ole2 stream

Looking at your code, I think I can see why. You open the POIFSFileSystem, 
find interesting looking streams, then try to open these. Unfortunately, 
word files are made up of a few different streams, and the streams don't 
have the outer poifs header on them.

I'd suggest that when you find a WordDocument stream, and then identify 
its matching table entry, you knock up a new POIFSFileSystem for them. 
Give that the two streams, then call WordExtractor on that

See the HWPFDocument(POIFSFileSystem pfilesystem) constructor for details 
of what the streams need to be called

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org