I'm using ABCpdf to extract the text content of some PDF files, in particular by calling Doc.GetText("Text"). (You call it in a loop, once per page.) This usually works well, but for some PDF files the resulting text consists of text with a dearth of space characters, e.g.
Thissentencedoesn'thaveanyspacesbetweenwords.
What's interesting is if I try to extract text from the exact same PDFs using Apache Tika (powered under the hood by PDFBox), I tend to get all the spaces I'd expect between words. That is, the above sentence would be rendered by Tika as
This sentence doesn't have any spaces between words.
Overall, the two tools act like they're afraid of committing different mistakes -- ABCpdf acts like the worst thing in the world would be to insert a space where one doesn't belong, while Tika acts like the worst thing in the world would be to fail to insert a space where one does belong.
Are there any settings to make ABCpdf act more like Tika in this regard?
Short Answer: You can get individual tokens of text via
Doc.GetText("SVG")
, parsing the XML forTEXT
andTSPAN
elements, and determining if there is layout spacing that should be treated as actual spaces. The behavior you're seeing from PDFBox is probably their attempt to make that assumption. Also, even Adobe Acrobat can return spaced text via the clipboard as PDFBox does.Long Answer: This may cause more problems, as this may not be the original intent of the text in the PDF.
ABCpdf is doing the correct thing here, as the PDF spec only describes where things should be placed in the output medium. One can construct a PDF file that ABCpdf interprets in both styles, even though the original sentence looks nearly the same.
To demonstrate this, here is a snapshot of a document from Adobe InDesign that shows a text layout matching both cases for your sample sentence.
Note that the first row was not constructed with actual spaces, instead, the words were placed by hand in individual text regions and lined up to look approximately like a properly spaced sentence. The second row has a single sentence that has actual text spaces between the words, in a single text region.
When exported to PDF and then read in by ABCpdf,
Doc.GetText("TEXT")
will return the following:Thus if you wish to detect layout spaces, you must use SVG output and step through the tokens of text manually.
Doc.GetText("SVG")
returns text and other drawing entities as ABCpdf sees them on the page, and you can decide how you want to handle the case of layout based spacing.You'll receive output similar to this:
And note that the basic structure reveals the original intent that gave you problems. (xml:space and attributes removed, whitespace modifications for the sake of example)
This question and answer are based around old releases of ABCpdf.
ABCpdf Version 9 will do this all automatically for you.
I work on the ABCpdf .NET software component so my replies may feature concepts based around ABCpdf. It's just what I know. :-)