Building a Multi-PDF Bates Stamper, Macro Interpolator, and Bezier Signature Canvas in Browser TypeScript
How to maintain sequential Bates counters across multi-PDF batches, interpolate SHA-256 macros, and smooth HTML5 Bezier ink strokes. Recently I built an in-browser PDF utility for three tasks that usually require either
How to maintain sequential Bates counters across multi-PDF batches, interpolate SHA-256 macros, and smooth HTML5 Bezier ink strokes.
Recently I built an in-browser PDF utility for three tasks that usually require either desktop Acrobat Pro or uploading files to a cloud service:
Drawing smooth handwritten signatures (or vector round seals) and appending a WebCrypto SHA-256 audit page.
Injecting 45-degree tiled underlay watermarks behind existing PDF text streams.
Running sequential Bates numbering (
CASE-000001..CASE-004500) across a batch of multiple PDF files without clipping existing page footers.
Here are the three most useful TypeScript snippets from that build: smoothing raw mouse/touch coordinates into quadratic Bezier ink curves, carrying sequential Bates counters across multi-file queues, and interpolating runtime SHA-256 macros.
1. Smoothing Raw Pointer Events into Quadratic Bezier Ink Curves
If you capture raw pointermove events on an HTML5 <canvas> and connect them with straight ctx.lineTo(x, y) segments, handwritten signatures look jagged and polygonalβespecially when a user signs quickly with a mouse or trackpad.
Instead of connecting raw points (p[i].x, p[i].y) directly, we compute the midpoint mid between consecutive points p[i] and p[i + 1] and use p[i] as the control point of a quadratic Bezier curve (ctx.quadraticCurveTo):
export interface InkPoint {
x: number;
y: number;
}
export function renderSmoothBezierStroke(
ctx: CanvasRenderingContext2D,
points: InkPoint[],
strokeColor = '#0f172a',
lineWidth = 2.6
): void {
if (points.length === 0) return;
ctx.save();
ctx.strokeStyle = strokeColor;
ctx.lineWidth = lineWidth;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
if (points.length === 1) {
ctx.arc(points[0].x, points[0].y, lineWidth / 2, 0, Math.PI * 2);
ctx.fillStyle = strokeColor;
ctx.fill();
ctx.restore();
return;
}
for (let i = 1; i < points.length - 1; i++) {
const midX = (points[i].x + points[i + 1].x) / 2;
const midY = (points[i].y + points[i + 1].y) / 2;
ctx.quadraticCurveTo(points[i].x, points[i].y, midX, midY);
}
const last = points[points.length - 1];
ctx.lineTo(last.x, last.y);
ctx.stroke();
ctx.restore();
}
This turns even low-sample-rate trackpad strokes into continuous $C^1$-smooth ink curves that export cleanly into a PDF appearance stream.
2. Carrying Sequential Bates Counters Across Multi-PDF Batches
In legal eDiscovery, Bates numbering doesn't reset to 1 on each file. If a batch contains 3 PDFs (12 pages, 8 pages, and 25 pages) starting at 000001, File #2 must start at 000013 and File #3 must start at 000021.
Here is how we structure the stateful batch planner and dynamic macro interpolator ({{BATES}}, {{PAGE}}, {{TOTAL}}, {{FILE}}, {{DATE}}, {{SHA256}}):
export interface BatchPdfDescriptor {
fileName: string;
pageCount: number;
sha256Hex: string;
}
export interface PlannedFileBatesRange {
fileName: string;
exhibitLabel: string;
startSequence: number;
endSequence: number;
firstStampPreview: string;
lastStampPreview: string;
}
export function formatBatesMacro(
template: string,
prefix: string,
seq: number,
digits: number,
suffix: string,
meta: { page: number; total: number; file: string; sha256: string; date: string }
): string {
const batesId = `${prefix}${String(seq).padStart(Math.max(1, digits), '0')}${suffix}`;
return template
.replace(/\{\{BATES\}\}/g, batesId)
.replace(/\{\{PAGE\}\}/g, String(meta.page))
.replace(/\{\{TOTAL\}\}/g, String(meta.total))
.replace(/\{\{FILE\}\}/g, meta.file)
.replace(/\{\{DATE\}\}/g, meta.date)
.replace(/\{\{SHA256\}\}/g, meta.sha256.slice(0, 12));
}
export function planMultiPdfBatesBatch(
files: BatchPdfDescriptor[],
startNumber: number,
digits: number,
prefix: string,
suffix: string,
template: string,
isoDate: string
): PlannedFileBatesRange[] {
let cursor = Math.max(1, Math.floor(startNumber));
return files.map((file, idx) => {
const startSeq = cursor;
const endSeq = cursor + Math.max(1, file.pageCount) - 1;
cursor = endSeq + 1;
const exhibitLetter = String.fromCharCode(65 + (idx % 26)); // A, B, C...
return {
fileName: file.fileName,
exhibitLabel: `EXHIBIT ${exhibitLetter}`,
startSequence: startSeq,
endSequence: endSeq,
firstStampPreview: formatBatesMacro(template, prefix, startSeq, digits, suffix, {
page: 1,
total: file.pageCount,
file: file.fileName,
sha256: file.sha256Hex,
date: isoDate,
}),
lastStampPreview: formatBatesMacro(template, prefix, endSeq, digits, suffix, {
page: file.pageCount,
total: file.pageCount,
file: file.fileName,
sha256: file.sha256Hex,
date: isoDate,
}),
};
});
}
3. Preventing Footer Collisions with 94% Shrink-to-Fit
To prevent those Bates stamps from printing over existing footer text on full-bleed scans, we wrap each pageβs existing content stream in q 0.94 0 0 0.94 tx ty cm ... Q before drawing the 6-position header/footer stamps. That reserves a clean ~24pt margin collar around the page while keeping the outer MediaBox size unchanged.
You can test the live client-side workspace at KivaroTools PDF Digital Signer, Watermark & Bates Stamper (part of my browser utility suite on kivarotools.com).
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.