Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Your PDF shows text, but there is none: detecting outlined letters

A form came to us that looked completely normal. Logo at the top, labels, lines to write on, a small table. Then we tried to work with it. You could not click a single word. Search found nothing. Copy and paste gave an

A form came to us that looked completely normal. Logo at the top, labels, lines to write on, a small table. Then we tried to work with it.

You could not click a single word. Search found nothing. Copy and paste gave an empty clipboard. Our own OCR tool said the file "already had real text" and skipped it, and PDF to Word called it a scan.

None of that was true. The page had no text at all, and it was not a scan either. Every letter had been turned into a drawing.

What "outlined text" means

Design apps have an export option usually called "convert text to outlines" or "create outlines". Designers use it so a file looks the same on every machine, even if the font is not installed. The app replaces each letter with the vector shape of that letter: a few curves, filled black.

Inside the PDF this means:

  • no font in the page resources
  • no text drawing operators (Tj, TJ) in the content stream
  • thousands of path operators instead (m, l, c, f)

To your eyes it is text. To any program it is a picture made of lines, like a logo.

Why tools get it wrong

Most PDF tools ask two questions about a page:

  1. Does it have text? If yes, treat it as a normal document.
  2. If not, does it have an image? If yes, it is probably a scan, so run OCR.

An outlined page answers "no" to both. There is no text, and there is no image either, only vector paths. So the page falls through both checks and gets treated as blank, or as decoration. That is exactly the bug we had: the "no text, no image" case was being counted as a text page.

Detecting it

The fix is to look at what the page actually draws. With pdf.js you can count operators from getOperatorList():

import * as pdfjs from "pdfjs-dist";
const { OPS } = pdfjs;

async function looksLikeOutlinedText(page) {
  const { fnArray } = await page.getOperatorList();
  let text = 0, paths = 0, images = 0;
  for (const op of fnArray) {
    if (op === OPS.showText || op === OPS.showSpacedText) text++;
    else if (op === OPS.constructPath) paths++;
    else if (op === OPS.paintImageXObject || op === OPS.paintInlineImageXObject) images++;
  }
  return text === 0 && images === 0 && paths > 300;
}

The number 300 is a judgement call, not a law. A simple chart or a logo can have a few dozen paths; a page of outlined letters usually has hundreds or thousands. Tune it on your own files. A stricter check also looks at the size of the paths: letters are many small closed shapes sitting on the same baselines, while a diagram has fewer, larger ones.

What to do once you know

Reading it. Render the page to an image (around 300 dpi) and run OCR, exactly as you would for a scan. Then you get words with positions, and you can put an invisible text layer on top so search and copy work.

Editing it. This is where we learned the hard way. Our first version placed the new word on top of the old one, which looked fine on screen but saved the new text over the old letter shapes, so both printed. To replace a word you have to remove the paths that fall inside that word's box. Bold letters are often drawn twice (a fill and a stroke), so you need to remove both or a ghost outline stays behind. Then draw the new text in a font that matches the size and weight of the shapes you removed.

Converting it. For PDF to Word, OCR the page first and build paragraphs from the OCR lines. Do not let leftover letter shapes or dotted leader lines feed a table detector. On our test form they turned into a grid of narrow, empty table columns.

A note for people who make forms

If a PDF is meant to be filled in, searched or edited later, export it with the fonts embedded instead of outlined. Embedded fonts keep the look just as well, and the file stays readable by people, screen readers and every tool that comes after.

We ran into this while building the PDF editor at KovaPDF. Outlined pages are now detected and read with OCR there, so the words can be clicked and changed: https://www.kovapdf.com/edit-pdf

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.