DEV Community

Cover image for Finding and erasing old page numbers in a PDF, in the browser
Chandan Singh
Chandan Singh

Posted on

Finding and erasing old page numbers in a PDF, in the browser

I build editpdf.tools, a set of PDF and image tools that run in the browser. This week I shipped two updates, both because friends ran into these problems and asked me for an easy fix. The one with the more interesting code is in the page number tool, so most of this post is about that.

The problem

You have a 100-page PDF with page numbers, and you insert 5 pages after page 50. Now pages 51 to 55 have no number, and every page after them is off by five. Stamping new numbers on top gives you two numbers on most pages.

So the tool has to do four things:

  1. find the old numbers;
  2. be sure they really are page numbers;
  3. make them disappear without a visible box;
  4. do the same for scanned PDFs, without uploading the file.

1. Finding candidates in text PDFs

pdf.js getTextContent() gives text items with a transform matrix. I keep the items in the top and bottom 22% of the page as the reader sees it; pages with /Rotate go through the viewport first. Then I group the items into lines by baseline and test windows of up to six neighbouring items against these patterns:

  • 12
  • Page 12
  • 12 of 40
  • 12 / 40
  • - 12 -
  • roman numerals

One rule removed a lot of noise: a match must stand apart. It has to be the whole line, or be separated from the next word by a gap of more than about 1.2 times the text height. That rejects "see page 4" and headings like "Annex page 1".

2. Proving it is a page number

A footer can hold a year, a phone number, footnote markers or table rows. Pattern matching alone would erase all of them. The check that works is simple: a real page number goes up by one per page.

// candidate c on page index c.page, value c.v
const sameSequence = (c, d) =>
  d.v - d.page === c.v - c.page &&                       // rises by one per page
  d.band === c.band &&                                   // same top/bottom band
  Math.abs(d.vy - c.vy) < Math.max(1.5 * c.h, 12) &&     // same height on the page
  (Math.abs(d.nx - c.nx) < 0.2 || Math.abs(d.nx - (1 - c.nx)) < 0.2); // same x, or mirrored
Enter fullscreen mode Exit fullscreen mode

A candidate is kept only if a page within eight pages has a matching one. The offset (v - page) is allowed to change between sections, and that is exactly what happens after inserting pages:

  • pages 1 to 50 have offset +1;
  • the old pages after the insert have offset -4;
  • the inserted pages have no number at all.

A year stays the same on every page, so its offset changes from page to page and it never matches.

3. Scanned pages

A page with almost no text layer is treated as a scan. Tesseract.js reads only the top and bottom strips, rendered about 1500 px wide. Block mode (PSM 6) reads a lone number better than sparse mode, so it goes first.

OCR still misses sometimes: in one test it read a "5" as "b". The fix uses what we already know. If page 4 says 4 and page 6 says 6, then page 5 should say 5 in the same spot. The tool re-reads just that small area, with a digits-only fallback, and accepts the result only if it equals the expected value.

When a scan is blurry or the numbers are handwritten, the tool says so plainly instead of guessing.

4. Erasing without a visible patch

For each number, the tool:

  1. renders the area around it with pdf.js at 3x;
  2. takes the median colour on each side of the box, so a stray stroke does not count;
  3. drops any side that is clearly different from the others;
  4. fills the box with a bilinear blend of the remaining sides, plus a little noise on scans so the paper grain matches;
  5. embeds the patch as a PNG with pdf-lib at the exact position.

Then the new numbers are drawn.

The old number is hidden, not removed from the text layer, so copy or search can still find it. On screen and on paper you see only the new numbering.

Testing

I ran 21 PDFs through it:

  • pages inserted into a numbered file;
  • "Page n of N" written as separate words;
  • roman, then arabic numbering;
  • a coloured footer band;
  • a book layout with the numbers alternating sides;
  • a Word document exported to PDF with LibreOffice;
  • a LaTeX file;
  • rotated pages;
  • a 125-page file (2.7 s);
  • scans that were clean, skewed, blurry or handwritten.

After that I ran a pixel check that no ink was left in any old number box, and a diff check that nothing else on any page changed.

The other update: signing with a photo of your signature

PDF Sign used to accept only a drawn signature. Now you can also upload a photo of your signature on paper. The paper is removed in three steps:

  • a local brightness map, so shadows do not leave grey patches;
  • an automatic ink/paper threshold;
  • a cleanup of small specks.

The ink keeps its own colour. Each placed signature also keeps its own image, so one PDF can carry several different signatures.

Both tools: Page Numbers and PDF Sign. If you find a file where either one gets something wrong, I'd like to see it.

Top comments (0)