Overview

I had the opportunity to create a transparent text PDF from a PDF using Google Cloud Vision API, so this is a personal note for future reference.

Below is an example of searching for simple.

Background

This time, we target PDFs consisting of a single page.

Procedure

Creating the Image

Create an image to be used as the OCR target.

With the default settings, the resulting image was blurry, so I set the resolution to 2x and performed position alignment considering the resolution in the process described below.

Install the following packages.

PPyiMlulPoDwF
ififi#io#pp##z#mpiimrmrmnudaoaimmpopopptfgpotxggomomoup_eim.rrrPtuPdx===stPtttD_tDo==aIqFp_Fc=fpIvfLjdidpup2iameismofdmdp.tga(tio_fefa0zeg"zmnip_n_g..e.pmaptdeMg.optao.aei#roht=cgttpmtrPhuer_eaPtD=fmtipngyIF=ie_xi(eMmt"tnp(xi.uaq."ztizmopPgd/..[Oxoa.nDemi/o0CmopBgFnop]Ram(y"puep,mt)utn(aetp(#)zts.uiorIptnoiOd.p#mx(fpu)=p"dtmif_ax"pt.#d3)tf0o_0bpyaDttPehIs)(P"DpFng")))

Google Cloud Vision API

Apply the Google Cloud Vision API to the output image.

{"text{}{}{}A,,,nno"}"""}""}"tb,dlb,db,daoeooeoetuscususin"]can"]cn"]codvrldvrdvrnieieieiieisnrp"nrpnrp"gt{}{}{}{}t:gt{}{}{}{}tgt{}{}{}{}t:Pi,,,iPi,,,iPi,,,ioco"ocooco[lenllenlenys"""""""""ays"""""""""ys"""""""""""xyxyxyxy:"""xyxyxyxy:""xyxyxyxy:::""""""""::""""""""::""""""""::::::::"::::::::"::::::::"{[S{[S{[P11111111a11313212a31515232D45050444m45656141m85050181F12828111p19364629p26647335",220,0l,,,,l,,,,,,ee"PDF...",

Save the output JSON file with a name such as ./google_ocr.json.

Then, retrieve the OCR results as follows.

j#wtsieoJtxnSht_OrspNoeaps=tephnor(ne=jsssep"Ooo.Cn=n/R_sgpjeoas[oto"ghntl,.eelx_"otoraAc"dnr)(n.foja)tssaotnfi":ons"]

Creating the Transparent Text

Apply the results to the PDF using the following script. The key point is that it was necessary to “adjust the font size to check if it fits.”

#r#i#ppefddcOfftCt__Refdd=xoootrPccpsDuua:tvxyxyxyxy#bc#ff#wiFmmgee________booihfeeexrmmmmmmmmonntinn.ttiiaaiiaaxttslnttrinnxxnnxx_eserifop..eicrni=efotrsccne========etzfsnialtsceFortfnvotmmmmxyxyt=an=efbsitesq=iiaa____=ltsirit((ednnxxmmmm=ts調sptezsfo(mt((((iiaae1eia>sae:"u)(e[[[[nnxxfx0zg=k'ttxvvvvitee=-{pet[[[[////t[.0=cux[""""z">i:Tott"xyxyzzzz.dnr1n_sb""""ooooRe0sutp[o]]]]ooooes:eeed1ummmmccr#nf:nfffftrtt_]doooo(i_}p)irrrrxpt'a:n_tetgvvvvmixchPioto)#oiiiinnbulnnnn,"olty]xde"vvvvy(x]eeee_bnt[rrrrmbos"ttttiot[viiiinx0ecccc,_f]reeeeritssssxeti_cciiiimtieffffa,nsx"vvvv,ct]]]]]oh))))yne_tmeranextc),tafnognltes.i"z)e=fontsize,color=(0,0,0,0),render_mode=3,align=1)

Results

We target the PDF available at the following link.

https://pdfobject.com/pdf/sample.pdf

As a result, we were able to create a transparent text PDF as shown below.

Summary

I hope this article serves as a useful reference when OCR is needed for specific pages only.