Skip to main content
thomasj54347878
Participant
Aug 22, 2017 4:26 AM UTC
Answered

Problem with loop and addWatermarkFromFile

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 is closed to new replies. Start a new post to keep the conversation going.
Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
Aug 22, 2017 7:56 AM UTC

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); 

}; 

3 replies

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
Aug 22, 2017 7:56 AM UTC

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); 

}; 

Translate
thomasj54347878
Participant
Aug 22, 2017 8:24 AM UTC

Now it works as it should. Thanks.

Translate