NL-2610.005Result[nl.DO]
Editing text in a PDF without damaging the rest of the page
The Nook team
Submitted 5 October 2026 · 4 min read
Nook's PDF editor changes text where it stands, in the same font, using PDFium. In testing, a PDF made with an online PDF tool came back from an edit with its icons and numbers turned black: when PDFium rewrites a page, it can drop colours defined by ICC colour profiles, and gradients. Nook now draws the page before and after every edit and compares the pixels outside the edited words. If anything else changed, the edit is taken back and tried a safer way, and if no way is safe the page is left as it was. For words inside a scan, Nook reads them with Windows' built-in OCR and matches the lettering against 40 installed typefaces, or reuses the letters already on the page.
- Comments
- PDFium chromium/7881 through pdfium-render 0.9.4; Windows.Media.Ocr. Checked by two live tests.
- Subjects
- Documents (nl.DO)
- Cite as
NL-2610.005· usenook.ai/labs/pdf-edits-that-keep-the-page.html
Full text
Don't trust a PDF library to leave the rest of a page alone after an edit: draw the page before and after, and compare. That check caught colour loss we would otherwise have shipped.
The problem
A PDF page is a list of drawing instructions. To change a line of text, an editor changes the instructions for it, and the library writes the page out again. Nook uses PDFium, the PDF engine in Chrome, for this. Most pages come through untouched. One of our test PDFs, made with an online PDF tool, did not: after a one-word edit, its coloured icons and numbers came back black.
The cause was in the rewrite. When PDFium regenerates a page's instructions, colours defined through ICC colour profiles, and gradients, can be lost. The edit itself was right; the damage was everywhere else.
The check: draw it twice
- Before the edit, Nook draws the page at 1.5 pixels per point.
- It makes the edit, and draws the page again.
- It counts the pixels that changed outside the band around the edited words. A small allowance, which grows with the page's size, absorbs the soft edges of letters.
- If more than that changed, the edit is taken back and the next way is tried.
There are three ways, from the cleanest to the safest:
| Way | What it does |
|---|---|
| In place | Changes the text's own instructions. The cleanest result, and searchable text stays searchable. |
| Recoloured | The same, after setting every colour on the page as plain RGB, so nothing depends on a colour profile the rewrite could drop. |
| Over | Leaves the page's drawing untouched: covers the old words in the paper's colour and puts the new text on top. |
If none of the three passes, Nook says it could not change that text without spoiling the rest of the page, and leaves the page as it was. A kept edit becomes one undo step.
The live test keeps_the_rest_of_the_page builds a page designed to break things: an ICC-based colour space, a transparent fill, a pattern and a clipped shape, and checks that an edit leaves all of them as they were.
Text inside a scan
A scanned form has no text to change, only a picture of it. Nook edits that too:
- Read it. Windows' built-in OCR (Windows.Media.Ocr) finds the words, in the languages installed on the computer.
- Clean it. Otsu thresholding separates ink from paper; the lines of a form and the ink of neighbouring words are left out; the baseline is where the ink drops off most sharply.
- Find the typeface. Nook draws the word in each of 40 Windows and Office typefaces and scores each by how much it overlaps the scan, less how far its width is off. The best one sets the new words.
- Or reuse the page's letters. The “Page letters” option cuts the scanned letters out of the words OCR found and sets the new text with them. A letter from the page is used only if it is the same ink, within 12% of the height needed (or 1.5 pixels, for small marks such as a hyphen), and at least 45% the same shape as the matched typeface's letter; otherwise the typeface fills in.
- Put it back. The old letters are covered in the paper's colour, and the new text goes on as a transparent picture with invisible, searchable text beneath it.
The live test for this is edits_text_in_a_picture.
Notes for developers
- pdfium-render draws RGBA by default. If you need BGRA, call
set_reverse_byte_order(false). - In ab_glyph, a
PxScaleis the height from ascent to descent, not the em. Convert before you compare a font size with a PDF's. - When a PDF's embedded font subset lacks a letter you need, the same typeface installed in Windows usually has it; Nook finds it through the registry.
- For converting a PDF to Word, do not drive Microsoft Word: its PDF import can wait forever on a dialog no one sees. Nook takes the text out with PDFium and builds the document with Pandoc.