Skip to main content
Participating Frequently
December 13, 2022
Question

Adding Watermark in Adobe JavaScript

  • December 13, 2022
  • 1 reply
  • 615 views

Hello all,

 

my task is to add a Watermark based on key words in the pdf file on the first page. So if there is "DEST. 117.00 USD" in pdf file then add as a watermark "45125-123" if  there is in file "Credit note" then add watermark "457-125"  if nothing from above then add nothing. I checked it cannot be the situation when both variants "DEST. 117.00 USD" and "Credit note" can be in the file.

So, please help me to write a code in adobe JavaScript to solve this issue.

Please keep in mind that I am new to coding. I tried to write a part of the code.

Thanks for any support.

 

 

var pageArray = [];

var stringToSearchFor = "page\s1\sof\s1";

for (var p = 0; p < this.numPages; p++) {

// iterate over all words

for (var n = 0; n < this.getPageNumWords(p); n++) {

if (this.getPageNthWord(p, n) == "DEST. 117.00 USD") {
pageArray.push(p);
break;

}}

}
if (pageArray.length > 0) {

this.addWatermarkFromText({
cText: util.printd("45125-123", new Date()),
nHorizAlign: app.constants.align.center,
nVertAlign: app.constants.align.bottom,
nVertValue: 16, nFontSize: 10
});
}
else if (pageArray.length = 0) {

this.addWatermarkFromText({
cText: util.printd("457-125", new Date()),
nHorizAlign: app.constants.align.center,
nVertAlign: app.constants.align.bottom,
nVertValue: 15, nFontSize: 9

});}
else {}

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
December 13, 2022

You have multiple errors in your code.

The main one is that getPageNthWord only returns a single word at a time. If you're looking for a phrase that's longer than one word then you need to either first collect the whole page text and then search it for that phrase, or split the phrase into individual words and look for them one after another.

 

In addition, this is wrong:
(pageArray.length = 0)

As well as this:

util.printd("457-125", new Date())