0

Can't for the life of me figure out why when I extract text using iTextSharp, some of the text comes in backwards.

using (iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(@"C:\Temp\pdftest\sample.pdf"))
{
     string sText = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, 1, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
}

*Reason I using a LocationTextExtractionStragegy is because I will be using coordinates to pull the text from this position. I've just included a crop of the full PDF for my example. If I use a SimpleTextExtractionStrategy, the "B uy 5 egt 5" and " eerf" don't show up.

Output (from sample code):

B uy 5  egt 5

eerf
4x6 PRINTS    Download free   
CVS Mobile App.   

Promo code O               H m OBILe PICS        
sed items available in all stores We reserve the right to

There's definitely something weird going on the with "eerf". In the pdf, the cursor goes horizontal when you try to select it (Big red FREE).

[

If I use acrobat professional, Advanced -> PDF Optimizer, select Transparency, then save the file, the text is extracted correctly and the "Red Free" is selectable.

So two questions, how can I emulate the PDF Optimizer in iTextSharp? Or, how can iTextSharp read this text correctly?

As you can see this is my first post so don't beat me up too bad.

Additional Test:

I even extended the LocationTextExtractionStrategy and RegionTextRenderFilter so I could return the coordinates of each Textchunk. The weird thing about the "Big Red" Free, is the F's start and end points was the exact same. Same case with the R, and two E's. I would have expected that the end point was equal to the start point + the width of text.

4
  • The short answer to your questions How can I emulate the PDF Optimizer in iTextSharp? and How can iTextSharp read this text correctly? is With a lot of low-level PDF development. The long answer would require at least a few days and that is more than people can usually spend on a single StackOverflow question. I've looked inside the PDF and I see that it's not the original file. It looks like an extract of a bigger PDF and it contains a lot of rubbish that isn't visible to the naked eye. I would label this job as "too much work for the goal you want to achieve." Mar 27, 2015 at 18:01
  • Bruno, thanks for the quick replay. You're correct, this is an extract of a bigger PDF. My main goal is to use the entire PDF and extract the text for each ad block. I posted a small sample to see if you could figure out why the "FREE" and "get" where extracted backwards. Thanks for your insight. Mar 27, 2015 at 18:09
  • When you selected the text in Adobe Reader, did you notice that the REE in FREE weren't real characters? You can't select then, can you? However, when you select the F and copy/paste the text, you get FREE. That's because the letters are there, but not at the same position where you see them. iText sorts the different text snippets based on the position of the baseline. When the baseline of the E is slightly higher than the baseline of the F, iText will say the E precedes F. Adobe Reader sees the same data, but corrects this based on a more elaborate algorithm. Mar 27, 2015 at 18:21
  • I was noticing the F can be selected but the REE couldn't. Thanks for explaining why Adobe Reader displays it correctly and iText reads it how it is. I'll find a way to pre-process/optimize/fix the pdf file before extracting text with itext. Thanks again. Mar 27, 2015 at 18:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.