New Tool Radar

AI PDF

How to Rebuild a Bad Text Layer With OCRmyPDF Force-OCR and Check the New Text

Re-run OCR on every page of a PDF with a bad text layer, then check the new text against the pages.

Schematic with three steps: Confirm the text layer is bad; Run OCRmyPDF with force-ocr; Check the rebuilt text.

Some PDFs have a text layer that is worse than useless: garbled characters, words in the wrong order, or text from a different document. The –force-ocr option rasterises every page and OCRs it again, replacing the bad layer with a fresh one built from the visible pages.

What you need

  • OCRmyPDF and Tesseract installed
  • A PDF whose text layer is garbled or wrong
  • A copy of the original file, kept unchanged

Step 1: Confirm the text layer is bad

Copy a paragraph from the PDF and read it. If the copied text is garbled or in the wrong order while the page looks fine, the text layer needs rebuilding.

Step 2: Run OCRmyPDF with force-ocr

Run ocrmypdf with the language code, an ordinary PDF output type and –force-ocr, for example ocrmypdf -l eng –output-type pdf –force-ocr bad.pdf fixed.pdf.

Step 3: Check the rebuilt text

Copy the same paragraph from the output and compare it with the page image. Check names, numbers and the reading order before using the new text layer.

Worked example: synthetic force-OCR check on a garbled PDF

The table below is a synthetic example. It shows the expected result of rebuilding a text layer; no OCR was run.

Check Before force-OCR After force-OCR
Copied paragraph readable No Yes
Names correct 2 of 6 6 of 6
Numbers correct 1 of 4 4 of 4
Reading order correct No Yes

Expected result (UNTESTED). after –force-ocr the copied paragraph reads correctly, all 6 names and all 4 numbers match the page, and the reading order is right. Use the rebuilt file only after this check. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • –force-ocr rasterises and OCRs every page, so it takes longer than a normal run.
  • A fresh OCR layer can still contain errors; inspect the output before relying on it.
  • Keep the original file: the rebuild replaces the text layer, and the old layer cannot be recovered from the new file.

Sources

See all