If you scan a page with text into a PDF and run an OCR application on it, then the text will be added to the page, but the "text rendering mode" is set to invisible. It's there, but it's not rendered on screen (or on paper if printed). What you see or print is the original scanned image.
How can we make the invisible text visible?
Well, we can edit the PDF... The PDF code to set text rendering to invisible is this:
3 Tr
You cannot find this string (yet) in the original from_abbyy.pdf nor in from_ghostscript.pdf because parts of the PDFs are compressed. So we uncompress them as far as possible with the help of qpdf
:
qpdf \
--qdf \
from_abbyy.pdf \
qdf--from_abbyy.pdf
qpdf \
--qdf \
after_ghostscript.pdf \
qdf--after_ghostscript.pdf
Now we can find above string easily (and there is only one occurrence in each file).
Let's switch this to one of the visible modes of text rendering. Overall, we can choose amongst these 8 text rendering modes:
0 - fill glyph shapes
1 - stroke glyph shapes
2 - fill, then stroke glyph shapes
3 - neither fill nor stroke glyph shapes (invisible)
4 - fill and add to path for clipping glyph shapes
5 - stroke glyph shapes and add to path for clipping
6 - fill, then stroke glyph shapes and add path for clipping
7 - add glyph shapes to path for clipping
If I use the "fill" mode, the text from the OCR will probably look not so good on top of the underlying scan image. Therefore I prefer the "stroke" variant. So I simply change above line to read
1 Tr
Looking at this modified PDF, I don't like it, because the default linewidth is too thick for my taste. Also, the color of the outline stroke is black (default); I'd prefer red so to have a contrast to the originally scanned shapes. Therefore I add some code to the front of this line which sets the linewidth to a quarter of a point:
.25 w
and some other to set the stroke color to red:
1 0 0 RG
The complete line now is reads:
.25 w 1 0 0 RG 1 Tr
That's all.
Note, that our little manipulation has damaged the file, because its "TOC" (in technical terms: its xref
table) will now no longer be valid. Acrobat Reader or Acrobat Professional will nevertheless still open it (without complaining even) and silently "repair" the xref section of the file. Other PDF viewers may reject the file, but for now we don't care...
Here are screenshots of the result:
(First screenshot is zoomed to window width.)
(Second screenshot is zoomed to 800%.)
The red outlines is the scanned text made visible now, just as we wanted it.
I conducted the same procedure as outlined above for both files from_abbyy.pdf and after_ghostscript.pdf. I opened both results in 2 different instances of Acrobat Reader. If we make them both zoom to the same value and maximize both windows, then it is easy to toggle the view between both files via [alt]+[tab]
. This is a good way to reveal even the finest rendering differences between two PDF files.
My result is: there is not even a single pixel different between Ghostscript's (v9.02) input and its output for this file. But there is quite a difference if you want to copy'n'paste text...