Wsjs

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

// PDF.

js initialization
pdfjsLib.GlobalWorkerOptions.workerSrc =
'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.worker.min.js';

// Function to add watermark to the PDF


function addWatermarkToPDF(pdfData) {
const watermarkText = "Confidential"; // Replace with your watermark text
const options = { textRotation: -45, opacity: 0.5 }; // Customize watermark
options

// Load the PDF document


pdfjsLib.getDocument({ data: pdfData }).promise.then(function (pdf) {
// Fetch the first page
pdf.getPage(1).then(function (page) {
const canvas = document.createElement('canvas');
const viewport = page.getViewport({ scale: 1.0 });
const context = canvas.getContext('2d');

// Set the canvas dimensions


canvas.height = viewport.height;
canvas.width = viewport.width;

// Render the page content on the canvas


const renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext).promise.then(function () {
// Draw the watermark text on the canvas
context.font = '48px Arial';
context.fillStyle = 'red';
context.rotate(options.textRotation * Math.PI / 180);
context.globalAlpha = options.opacity;
context.fillText(watermarkText, 100, 100); // Adjust the position of the
watermark

// Convert the canvas to base64 data URL


const imageDataUrl = canvas.toDataURL();

// Create an image element with the watermark and append it to the PDF
container
const watermarkImage = new Image();
watermarkImage.src = imageDataUrl;
document.getElementById('pdfContainer').appendChild(watermarkImage);
});
});
});
}

// Fetch the PDF file and add watermark when the document is ready
document.addEventListener('DOMContentLoaded', function () {
// Replace 'example.pdf' with the URL or path to your PDF file
fetch('example.pdf')
.then(response => response.arrayBuffer())
.then(arrayBuffer => addWatermarkToPDF(new Uint8Array(arrayBuffer)));
});

You might also like