1
votes

I have looked in to doing text extraction / data extraction from PDFs and used some other stack overflow answers for help, what I have set up is Tika parsing a pdf through a custom ContentHandlerDecorator which is using sax events to parse the content. I am having an issue though where the pdf when viewed in a PDF viewer contains a table of data that is arranged in rows and columns, but that information seems to just not begin pulled from the pdf, or im not seeing how to find it using tika / sax.

This is an example of what I'm seeing:

enter image description here

A table like the above gives me this (when i just to string the Sax events)

START http://www.w3.org/1999/xhtml, div, , div, class = page; 
START http://www.w3.org/1999/xhtml, p, , p, 
END http://www.w3.org/1999/xhtml, p, , p
WHITESPACE[
], 0, 1

START http://www.w3.org/1999/xhtml, p, , p, 
CHARS [I, n, f], 0, 3
WHITESPACE[
], 0, 1
CHARS [o], 0, 1
WHITESPACE[
], 0, 1
END http://www.w3.org/1999/xhtml, p, , p

START http://www.w3.org/1999/xhtml, p, , p, 
CHARS [I, n, f, o], 0, 4
CHARS [ ], 0, 1
CHARS [H, 1], 0, 2
CHARS [ ], 0, 1
CHARS [H, 2], 0, 2
CHARS [ ], 0, 1
CHARS [H, 3], 0, 2
CHARS [ ], 0, 1
CHARS [H, 4], 0, 2
WHITESPACE[
], 0, 1
END http://www.w3.org/1999/xhtml, p, , p

// Start of Second Row
START http://www.w3.org/1999/xhtml, p, , p, 
CHARS [1], 0, 1
CHARS [I, n, f, 1], 0, 4
WHITESPACE[
], 0, 1
CHARS [I, n, f, 2], 0, 4
WHITESPACE[
], 0, 1
END http://www.w3.org/1999/xhtml, p, , p

START http://www.w3.org/1999/xhtml, p, , p, 
CHARS [I, n, f, 3], 0, 4
WHITESPACE[
], 0, 1
END http://www.w3.org/1999/xhtml, p, , p

START http://www.w3.org/1999/xhtml, p, , p, 
CHARS [D, 1], 0, 2
CHARS [ ], 0, 1 
CHARS [D, 4], 0, 2
END http://www.w3.org/1999/xhtml, p, , p

Due to the seemingly random ending of paragraphs, no indication of when a new row is started, and in the case of the second row, there's somehow no gaps in the data for the empty columns despite it being displayed like that in the viewer.

I'm sorry I can't provide the pdf, but i am after any information on where this extra formatting / layout data is stored or can be extracted, because there is clearly missing info in what I am getting from Tika. There aten't any attributes in any of the paragraphs.

If it helps, I know the PDF was generated using iText as I can see that in the metadata, but when trialling iText it didn't seem like it offered a way to programmatically open and parse a pdf without providing it some sort of pre made extraction template, which i don't want to do.

1

1 Answers

3
votes

That SAX information is all but useless in getting the information you want. IIRC, iText has a text extraction API, and you need the position information it will provide in order to determine in which column a given bit of text resides.

Have a look at iText's SimpleTextExtractionStrategy. You'll need to build something similar that pays attention to eventOccurred(), looking for EventType.RENDER_TEXT. When you get a text rendering event, you need to look at the IEventData param cast to a TextRenderInfo, and figure out where on the page (and in the table) it is, if it's in the table at all, etc.

From the position information, you'll have to deduce which column each piece of text is from. Do not rely on the information appearing in logical order. Do not rely on text for a cell to be in a single render event.

NOTE: I know next-to-nothing about pdfbox, sax, or apache-tika, so there may well be a less "roll your own" way of doing this. You may be able to tweak Tika's output for example to give positioning information (absolute or relative). This information will probably come from something very much like SimpleTextExtractionStrategy.

Good hunting.