How to Watermark a PDF in Node.js

Cloudmersive
2 min readSep 22, 2023

--

Watermarking our PDF is the “finishing touch” to ensure our document can’t be stolen and repurposed by an unsolicited third party.

Using the below code, we can take advantage of a free PDF Watermark API that allows us to add a custom watermarking workflow into our applications. Along with customizing our watermark’s text, we can format our font name, font size, font color and font transparency to make sure it fits our marketing/branding needs.

We can easily structure our API call in two quick steps.

Let’s first install the SDK by either running this command:

npm install cloudmersive-convert-api-client --save

Or by adding the following snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

And then let’s copy the following ready-to-run Node.js code examples into our file & provide a free-tier API key to authorize our requests:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();

var watermarkText = "watermarkText_example"; // String | Watermark text to add to the PDF (required)

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.

var opts = {
'fontName': "fontName_example", // String | Font Family Name for the watermark text; default is Times New Roman
'fontSize': 8.14, // Number | Font Size in points of the text; default is 150
'fontColor': "fontColor_example", // String | Font color in hexadecimal or HTML color name; default is Red
'fontTransparency': 8.14 // Number | Font transparency between 0.0 (completely transparent) to 1.0 (fully opaque); default is 0.5
};

var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfWatermarkText(watermarkText, inputFile, opts, callback);

Now we can quickly secure our most sensitive PDF documents with our own standardized watermark using minimal code. Easy!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.