Add a Multiline Watermark to PDF document in Node.js

Is there a way to apply a watermark (text stamp) to all pages in a document with one api call?

I am using the nodejs sdk “asposepdfcloud” and I want to add a multiline watermark. How can I do that? I am currently doing this:

const textState = new TextState();
textState.fontSize = 48;
textState.font = ‘Arial’;

const stamp = new TextStamp();
stamp.background = true;
stamp.leftMargin = 1;
stamp.rightMargin = 2;
stamp.topMargin = 3;
stamp.bottomMargin = 4;
stamp.horizontalAlignment = HorizontalAlignment.Center;
stamp.verticalAlignment = VerticalAlignment.Center;
stamp.opacity = 0.3;
stamp.rotate = Rotation.on270;
stamp.rotateAngle = 45;
stamp.xIndent = 0;
stamp.yIndent = 0;
stamp.zoom = 1;
stamp.textAlignment = HorizontalAlignment.Center;
stamp.value = text;
stamp.textState = textState;

But stamp.value does not do anything interesting with \n\r I still get one line.

@aqpeeb

You can use PostDocumentTextHeader API method to add text to all pages of the PDF document. Please check the sample Node.js code for reference.


async function test(){
const { PdfApi } = require("asposepdfcloud");
const { TextHeader }= require("asposepdfcloud/src/models/textHeader");
const { TextState }= require("asposepdfcloud/src/models/textState");
const { HorizontalAlignment }= require("asposepdfcloud/src/models/horizontalAlignment");
const { Color }= require("asposepdfcloud/src/models/Color");
const { Rotation }= require("asposepdfcloud/src/models/rotation");
const { FontStyles }= require("asposepdfcloud/src/models/fontStyles");


// Get App Key and App SID from https://dashboard.aspose.cloud/
pdfApi = new PdfApi("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx", "xxxxxxxxxxxxxxxxxx");


const color = new Color();
color.a = 1;
color.r = 0;
color.g = 0;
color.b = 0;

const textState = new TextState();
textState.fontSize = 36;
textState.font = "Arial";
textState.foregroundColor = color;
textState.fontStyle = FontStyles.Regular;

const textHeader = new TextHeader();
textHeader.background = true;
textHeader.horizontalAlignment = HorizontalAlignment.Center;
textHeader.textAlignment = HorizontalAlignment.Center;
textHeader.topMargin = 250;
textHeader.opacity = 0.5,
textHeader.zoom = 1;
textHeader.rotate = Rotation.on90;
textHeader.rotateAngle = 45;
textHeader.value = "test aspose cloud";
textHeader.textState = textState;

const processWatermark = new Promise((resolve, reject) => {
let name = "4pages.pdf";


pdfApi.postDocumentTextHeader(name, textHeader, null, null, null, null)
    .then((result) => resolve(true))
    .catch((err) => resolve(true));
})
console.log(await processWatermark);
}

test();

I am afraid you can not add a multiline text stamp. However, you can add text twice to different positions.