Hi so I'm trying to parse some text from some pdfs and I would like to use PoDoFo, now I have tried searching for examples of how to use PoDoFo to parse a pdf however all I can come up with is examples of how to create and write a pdf file which is not what I really need.
If anyone has any tutorial or example of parsing a PDF file with PoDoFo or have suggestions for a different library that I can use please let me know. Also I know there is pdftotext on linux, however, not only can I not use that, but I would much rather be able to do everything I need to internally and not rely on outside programs being installed.
I haven't used PoDoFo, but a quick browse through the class hierarchy on their API webpage reveals:
(API doc link)
So I would just hazard a guess here, that you do:
Then I imagine you navigate the document tree by calling
doc.GetObjects()
and walking through that array (see PdfDocument class)PoDoFo does not provide a means to easily extract text from a document, but it is not hard to do.
Load a document into a
PdfMemDocument
:Iterate over each page:
Iterate over all the PDF commands on that page:
The "process token" & "process var" comments is where it gets a little more complex. You are given raw PDF commands to process. Luckily, if you're not actually rendering the page and all you want is the text, you can ignore most of them. The commands you need to process are:
BT
,ET
,Td
,TD
,Ts
,T
,Tm
,Tf
,"
,'
,Tj
andTJ
The
BT
andET
commands mark the beginning and end of a text stream, so you want to ignore anything that's not between aBT
/ET
pair.The PDF language is RPN based. A command stream consists of values which are pushed onto a stack and commands which pop values off the stack and process them.
The
"
,'
,Tj
andTJ
commands are the only ones which actually generate text."
,'
andTj
return a single string. Usevar.IsString()
andvar.GetString()
to process it.TJ
returns an array of strings. You can extract each one with:The other commands are used to determine when to introduce a line break.
"
and'
also introduce line breaks. Your best bet is to download the PDF spec from Adobe and look up the text processing section. It explains what each command does in more detail.I found it very helpful to write a small program which takes a PDF file and dumps out the command stream for each page.
Note: If all you're doing is extracting raw text with no positioning information, you don't actually need to maintain a stack of
var
values. All the text rendering commands have, at most, one parameter. You can simply assume that the last value invar
contains the parameter for the current command.