webdevpuneet.com

pdf-lib Watermark and Page Numbers for Existing PDFs — Free Snippet

pdf-lib Watermark and Page Numbers for Existing PDFs · Tools · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

<div class="pw">
  <aside class="pw-panel">
    <h2>Stamp a PDF</h2>
    <label class="pw-file">Choose a PDF <input type="file" id="pwFile" accept="application/pdf"></label>
    <p class="pw-src" id="pwSrc">Using a generated 3-page sample.</p>
    <fieldset>
      <legend>Watermark</legend>
      <label>Text <input id="pwText" value="CONFIDENTIAL" maxlength="24"></label>
      <label>Size <input type="range" id="pwSize" min="24" max="110" value="64"></label>
      <label>Angle <input type="range" id="pwAngle" min="-90" max="90" value="35"></label>
      <label>Opacity <input type="range" id="pwOpacity" min="5" max="60" value="18"></label>
      <label>Colour <input type="color" id="pwColor" value="#dc2626"></label>
    </fieldset>
    <fieldset>
      <legend>Footer</legend>
      <label class="pw-check"><input type="checkbox" id="pwNums" checked> Page numbers "Page X of N"</label>
      <label>Position <select id="pwPos"><option value="center">Centre</option><option value="right">Right</option></select></label>
    </fieldset>
    <button type="button" id="pwDownload">Download stamped PDF</button>
    <p class="pw-status" id="pwStatus" role="status"></p>
  </aside>
  <div class="pw-preview" id="pwPages" aria-label="Preview of stamped pages"></div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Stamps existing PDFs
Load, draw on each page, save.
Rotated watermark
Any angle from −90° to 90°.
Correct rotated centring
Start point offset by the rotated half-size.
Adjustable opacity
Content underneath stays readable.
Colour picker
Hex converted to pdf-lib rgb().
Page X of N footers
Centred or right-aligned.
Non-destructive re-stamping
Always starts from the original bytes.
Live page previews
Up to six pages rendered with PDF.js.

About this UI Snippet

Watermarking PDFs With pdf-lib — Rotated Text, Opacity and Page Numbers

Screenshot of the pdf-lib Watermark and Page Numbers for Existing PDFs snippet rendered live

Stamping "DRAFT" or "CONFIDENTIAL" across every page, or adding page numbers to a document that doesn't have them, are everyday PDF chores. pdf-lib can modify an existing PDF in the browser: load it, draw on top of each page, save. This snippet adds a configurable watermark and footer and shows the stamped pages live.

Editing, not re-creating

PDFDocument.load(bytes) parses the existing file, and getPages() returns its pages. Drawing with page.drawText adds new content on top of what's already there — text, images and vector content in the original are untouched, and text stays selectable.

Always stamp from the original

Each change of text, size, angle or colour reloads the *original* bytes and stamps again. Stamping the previous output instead would layer watermark upon watermark every time a slider moved.

Centring rotated text is geometry

drawText with rotate: degrees(a) rotates around the text's start point — the left end of its baseline — not its centre. Putting that point at the page centre would swing the text off to one side. The snippet measures the text with widthOfTextAtSize and heightAtSize, then moves the start point back from the centre by half the width and half the height rotated by the same angle, so the watermark's midpoint lands exactly in the middle at any angle.

Transparency and colour

opacity makes the watermark translucent so the content underneath stays readable. The colour picker's hex value converts to pdf-lib's rgb(), which takes 0–1 components.

Page numbers

"Page X of N" is measured and either centred or right-aligned 40 points from the edge, 26 points above the bottom.

Private by default

Files are read with the File API and never uploaded. A generated three-page sample means the tool works without your own file.

Step by step

How to Use

  1. 1
    Start with the sampleA generated 3-page report is stamped immediately.
  2. 2
    Adjust the watermarkText, size, angle, opacity and colour update the preview.
  3. 3
    Toggle page numbersCentred or right-aligned "Page X of N".
  4. 4
    Use your own PDFChoose a file; it's processed locally.
  5. 5
    DownloadSaves stamped.pdf (open in its own tab if the sandbox blocks it).

Real-world uses

Common Use Cases

DRAFT stamps for reviews
Mark documents as DRAFT or FOR REVIEW before sharing. The rotated, semi-transparent text keeps the content underneath readable while making the document's status obvious.
Confidential report labels
Stamp CONFIDENTIAL across every page of a sensitive report, choosing any angle from −90° to 90° and the opacity that suits the document.
Adding page numbers after export
Fix documents exported without numbering by adding Page X of N footers to every page, with a live PDF.js preview before you download the result.
Sample and preview handouts
Stamp SAMPLE or PREVIEW across teaching materials or product files you want to share without giving away the usable version.
Batch stamping without a server
Build an internal tool that watermarks documents on the user's own device. Rotated text is centred correctly by offsetting its start point by half the rotated size.
Related: pdf-lib Merge PDFs
Related: jsPDF Invoice Generator
Create PDFs from scratch instead: jsPDF Invoice Generator with Live PDF Preview.

Got questions?

Frequently Asked Questions

Load the file with pdf-lib's PDFDocument.load, embed a font, and for each page call page.drawText with the watermark text, a rotate value from degrees(), a colour and an opacity. Save the document to get the stamped bytes.

drawText rotates around the text's start point. Measure the text width and height, then place the start point at the page centre minus half the width and height rotated by the angle: x = cx − (w/2)cosθ + (h/2)sinθ and y = cy − (w/2)sinθ − (h/2)cosθ.

Yes. Loop over doc.getPages() and draw "Page i of n" on each, measuring the label with font.widthOfTextAtSize to centre or right-align it.

No. A watermark is visible labelling, not protection; someone can remove or crop it. Use it to communicate status or ownership, not as security.

Drawing adds content permanently to the loaded document. Re-stamping the previous output would add another watermark each time, so the snippet always starts from the untouched source bytes.