Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save documentprocessing/13186a46a11626d4cfad6db39a8411b9 to your computer and use it in GitHub Desktop.
Save documentprocessing/13186a46a11626d4cfad6db39a8411b9 to your computer and use it in GitHub Desktop.
Add Links, Crossed-Out Text & Interactive Notes Annotations to PDF documents in JavaScript using PDFKit Library. Check https://products.documentprocessing.com/annotation/javascript/pdfkit/ for more details.
// Include pdfkit library and fs module of Node.js
const PDFDocument = require('pdfkit');
const fs = require('fs');
// Create a new PDF document
const doc = new PDFDocument();
// Create a writable stream to save the PDF
const stream = fs.createWriteStream('annotations.pdf');
// Pipe the PDF document to the writable stream
doc.pipe(stream);
doc.fontSize(36);
// Write the text
doc.text('Strike!', 50, 50);
// Set the line color and width for strikethrough
doc.strokeColor('black');
doc.lineWidth(5);
// Draw a line through the text
const textWidth = doc.widthOfString('Strike!');
doc.moveTo(50, 50 + doc.currentLineHeight() / 2)
.lineTo(50 + textWidth, 50 + doc.currentLineHeight() / 2)
.stroke();
// Finalize the PDF document
doc.end();
// Include pdfkit library and fs module of Node.js
const PDFDocument = require('pdfkit');
const fs = require('fs');
// Create a new PDF document
const doc = new PDFDocument();
// Create a writable stream to save the PDF
const stream = fs.createWriteStream('annotations.pdf');
// Pipe the PDF document to the writable stream
doc.pipe(stream);
doc.fontSize(36);
// Write the text
doc.text('Strike!', 50, 50);
// Set the line color and width for strikethrough
doc.strokeColor('black');
doc.lineWidth(5);
// Draw a line through the text
const textWidth = doc.widthOfString('Strike!');
doc.moveTo(50, 50 + doc.currentLineHeight() / 2)
.lineTo(50 + textWidth, 50 + doc.currentLineHeight() / 2)
.stroke();
const x = 100; // X-coordinate of the note
const y = 100; // Y-coordinate of the note
const width = 200; // Width of the annotation
const height = 50; // Height of the annotation
const contents = 'Hello i am a note added using the PDFKit library';
// Add a note annotation to the PDF
doc.note(x, y, width, height, contents, {
Title: 'Note',
Open: true,
Name: 'Note',
});
// Finalize the PDF document
doc.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment