Copy link to clipboard
Copied
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.
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
...Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Not sure what you mean... That is the way of doing that.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Almost, but not quite... Post your code as text and I'll help you fix it.
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 ?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Tried it console window and got a syntax error. See attached screen shot.
Copy link to clipboard
Copied
You need to select the full code with the mouse or keyboard (Ctrl+A) before executing it.
Copy link to clipboard
Copied
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 !
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks Karl ! It was the syntax error issue. It worked !!!
try67 - Appreciate all your help working with me on this. Thank You !
Copy link to clipboard
Copied
Oops, my bad. Thanks for catching it, Karl!
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I see KHK was quicker than me in replying... Sorry for the double-post.
Copy link to clipboard
Copied
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 !
Copy link to clipboard
Copied
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).
Find more inspiration, events, and resources on the new Adobe Community
Explore Now