Finding the blanks in a PDF form with nothing but pixels
Most PDF forms people deal with are flat: a scan, or a Word document exported to PDF with Name: ________ lines. To make one fillable you have to place a text box over every blank, by hand.
Acrobat's "Prepare Form" does this automatically, and I wanted the same thing in the browser for KaiPDF, where files never leave the device. The trick I ended up with ignores the PDF's structure completely. It renders each page to pixels and looks for the shapes a person looks for: a line with empty space above it, an empty box, a small square.
That means it works the same on scans as on born-digital PDFs. Here's how it works, in about 200 lines of TypeScript.
1. Render and threshold
The page is rendered with PDFium (compiled to WebAssembly) at about 2 px per point, then turned into a 1-bit "ink" map:
const dark = new Uint8Array(width * height);
for (let i = 0, j = 0; j < dark.length; i += 4, j++) {
const a = px[i + 3] / 255;
const lum = (0.299 * px[i] + 0.587 * px[i + 1] + 0.114 * px[i + 2]) * a + 255 * (1 - a);
dark[j] = lum < 150 ? 1 : 0;
}
2. Horizontal strokes
Scan each row for dark runs at least 6 pt long. A printed line is usually 1 to 3 pixels thick, so a run in one row is joined to a run in the next row when both ends line up within 2 px. Keeping only the strokes that touched the previous row makes this linear instead of quadratic, which matters on text-heavy pages:
let open: Seg[] = [];
for (let y = 0; y < height; y++) {
const next: Seg[] = [];
// ...for each dark run [x0, x1] in this row:
const s = open.find((g) => Math.abs(x0 - g.x0) <= 2 && Math.abs(x1 - g.x1) <= 2);
if (s) { s.y1 = y; next.push(s); }
else { /* start a new stroke */ }
open = next;
}
Anything thicker than ~2 pt is a filled area or bold text, not a stroke, and is dropped.
3. Boxes, table rows and tick boxes
Two strokes with the same span, a few points apart, are a box if both ends are joined by vertical ink. Checking a column is a cheap vote: what fraction of pixels between the strokes are dark (with ±2 px of tolerance for anti-aliasing).
A table row is just a wide box, so before accepting it, I look for interior columns that are dark top to bottom and cut the row into cells. Each cell is then checked for ink on its own. Header cells contain text, so they are skipped, and empty cells become fields.
A box that is small and square (under ~22 pt) becomes a checkbox.
4. Answer lines
A stroke that isn't part of a box is a candidate answer line, like Name: ________. The test that separates it from underlined text is the space above it: grow a rectangle upwards in 2 pt steps while it stays almost empty (<1.5% ink).
If at least 9 pt of empty space sits above the line, it's a blank waiting for an answer. Otherwise text sits on it and it's an underline.
5. Naming the fields
A field named "Text 7" is useless to whoever collects the answers. PDFium gives the page's text lines with positions, so each field takes its name from:
- text on the same row that ends just before it (
Date of birth: ____→ "Date of birth"), - otherwise text just above it, overlapping horizontally,
- for tick boxes, the text just to the right ("☐ Student" → "Student").
Trailing colons and underscores are trimmed. Cells in a table column inherit the column heading and get numbered: "Club 1", "Club 2".
Results
On a test membership form (labelled blanks, three tick boxes, a comments box and a three-column table) it found all 17 answer spaces and named every one correctly.
It misses things on skewed scans, so the editor lets you drag fields on, snap them to each other and rename them. Then pdf-lib writes real AcroForm fields that work in Acrobat Reader, Chrome, Edge and Firefox.
The pixel approach is crude compared to a layout model, but it costs nothing to run, needs no upload and handles scans for free. You can try it on your own form: Make PDF Fillable. If you'd rather start from scratch, the same site has a form designer with templates. I'd love to hear about forms it gets wrong.
Comments
No comments yet. Start the discussion.