yob / pdf-reader

The PDF::Reader library implements a PDF parser conforming as much as possible to the PDF specification from Adobe.
MIT License
1.82k stars 271 forks source link

Detecting underlined text #525

Open lluchez opened 1 year ago

lluchez commented 1 year ago

Hi, Thank you so much for creating this awesome gem.

I'm having troubles figuring out a way to detect underlined text. Underlined texts have a different meaning the documents that I'm trying to parse and so I need to be able to detect if a text is underlined.

I've been looking at the PageTextReceiver and the method internal_show_text but I'm seeing anything that will tell me if a character is underlined or not.

I'm gonna continue digging into the library but if anyone has any pointers to share, I would greatly appreciate!

Thank you so much for any help!

Best, Lio

yob commented 1 year ago

I'm not super familiar with underlining.

I suspect there's two common approaches

  1. drawing a line using line/stroke primitives rather than a text command
  2. using annotations

(1) will be hard to detect.

You might have some success with annotations. Annotations are stored in each page outside the content stream. This comment might help you poke around: https://github.com/yob/pdf-reader/issues/179#issuecomment-282454896

lluchez commented 1 year ago

Thank you @yob for your comment.

Unfortunately, I had already tried to parse annotations but nothing is coming back 😞. I'm not too sure how the first option will work.

My original thought was to look at fonts (which we can get access to), but underlines are actually styles, not fonts. I might need to educate myself with the PDF format and how streams/objects are stored/formatted.