Skip to content

Engineering · · 5 min read

How we edit text in a PDF, in the browser, without a white box

A PDF is not a word processor file: a page is a list of drawing instructions, and its fonts often hold only the letters the author used. This is how the editor changes that text in place, what it does when a letter is missing, and where it stops.

By the getPDF team

The easy way to “edit” a PDF is a white rectangle over the old words and new ones typed on top. It looks right, and it leaves the old text in the file, where it still selects, searches and copies from under the box. getPDF’s editor changes the text that is already on the page instead, through the engine Chrome uses to show PDFs, and then writes the file anew.

A page is a list of objects

The engine is PDFium compiled to WebAssembly, running in a Web Worker in your tab. When the editor opens a page, PDFium hands it the page’s objects: text, paths (lines and shapes) and pictures. Each object keeps the same id while the document is open, so an edit, its undo and its redo all point at the same thing.

A text object is a run of characters with one font, one size, one fill colour and one matrix, the transformation that places, scales and rotates it. For each run the engine also reports the font’s name, whether the font is embedded in the file, and a box around every character. When you click, PDFium names the character under the pointer and the run it belongs to.

Double-click a run and type, and the new text has to go back into that object, in that font. Whether it can depends on the font.

Why the font may not have your letter

PDFs from Word, a browser or an accounting program almost always carry their fonts as subsets: a font cut down to the letters the document used, marked by a 6-letter prefix such as BAAAAA+ArialMT. “Payment due within 30 days” may carry a 3 and a 0 but no 1 and no 4. Change it to “14 days” and those glyphs are not in the file.

So before it writes anything, the engine asks the run’s font for the outline of every character you typed, spaces aside. An empty outline means a missing letter. Then one of 4 things happens:

The run’s font The new text What the editor does
Embedded, has every letter anything Changes the text in place: same font, size, colour and position
Embedded, a letter is missing fits the standard PDF fonts Rewrites the run in Helvetica, Times or Courier, picked from the font’s name
Embedded, a letter is missing needs more (ł, Č, Greek, Cyrillic) Rewrites the run in Liberation Sans, Serif or Mono, embedded in the file
Not embedded anything Changes the text in place; each viewer draws it with its own copy, as before

In place means 2 PDFium calls: one sets the object’s new text, the next regenerates the page’s content stream. The engine then reads the text back; if it did not take, the old text goes back and the substitute path runs instead.

Why the substitutes keep the line length

Helvetica, Times and Courier are 3 of the 14 standard PDF fonts: every viewer has them, so nothing is embedded. Liberation Sans, Serif and Mono are drawn to the widths of Arial, Times New Roman and Courier New. The Liberation files are fetched from this site only when an edit needs one, and the whole font goes into the file; in our test that added about 230 KB, once per font.

Matching widths keeps a rewritten line from running into the next column. On a test invoice, an Arial line was 184.0 points wide before and after it moved to Helvetica. A Verdana line came out 13 % narrower, because Verdana has no metric twin among the substitutes. Why the font changes when you edit a PDF has the full table.

Some scripts no bundled font can draw: Arabic, Hebrew, Chinese, Japanese and Korean, Thai, the Indic scripts, Armenian and Georgian. When we measured it, Arabic and Chinese came out as empty boxes, so the editor now refuses such an edit, names the script, and leaves the line as it was.

Edit PDFReal text editing, objects, annotations, forms. Free, runs on your device.

Saving: written in full, so the old line is gone

PDF allows an incremental save: the original bytes stay at the front and the changes are appended. The editor first saved that way, but it keeps the earlier revision inside the file: change an invoice total, and a text editor still finds the old amount in the saved bytes.

So the editor now writes the whole file every time, and PDFium’s full save writes only the objects the document still uses. On our 3-page test file, after the same edit, the incremental save was 4,951 bytes and still held the old text; the full save was 2,294 bytes and did not, with every stream inflated and searched. An end-to-end test edits the total in the browser, saves, and checks for one revision, the new amount, and no trace of the old one.

The exception is a file with a digital signature: rewriting the signed bytes would break the signature, so its changes are appended. The editor says so after the save: the signature stays valid for what was signed, and that signed version, with any text you changed, is still inside the file.

Because the edit lands in a real text object, the saved text stays selectable and searchable; the engine tests search the saved file for it.

Where it stops, and the white box

The engine cannot change every piece of text, and the editor says so rather than painting over it:

  • Scans. A scanned page is a picture, with no text object to edit. OCR PDF adds the words it reads as an invisible layer, which makes the page searchable but not editable. Editing a scanned PDF walks through that case.
  • Text the engine does not rewrite. Find and replace skips a match inside grouped content, written letter by letter, or in an OCR layer, and says how many it skipped and why.
  • Type 3 fonts and text in patterns. Our font decision record names these as files the engine cannot edit.

For those, covering the old words and typing new ones is the last resort: a fallback, never the default, and labelled as such when the editor offers it as a mode. A white box hides text from the eye, not from search, copy or a text editor; white out or edit for real shows what each leaves behind. When nobody may read the old words, use redaction, which takes the text and pictures under its boxes out of the saved file.

Redact PDFRemoves content, not just a black box. Free, runs on your device.