Copy link to clipboard
Copied
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.
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);
};
Copy link to clipboard
Copied
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);
};
Copy link to clipboard
Copied
Now it works as it should. Thanks.
Copy link to clipboard
Copied
Bernd,
Thank you so much! I've been working on a script that does this (and a little more) for days off and on. Probably around 12 hours if I'm being honest. I was really good at making the code correct, but deliver the wrong result in all kinds of different ways. Mostly like Thomas describes.
I finally got a version of the code developed where it would work if I batch processed all blank documents with matching individual serialized watermark documents. But then I couldn't figure out how to get it to operate on just the odd pages. Then I googled a final time with that added and here is your amazing code. If only I had googled a little more before going down this path!
But anyway, my time wasn't wasted because I needed to expand your above code for my own use. I added my developed code into yours and now it's perfect. It's amazing.
Thank you so much for freely offering your knowledge and expertise. I couldn't have finished my code without yours. This will save so much time, it was worth every stressful second to get here.