• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Problem with loop and addWatermarkFromFile

New Here ,
Aug 21, 2017 Aug 21, 2017

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.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

798

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 22, 2017 Aug 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); 

}; 

Votes

Translate

Translate
Community Expert ,
Aug 22, 2017 Aug 22, 2017

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

}; 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 22, 2017 Aug 22, 2017

Copy link to clipboard

Copied

Now it works as it should. Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 18, 2018 Mar 18, 2018

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines