Skip to main content
thomasj54347878
Participant
August 22, 2017
Answered

Problem with loop and addWatermarkFromFile

  • August 22, 2017
  • 1 reply
  • 1244 views

Hello everyone

I'd like to insert a PDF as a watermark on odd page numbers in a specific page range of another PDF. My problem with the code below is that the loop doesn't work. The watermark is always inserted on all pages of my target PDF (all 600 pages). Even if I replace the variables with fixed numbers, the watermark appears on all 600 pages.

I have to say, that I'm a beginner regarding JavaScript. Here's my code:

var nStart = 1;
var nEnd = 11;

function watermark() {
     this.addWatermarkFromFile({
     cDIPath: "Register_DEU_Layerk_Rand.pdf",
     nSourcePage: 0,
// nStart: 0,
// nEnd: 11,
     nHorizAlign: app.constants.align.right,
     nVertAlign: 0,
     })
}
var p;
for (var p = nStart - 1; p <= nEnd; p++)
     if (p%2==1) {
     watermark();
};

Where's the problem?

Thanks in advance.

This topic has been closed for replies.
Correct answer Bernd Alheit

Use something like this:

var nStart = 1; 

var nEnd = 11; 

function watermark(p) { 

     this.addWatermarkFromFile({ 

     cDIPath: "Register_DEU_Layerk_Rand.pdf", 

     nSourcePage: 0, 

     nStart: p, 

     nEnd: p, 

     nHorizAlign: app.constants.align.right, 

     nVertAlign: 0, 

     }) 

for (var p = nStart - 1; p <= nEnd; p++) 

     if (p%2==1) { 

     watermark(p); 

}; 

1 reply

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
August 22, 2017

Use something like this:

var nStart = 1; 

var nEnd = 11; 

function watermark(p) { 

     this.addWatermarkFromFile({ 

     cDIPath: "Register_DEU_Layerk_Rand.pdf", 

     nSourcePage: 0, 

     nStart: p, 

     nEnd: p, 

     nHorizAlign: app.constants.align.right, 

     nVertAlign: 0, 

     }) 

for (var p = nStart - 1; p <= nEnd; p++) 

     if (p%2==1) { 

     watermark(p); 

}; 

thomasj54347878
Participant
August 22, 2017

Now it works as it should. Thanks.