Tools,

Remove OCR from a PDF for free, tested

0

OCR words left, images untouched

Get the command

One command, pages untouched

ocrmypdf --mode strip scan.pdf clean.pdf

On our 10-page scan it removed all 2,052 hidden words. Every page image stayed the same, byte for byte.

Use OCRmyPDF 17.14.0 or newer: pip install -U ocrmypdf. Older copies had two strip bugs, fixed in 17.11.0 and 17.14.0.

It removes only text drawn as invisible, which is how most OCR tools write it. The OCRmyPDF cookbook warns that some OCR tools draw visible text and cover it with the image instead.

Six ways, two test files

File one: our 10-page book scan with an OCRmyPDF text layer. File two: a typed page of 44 words, then two of those scan pages.

All six left 0 hidden words. What else each one touched:
Way Typed page Scan pages
ocrmypdf --mode strip Kept Same
Our pikepdf script Kept Same
Ghostscript -dFILTERTEXT Text removed Same
pdfimages + img2pdf Page dropped Now 20 in wide
OCRmyPDF, no OCR engine Made an image JPEG saved again
pdftoppm + img2pdf Made an image JPEG saved again

Same means the same JPEG bytes. Our JPEGs carry no DPI, so img2pdf guessed 96 and blew the pages up.

File sizes
Way Scan file Change
ocrmypdf --mode strip 6.45 MB -33 KB
Our pikepdf script 6.47 MB -11 KB
Ghostscript -dFILTERTEXT 6.49 MB +7 KB
pdfimages + img2pdf 6.49 MB +6 KB
OCRmyPDF, no OCR engine 4.61 MB -1.87 MB
pdftoppm + img2pdf 4.74 MB -1.75 MB

The file with its OCR layer: 6.48 MB. Strip mode also removed the OCR font.

Our pikepdf script (no OCRmyPDF needed)
"""Remove the invisible OCR text from a PDF. Images and vector art stay as they are. Visible text stays too, unless
it shares a text object with invisible text: then text after a removed part can move.

OCR tools write their text in render mode 3 (invisible). This drops every text-show operator drawn in mode 3, on
each page and in each form XObject the page uses. A form XObject is read from mode 0, so text that takes mode 3
from its page stays.

Run: pip install pikepdf, then python3 strip_ocr.py in.pdf out.pdf
"""

import sys

import pikepdf

SHOW = {"Tj", "TJ", "'", '"'}


def strip(owner):
    """New content for one page or form XObject, and how many invisible text runs it dropped."""
    out, mode, saved, dropped = [], 0, [], 0
    for operands, op in pikepdf.parse_content_stream(owner):
        o = str(op)
        if o == "q":
            saved.append(mode)
        elif o == "Q":
            mode = saved.pop() if saved else 0
        elif o == "Tr":
            mode = int(operands[0])
        elif o in SHOW and mode == 3:
            dropped += 1
            continue
        out.append((operands, op))
    return pikepdf.unparse_content_stream(out), dropped


def forms(res, seen):
    """Form XObjects under a resource dictionary, each once (forms can nest and repeat)."""
    for x in (res.get("/XObject") or {}).values():
        if x.get("/Subtype") == "/Form" and x.objgen not in seen:
            seen.add(x.objgen)
            yield x
            yield from forms(x.get("/Resources") or {}, seen)


def main(src, dst):
    total, seen = 0, set()
    with pikepdf.open(src) as pdf:
        for page in pdf.pages:
            page.contents_coalesce()
            data, n = strip(page)
            page.Contents.write(data)
            total += n
            for x in forms(page.get("/Resources") or {}, seen):
                data, n = strip(x)
                x.write(data)
                total += n
        pdf.save(dst)
    print(f"dropped {total} invisible text runs")


if __name__ == "__main__":
    main(*sys.argv[1:3])

Ghostscript removes typed text too

gs -o clean.pdf -sDEVICE=pdfwrite -dFILTERTEXT scan.pdf

Its docs say it ignores all text, and it does. Our typed page lost all 44 of its words.

On a pure scan it works: the page images kept the same bytes, because Ghostscript passes JPEGs through.

To redo OCR, strip first

--redo-ocr swaps an old OCR layer for a new one. On the 10 scan pages it did. On our typed page it added a hidden copy of the 44 typed words, so search found each word twice.

ocrmypdf --mode strip in.pdf stripped.pdf
ocrmypdf --skip-text stripped.pdf out.pdf

That kept the typed page at 44 words, once, and read the two scan pages with OCR.

Acrobat and online tools

We tested none of them. Acrobat Pro and Foxit are paid apps.

Sources

Our Actor keeps an old layer

Our Actor reads a page's own text layer as it is, and that page is free. We ran its code on the OCRmyPDF file, then on the stripped copy.

Words found of 1,955
File Read from Words
With the OCR layer The layer, free 1,878
After the strip OCR, $0.06 1,877

Here the old layer was as good as a new read. Strip only a bad layer. When a layer is mostly not real words, the Actor checks the page with OCR on its own.

How we ran it
  • A Mac with Apple silicon. OCRmyPDF 17.14.2 (and the installed 17.13.0: same strip result), Tesseract 5.5.4, Ghostscript 10.08.0, pikepdf 10.17.0 (10.18.2 under OCRmyPDF 17.14.2), img2pdf 0.6.3, Poppler 26.03.0.
  • The scan: 10 proofread pages from two public domain books on Wikisource, 1,955 words, the same JPEGs as our earlier tests. ocrmypdf -l eng added the layer: 2,052 words of text.
  • The typed page: 5 lines in Helvetica, a standard font that is not embedded, drawn with pikepdf.
  • Same bytes: pdfimages -j before and after. Ghostscript moved each image by under 0.0001 pt, so 3 of 10 renders at 50 dpi land up to one pixel row off.
  • The Actor: its own code from our repository, language eng, as an Apify run would call it. Score: true words found in order.

Scanned pages to text, no install

Run it yourself. Our Apify Actor turns a scanned PDF into Markdown or text. $0.006 a page read with OCR; pages with real text are free.

Try the Actor

Or send us the scan. Tack, our AI, does the job for $29, up to 50 pages. First page free.

Get one page free

Questions people ask

How do I remove OCR from a PDF for free?

Run ocrmypdf --mode strip in.pdf out.pdf with OCRmyPDF 17.14.0 or newer. It removes the hidden OCR text and leaves the page images and any typed text as they are.

Does removing OCR change how the PDF looks?

Not with strip mode or our pikepdf script: every page image kept the same bytes. Ghostscript kept the image bytes too, but it also removes typed text. Tools that make each page a new image save the JPEG again, so it loses a little quality.

Can I remove OCR in Adobe Acrobat?

Acrobat Pro has Remove Hidden Information with a Hidden text item. Adobe does not say that it catches OCR text, and we did not test it.

Why remove an OCR layer?

To redo a bad OCR, to stop search from finding wrong words, or to share a scan without its text. OCRmyPDF also stops on a page that already has text, unless you pick a mode.

Does Ghostscript remove only the OCR text?

No. -dFILTERTEXT drops all text. On our typed page it removed all 44 words. Use it only on pure scans.