New Tool Radar

OCR & Text Layers

How to Give Tesseract a User Patterns File for a Scanned PDF

Add your document's code patterns to Tesseract's user-patterns file so serials and references keep their shape.

Schematic illustration of a Tesseract user-patterns file: without it the references are misread, with it all four keep their AB-1234 shape.

Tesseract’s manual describes –user-patterns as a file of patterns Tesseract should consider while recognising. Patterns use placeholders such as \d for digits, which is the fix for codes with a fixed shape — a reference that is always two letters, a dash and four digits.

Use the practice file

A practice document, practice-user-patterns.pdf. The file is fictional.

Page Content
1 Orders list with 4 references shaped “AB-1234”
2 Orders list with 3 references shaped “CD-5678”
3 Notes, no references

Page 1 is the check page: the references read “AB-1234”, “AB-1235”, “AB-1236” and “AB-1237”.

Check the user-patterns file

Expected result. We checked the OCRmyPDF manual and the Tesseract manual. We did not run any tool. The checks below are what to expect if the work is done correctly; run them yourself on the practice file.

1. Run OCR without patterns

Run Tesseract on page 1 with the default settings.

Expected result: Note how the references come out — digits and letters may be swapped.

2. Write the patterns file

Create a text file with the pattern for two capital letters, a dash and four digits.

Expected result: The file holds the pattern for the AB-1234 shape.

3. Run OCR with the patterns

Run Tesseract with –user-patterns patterns.txt on page 1.

Expected result: Tesseract produces a new text file.

4. Compare the references

Search both outputs for the four references.

Expected result: The run with the patterns file returns all four references in shape; the run without it does not.

Verdict table

Check Expected result Pass?
Run OCR without patterns Note how the references come out — digits and letters may be swapped. —
Write the patterns file The file holds the pattern for the AB-1234 shape. —
Run OCR with the patterns Tesseract produces a new text file. —
Compare the references The run with the patterns file returns all four references in shape; the run without it does not. —

Related guides: How to Batch OCR a Multi-Page PDF with Consistent Settings — How to Add a Searchable Text Layer to a Scanned PDF.

Limits

  • A pattern file describes the shape of a code, not its value: it keeps AB-1234 shaped correctly, but it cannot tell a 3 from an 8.
  • Command-line OCR rewrites the file, so always work on a copy and keep the original scan untouched.
  • UNTESTED — no OCR tool was run; all results above are expected results. The practice file is fictional.

Sources

See all