Last active
November 28, 2023 08:24
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
// Add the link text | |
doc.fontSize(25) | |
.fillColor('blue') | |
.text('This is a link!', 20, 10); | |
// Measure the text | |
const width = doc.widthOfString('This is a link!'); | |
const height = doc.currentLineHeight(); | |
// Add the underline and link annotations | |
doc.underline(20, 10, width, height, {color: 'blue'}) | |
.link(20, 10, width, height, 'http://example.com/'); | |
// Finalize the PDF document | |
doc.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment