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

Javascirpt to delete pages with specifc watermark

Guest
Apr 17, 2017 Apr 17, 2017

HI,

Does any one have suggestion on a Java Script that would delete pages with a specific Watermark. 

I have a script that will delete pages with specific "words", but need some help modifying it, so it looks pages with watermarks and then deletes them.  I am currently using the Action Wizard.

picture.png

TOPICS
Acrobat SDK and JavaScript , Windows
1.4K
Translate
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 , Apr 19, 2017 Apr 19, 2017

Which version of Acrobat are you using?

There is actually a syntax error in try67's code, but not what your console screenshot shows: The function to delete pages is "deletePages" and not "deletePage" - the "s" is missing. in line #8. Other than that it is working and does exactly what it's supposed to do.

Can you execute other code in the console? Try the following different snippets, do they work?

Test 1:

app.alert("Hello World!");

Test 2:

for (var i=3; i>=0; i--) {

  app.alert("Countdown: " + i);

}

If

...
Translate
Community Expert ,
Apr 17, 2017 Apr 17, 2017

Do you mean watermarks with a specific name? If so, then you can use the

getOCGs method to retrieve all the Optional Content Groups on a specific

page (a watermark is placed in such a group), and then search that array

for the one you're after. If you find it, then you can delete that page.

On Mon, Apr 17, 2017 at 3:30 PM, chas37922860 <forums_noreply@adobe.com>

Translate
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
Guest
Apr 17, 2017 Apr 17, 2017

Would I use the same method if I wanted to delete all pages in a document that would have a watermark ?  or is there a quicker option ?

Thanks

Translate
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
Community Expert ,
Apr 17, 2017 Apr 17, 2017

Not sure what you mean... That is​ the way of doing that.

Translate
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
Guest
Apr 18, 2017 Apr 18, 2017

Hi,

Its been a few years since I dabbled in Java Script, so hoping to get some feedback.   I don't think I structured the script correctly.  Bottom line is that I have a pdf file that has pages with a "Watermark".    I need the Java Script to find the pages with the "Watermark" and delete them.  I am using the Action Wizard.

Pic2.png

Translate
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
Community Expert ,
Apr 18, 2017 Apr 18, 2017

Almost, but not quite... Post your code as text and I'll help you fix it.

Translate
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
Guest
Apr 18, 2017 Apr 18, 2017

var aOCGs = this.getOCGs();

for (var p=this.numPages-1; p>=0; p--) {

for(var i=0; aOCGs && i<aOCGs.length;i++)

{

if(aOCGs.name == "Watermark");

this.deletePage(i);

}

}

Translate
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
Community Expert ,
Apr 18, 2017 Apr 18, 2017

Try this instead:

pagesLoop:

for (var p=this.numPages-1; p>=0; p--) {

    if (this.numPages==1) break pagesLoop;

    var aOCGs = this.getOCGs(p);

    if (aOCGs!=null && aOCGs.length>0) {

        for (var i=0; i<aOCGs.length; i++) {

            if (aOCGs.name == "Watermark") {

                this.deletePages(p, p);

                continue pagesLoop;

            }           

        }

    }

}

EDIT: fixed mistake in the code.

Translate
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
Guest
Apr 18, 2017 Apr 18, 2017

Thanks for the help.   I created a "Execute JavaScript"  in the Action Wizard and pasted the above.  Unfortunately, it didn't work.  The pages with the Watermark are still intact.  Am I missing a step ?

Translate
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
Community Expert ,
Apr 18, 2017 Apr 18, 2017

Did you add a command to save the file, after running the script on it?

Try it first from the Console window, to make sure that it works, before putting it in an Action.

Translate
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
Guest
Apr 18, 2017 Apr 18, 2017

Tried it console window and got a syntax error.   See attached screen shot.

Pic3.png

Translate
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
Community Expert ,
Apr 18, 2017 Apr 18, 2017

You need to select the full code with the mouse or keyboard (Ctrl+A) before executing it.

Translate
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
Guest
Apr 19, 2017 Apr 19, 2017

Hi,

I selected full code and also tried adding it as an action script.  Neither did anything.   Not sure if you have a test file where you can test the script.  I would be curious if it works for you.

Thanks  !

Translate
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
Community Expert ,
Apr 19, 2017 Apr 19, 2017

It would be better if you could share a sample file with us, so we can test it for ourselves.

Use something like Dropbox, Google Drive, Adobe Cloud, etc., and post the link to it here.

Translate
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
Community Expert ,
Apr 19, 2017 Apr 19, 2017

Which version of Acrobat are you using?

There is actually a syntax error in try67's code, but not what your console screenshot shows: The function to delete pages is "deletePages" and not "deletePage" - the "s" is missing. in line #8. Other than that it is working and does exactly what it's supposed to do.

Can you execute other code in the console? Try the following different snippets, do they work?

Test 1:

app.alert("Hello World!");

Test 2:

for (var i=3; i>=0; i--) {

  app.alert("Countdown: " + i);

}

If you can make these two snippets work, then the script from above (after fixing the 'deletePage' error) should work as well. If you cannot make both of these snippets to run, then there is a problem with how you are trying to execute code in the console.

Translate
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
Guest
Apr 19, 2017 Apr 19, 2017

Thanks Karl ! It was the syntax error issue. It worked !!!

try67 - Appreciate all your help working with me on this.  Thank You !

Translate
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
Community Expert ,
Apr 19, 2017 Apr 19, 2017

Oops, my bad. Thanks for catching it, Karl!

Translate
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
Guest
Jun 07, 2017 Jun 07, 2017

Karl Heinz Kremertry67

Hi,

I wanted to pick your brain to see if there were any suggestions on improving the code.   It currently works well on small documents (5k pages), but when we have large documents 25k pages. It just hangs up and never complete's (even if left overnight)

One idea I had was to prompt the user for a range of page numbers to remove the watermarks.  That way large documents could be done in batches of 5k pages.  Below is a copy of the original working code.

Any thoughts or other ideas would be appreciated .

=================================================================

var j= 0;

pagesLoop: 

for (var p=this.numPages-1; p>=0; p--)

    if (this.numPages==1) break pagesLoop;

    var aOCGs = this.getOCGs(p); 

    if (aOCGs!=null && aOCGs.length>0) { 

        for (var i=0; i<aOCGs.length; i++) { 

            if (aOCGs.name == "Watermark") { 

                this.deletePages(p);

               j=j+1;

continue pagesLoop;

            }

        }

   }    

}

  app.alert(j + " pages were deleted", 3,0);

Translate
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
Community Expert ,
Jun 07, 2017 Jun 07, 2017

With that many pages you are pushing Acrobat's JavaScript to it's limits. If this is something you will do on a regular basis, I would recommend to move to a custom Acrobat plug-in or a standalone solution. If you are interested in exploring such a solution, feel free to contact me via email or a direct message here. Contact information is on my profile page.

Translate
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
Community Expert ,
Jun 07, 2017 Jun 07, 2017

Yes, running scripts on very large files is a recipe for problems... Your idea of doing it in batches is probably the most viable one.

Another approach I can think about is not to delete the pages directly but store their numbers in an array, and later on delete groups of pages, instead of one a time. For example, if you need to delete pages 1, 2, 3, 4, 5 and 7, it makes more sense to first delete page 7 and then pages 1-5 (as a group), instead of each page separately.

Translate
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
Community Expert ,
Jun 07, 2017 Jun 07, 2017

Forgot to mention the other option: To do it using a stand-alone application (or plugin), which is much more powerful than a script, but also more complicated to develop. If you're interested I've created similar stand-alone tools in the past and could certainly develop one for you that does this task, for a small fee. You can contact me directly at try6767 at gmail.com.

Translate
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
Community Expert ,
Jun 07, 2017 Jun 07, 2017

I see KHK was quicker than me in replying... Sorry for the double-post.

Translate
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
Guest
Jun 07, 2017 Jun 07, 2017

Thank You Karl Heinz Kremer​ and try67

Since it is quite rare that the file is large or that we run this script.   I wanted to investigate the option of doing it in batches.   This is where we would prompt the user for a range of page numbers and then use that range to run the script.   So, the occasional large file might have to be ran 4 or 5 times, but that would be ok.

Any thoughts on how capture a range of pages from the user ?   Thanks !

Translate
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
Community Expert ,
Jun 07, 2017 Jun 07, 2017
LATEST

You can use a custom dialog object or even the app.response command. The latter is much simpler to use... Just remember to convert the value they enter to numbers and reduce one from it before using it in your code (because page numbers are zero-based).

Translate
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