Manually add text layer (OCR) over a scanned image

I have a PDF consisting of scanned images of pages from an old printed book. (It has not been OCRed, so is not searchable.) Using the Google Cloud Vision API, one can perform OCR, and what's more interesting, get the position of the (bounding box for) each word. Now, using TeX/LaTeX (with any engine), is there a way to add these words to the PDF at the corresponding positions, i.e. manually add an (invisible) OCR/text layer to the PDF such that it's still the scanned image that's visible but the text can be selected and copied?

(I realize that as we're not using any of LaTeX's structured-document features, nor any of TeX's typesetting features—breaking paragraphs into lines, doing kerning etc—and are manually positioning text that will not even be visible, it may seem that TeX is not really needed for this job. But I don't know any other tool either: there are tools like tesseract that automatically do OCR and add the text, but I want control, to be able to choose what text goes where. There's probably a way to do it from within TeX/XeTeX/LuaTeX.)

• I imagine this question has been asked before, but I couldn't find a duplicate even after much searching. Then again, maybe it hasn't been. Sep 12 '20 at 3:54
• Yes, you should be able to do this with latex. Here's the process: In a loop import 1 pdf page of your file using package includepdf (check option fitpaper), process the contents of json output (for that page) by Google Cloud Vision API to generate textboxes at absolute positions using package textpos, repeat the process page-by-page till you reach the end of imported pdf. You can also add pdf nodes directly from lua using luatex instead of using textpos. Sep 12 '20 at 4:23
• @reportaman The part that's not clear to me is how to add invisible textboxes at absolute positions using textpos (or luatex), that will appear on top of the scanned image. Sep 12 '20 at 4:34
• You can control opacity of text using fontspec (check option Opacity, set it to 0), and textpos (select absolute mode) will get coordinates from json file. Sep 12 '20 at 5:01
• I found this old project of mine: github.com/michal-h21/hocrtex -- I don't know if it still works, as I haven't touch it for nine years, but it was able to create PDF with text layer under image. I think the characters were even at the correct position. Sep 12 '20 at 10:54

You can use the transparent package to make text transparent. Copy&paste should work fine, but finding the text to copy is a bit more difficult ;-) transparent currently works with pdflatex and lualatex, in the next text live it will also work with (x)dvipdfmx.

\documentclass{article}
\usepackage{pdfpages,transparent}
\usepackage{eso-pic}

\AddToShipoutPictureFG{\AtPageCenter{\texttransparent{0}{\Huge This is some text in the center}}}
\begin{document}
\includepdf[pages=1]{example-image-a}
\end{document}


• Thanks! It turns out I anyway needed to use fontspec, so instead of the transparent package I just set the Opacity parameter, like \newfontfamily\devanagarifont[Script=Devanagari,Color=red,Opacity=0]{Chandas} — values between 0 and 1 were useful for debugging too, hence the red colour for invisible text. :-) Sep 12 '20 at 22:26