Add watermark to PDF

Hi

I am trying to add a watermark to existing pdf’s in 10.4.11 & 10.5.7 using Adobe Acrobat Professional (8.1.3), having the watermark appear on top of page, show when printing, selecting a file source, fitting to page.

I have searched for an applescript solution, but have found only one, I do not no if doing a shell script is possible as I do not no shell scripting.

I have run into this link which uses pdftk

http://www.accesspdf.com/pdftk/index.html#docs

I have installed on 10.4.11 hoping it would work, but it appears not to as it looks like it was only set up for Panther (i could be wrong).

I also found this link

http://discussions.apple.com/message.jspa?messageID=676855

which is what I was wanting, an applescript to run the above command to add a water mark, but this also appears not to work.

Does anybody no of and applescript, shell script solution to do what I am after please.

cheers
Budgie

Hi Budgie,

I went down the same road as you but to no avail. Eventually my company had to put there hand in there pocket and purchase PDFpen. This is a great little app and is also scriptable. Below is a handler I wrote to scale an image to the full width of a PDF document.

set myfile to choose file --> choose the PDF you want to add a watermark to
set WaterMark_Path to "FTP_FileCheck:config_files:AM_Watermark2.pdf" as alias --> insert the path to your watermark pdf (I made a pdf with trensparenvy so it doesn't obscure the main pdf)

my add_watermark(myfile, WaterMark_Path)

on add_watermark(myfile, WaterMark_Path)
	tell application "PDFpen"
		activate
		open (myfile as alias)
		tell document 1
			repeat with i from 1 to count pages
				
				set doc_width to width of page i
				set orig_doc_height to height of page i
				
				set the_increase to doc_width * 100 / 4608
				set doc_height to the_increase / 100 * 225
				
				set pos_y to orig_doc_height / 2 - (doc_height / 2)
				
				set imp to make new imprint with properties {path:WaterMark_Path, x position:1, y position:pos_y, width:doc_width, height:doc_height} at beginning of imprints of page i
			end repeat
		end tell
		close document 1 saving in (myfile as string)
	end tell
end add_watermark

Thanks,
Nik

You could always have a go calling in some JavaScript? This works just fine for me.

set Watermark to "this.addWatermarkFromFile({cDIPath: '/Users/marklarsen/Desktop/WaterMark.pdf', nSourcePage: 0, nStart: 0, nEnd: 0, nHorizAlign: 1, nVertAlign: 3, nHorizValue: 0, nVertValue: -288, nRotation: 0});"

tell application "Adobe Acrobat 7.0 Professional"
	activate
	tell active doc
		do script Watermark
	end tell
end tell

Ditto Nik my watermark is already a transparent PDF.

Edit just to check went back and added the ‘ontop’ and ‘opacity’ parameters and it appears to work also.

set Watermark to "this.addWatermarkFromFile({cDIPath: '/Users/marklarsen/Desktop/WaterMark.pdf', nSourcePage: 0, nStart: 0, nEnd: 0, nHorizAlign: 1, nVertAlign: 3, nHorizValue: 0, nVertValue: -288, nRotation: 0, nOpacity: .5, bOnTop: true,});"

tell application "Adobe Acrobat 7.0 Professional"
	activate
	tell active doc
		do script Watermark
	end tell
end tell

Thankyou blend3 and Mark67 great stuff really appreciated

cheers

Budie

Hi

This works great using Adobe Acrobat 7.0 Professional and Adobe Acrobat Professional on 10.4.11 but when I try to get it to work on intel
10.5.7 using Adobe Acrobat Professional it throws an error saying that Acrobat doesn’ t understand the do script, can some one enlighten me as to what may be wrong please.

set Watermark to "this.addWatermarkFromFile({cDIPath: '/Macintosh HD/Applications/water mark-2.pdf', nSourcePage: 0, nStart: 0, nEnd: 0, nHorizValue: 0, nVertValue: 0, nRotation: 45, nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});"

--tell application "Adobe Acrobat 7.0 Professional"
tell application "Adobe Acrobat Professional"
	activate
	tell active doc
		do script Watermark
	end tell
end tell

cheers

Might be the way Acrobat wants Unicode text, which sometimes is proper and sometimes is not when it’s only an implied coercion into either a string or Unicode text. Where you set the ‘Watermark’ variable add ‘as string’ or ‘as Unicode text’ at the end. Also might want to try a different variable assignment, just in case “Watermark” is reserved as an object or function by one or more applications.

set myWatermark to "this.addWatermarkFromFile({cDIPath: '/Macintosh HD/Applications/water mark-2.pdf', nSourcePage: 0, nStart: 0, nEnd: 0, nHorizValue: 0, nVertValue: 0, nRotation: 45, nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});" as Unicode text

If that’s not it, the it’s when setting posix path strings as Unicode text from a Mac alias string, which is typically a problem for 10.4 not 10.5 and you’ve literally defined the posix path anyway, but it can’t hurt. I found an old post here that contained a gem I use and abuse regularly to for certain make a quoted form of a POSIX path string a Unicode string compatible with Acrobat’s ‘do script’ command:

on javaPOSIXpath(stringPath)
	try
		set {text:stringPath} to (quoted form of POSIX path of stringPath) as string
		return stringPath
	on error
		error "javaPOSIXpath() failed!"
	end try
end javaPOSIXpath

Hi Pandrake

thanks for the explanation and suggestion but neither as string or as Unicode text seem to be the trick, it keeps throwing the same error,
I also changed the variable as suggested, with no luck.

I am not quite sure how to implement the javaPOSIXpath code.

cheers

Dunno. I’m on 10.4 with Acrobat Pro 8, so it was a shot in the dark that it applied to your instance. Sounded the same as problems I had and information I found revolving around Unicode text and the differences between Applescript strings in 10.4 or 10.5, plus the differences between Acrobat 7, 8, and 9. I’ve been scripting for quite a while but pre-X, and have boggled for only a year over this javascript and one-code to rule them all stuff.

I typically build the javascript sent to ‘do script’ out of strings and variables concatenated into one string of text, and had problems whenever I’d try to use the quoted form of a POSIX path - I’d get the same “doesn’t understand.” error. It was something about the Unicode text class not being properly implemented in 10.4 (I think), and I found the 'set {text:stringPath} to." coercion method that did the trick. Whenever I want an alias path to a file as a string inserted as a proper, Unicode text POSIX path, I call the handler. For example:

set javascriptBegin to "this.addWatermarkFromFile({cDIPath: "
set filePath to (choose file) as string
set filePosixPath to my javaPOSIXpath(filePath)
set javascriptEnd to ", nOpacity: .1);"
set javaCode to javascriptBegin & filePosixPath & javascriptEnd as string
--then use javaCode in Acrobat with 'do script'

on javaPOSIXpath(stringPath)
	set {text:stringPath} to (quoted form of POSIX path of stringPath) as string
	return stringPath
end javaPOSIXpath

[edit]

Now that I think about it, I’m remembering that the problem had to do specifically with only the posix paths, because when I built the whole string and tried to coerce it into the proper class of text it’d fail no matter which method I used, but if I ran just the POSIX path strings thru that coercion Acrobat would allow me to compile strings for a “do javascript” as expected.

I have set your code up as below, and have run it from 10.5.7 & 10.4.11 with the same result, thought id post the error to see if you think
something is a miss.

Error

tell current application
	choose file
		alias "Preflight:Applications:watermark3.pdf"
end tell
tell application "Adobe Acrobat Professional"
	activate
	do script "this.addWatermarkFromFile({cDIPath: '/Applications/watermark3.pdf', nOpacity: .1);"
		"Adobe Acrobat Professional got an error: \"this.addWatermarkFromFile({cDIPath: '/Applications/watermark3.pdf', nOpacity: .1);\" doesn't understand the do script message."
set javascriptBegin to "this.addWatermarkFromFile({cDIPath: "
set filePath to (choose file) as string
set filePosixPath to my javaPOSIXpath(filePath)
set javascriptEnd to ", nOpacity: .1);"
set javaCode to javascriptBegin & filePosixPath & javascriptEnd as string
--then use javaCode in Acrobat with 'do script'
tell application "Adobe Acrobat Professional"
	activate
	tell active doc
		do script javaCode
	end tell
end tell
on javaPOSIXpath(stringPath)
	set {text:stringPath} to (quoted form of POSIX path of stringPath) as string
	return stringPath
end javaPOSIXpath

cheers for the help

Bummer the error isn’t more telling; what doesn’t it understand…

Try this (complete code based on your original, with step by step assembly of the javascript string)

tell application "Adobe Acrobat Professional"
	activate
	--file to watermark
	set aFile to (choose file with prompt "Select PDF to watermark")
	set {text:aFile} to (quoted form of POSIX path of aFile) as string
	--watermark file
	set waterMarkFile to (choose file with prompt "Select watermark art file")
	set {text:waterMarkFile} to (quoted form of POSIX path of waterMarkFile) as string
	
	--assemble javascript, first open the file into which the watermark will be placed
	set javaCommand to "var d = app.openDoc({cPath:" & aFile as string
	set javaCommand to javaCommand & ", bHidden:false});" as string
	--add the watermark (broken up for readability)
	set javaCommand to javaCommand & "d.addWatermarkFromFile({cDIPath: " as string
	set javaCommand to javaCommand & waterMarkFile as string
	set javaCommand to javaCommand & ", nSourcePage: 0, nStart: 0, nEnd: 0" as string
	set javaCommand to javaCommand & ", nHorizValue: 0, nVertValue: 0, nRotation: 45" as string
	set javaCommand to javaCommand & ", nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});" as string
	
	do script javaCommand
	
end tell

It works for me on OS 10.4.11 with AppleScript 1.10.7 and Adobe Acrobat Professional 8.1.5

Like you, it works like a dream using 10.4.11 & Applescript 1.10.7 but unfortunately when testing it on 10.5.7 using Applescript 2.0.1 it fails
throwing the same error as before.

tell application "Adobe Acrobat Professional"
	activate
	choose file with prompt "Select PDF to watermark"
		alias "Macintosh HD:Users:admin:Desktop:PDF TEST FILES:PDF TEST FILES.pdf"
	choose file with prompt "Select watermark art file"
		alias "Macintosh HD:Applications:water mark-2.pdf"
	do script "var d = app.openDoc({cPath:'/Users/admin/Desktop/PDF TEST FILES/PDF TEST FILES.pdf', bHidden:false});d.addWatermarkFromFile({cDIPath: '/Applications/water mark-2.pdf', nSourcePage: 0, nStart: 0, nEnd: 0, nHorizValue: 0, nVertValue: 0, nRotation: 45, nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});"
		"Adobe Acrobat Professional got an error: \"var d = app.openDoc({cPath:'/Users/admin/Desktop/PDF TEST FILES/PDF TEST FILES.pdf', bHidden:false});d.addWatermarkFromFile({cDIPath: '/Applications/water mark-2.pdf', nSourcePage: 0, nStart: 0, nEnd: 0, nHorizValue: 0, nVertValue: 0, nRotation: 45, nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});\" doesn't understand the do script message."

cheers

Major bummer, especially since my department is planning on getting a new mac with a new Acrobat for doing this kind of thing, and it looks like another solution must be found (or at least figure out why it’s different on the newer setup).

I’m very uncertain why and what the problem is in the first place, with a string on one system is not a proper string or class of text on another, and if it’s Acrobat that choking or AppleScript or the OS or the hardware (Intel or PPC) and all that mix of possible problems. I only know that the javascript syntax is correct (it does what it’s supposed to on 10.4 Intel with Acrobat Pro 8), but something about it’s text class isn’t completely correct. And maybe that’s not the problem and it’s something else about Acrobat’s implementation of ‘do script’ or maybe even it’s javascript API has changed.

Does putting one of these three before the ‘do script’ make it work?

	set {text:javaCommand} to javaCommand as Unicode text
	set {text:javaCommand} to javaCommand as string
	set javaCommand to javaCommand as Unicode text

If not then either it’s not a class issue, or AppleScript ain’t right, or Acrobat ain’t right, or I don’t understand coercion of classes. I’ve seen several posts here about the behavior of AppleScript changing from version 1.10.7 to 2.0.1 with regard to the Unicode text class, so I bet it’s there (but wouldn’t bet much since I really don’t know how the game is played).

looks like we have struck a dead end with this pandrake, as the new code on 10.5.7 still delivers the same error, I guess ill have to re-think how im going to achieve what I am after.

Thanks for all your suggestions and time, much appreciated

cheers
Budgie

No problemo - I’m self-serving as I’ll be where you’re at a soon as new equipment/software arrives. =(

Are both machines running the same version of Acrobat Pro?

[edit]
or a workaround might be to write the javascript to a temporary text file and pipe it in to Acrobat that way instead of with ‘do script’ in applescript? read it back in after it’s been saved as text and then read as text forcing it into something that ‘do script’ does accept?

Heck - does the ‘do script’ on 10.5 even work in Acrobat at all; like this?

tell application "Adobe Acrobat Professional"
	activate
	do script "var d = app.openDoc({cPath:'/Users/admin/Desktop/PDF TEST FILES/PDF TEST FILES.pdf', bHidden:false});"
end tell

I’m also wondering if the spaces in the posix path are weirding it out - thought the “quoted form of…” thing took care of that, tho.

yeah both 10.4.11 & 10.5.7 are running Adobe Acrobat Professional 8.1.3

keep me posted if you find a solution down the track,

shout ya a beer if your down my way one day :smiley:

Whoa. Just noticed you’re in New Zealand - my favorite dream destination! I’ve finished with becoming famous (or infamous), so my next goal is to become rich so I won’t have to swim it over and you can be certain I’ll take you up on that. In fact, I’ll be the one treating you to a beer since you’ve helped me (and probably others) flush out the bugs and workarounds and gotchas with the interplay of application and system versions not readily available for testing myself. It’s the beauty of this board, and takes me back to the beginning of infamy when I wrote news stories for the front page.

Hi Ray!

Anyhoo…

Therein lies the rub, methinks. I’m 99% certain that the problem is with the text class of javascript strings being sent to Acrobat 8’s do script action, which is supposed to be Unicode text but really it only works if it’s a [plain] text class. Or maybe either will work but it all has to be the same. Problem I was having was compiling the string of text - worked fine until I put in something that was Unicode text, which is what I got with 'quoted form of POSIX path of…" on my file and folder variables. All the rest of my strings were just text, and it worked fine even when literally writing out the POSIX paths and quoting them, but every time I coerced a Finder path (alias form or other) into a POSIX path it’d fail. This is with AppleScript 1.10.7 which is what I get with Tiger (OSX 10.4.11).

class of (quoted form of POSIX path of (choose file))
-->Unicode text

class of ("This is " & "just a string" as text)
-->string

In walks AppleScript 2.0.1 with OSX 10.5.7 (Leopard), and according to Nigel Garvey the big deal here is that all text (‘string’, ‘text’, or ‘Unicode text’) is Unicode text while it’s being held or handled within a script. The workaround I used with AppleScript 1.10.7 was to get rid of the Unicode text class on the POSIX paths allowing all strings to be [plain] text strings when fed to Acrobat. The post is here:

http://macscripter.net/viewtopic.php?pid=36059#p36059

I bet that the above script will use a different class for the string when sent as text to Acrobat’s do script action. While I couldn’t test whether or not his method for coercing a string’s class into proper shape will work on AppleScript 2.0.1, I’m betting you have answered that question. It does not (unless the method is applied to the final string, not just the file paths, and it works).

So that I keep all that I found searching this BBS for relevant posts, here’s a couple others mention the same problem - tho they aren’t panning out with initial tests either, and have more to do with AppleScript 1.10.7 and earlier, as well as Acrobat 6, not 8.

http://macscripter.net/viewtopic.php?pid=19702
http://macscripter.net/viewtopic.php?id=5280
http://macscripter.net/viewtopic.php?pid=24100

It’s really frustrating, because this works:

class of ("any string" as «class utf8»)

But neither of these work

class of ("any string" as «class ktxt»)
class of ("any string" as Unicode text as record) as «class ktxt»

It’s supposed to work, or at least it used to work for other folk when trying to coerce Unicode text into a ktxt class. Maybe it’s now a different «class xxxx» code for regular 8-bit text or something that works with AppleScript 2.0.1. There’s also a trick of writing the string to a file, and then reading it back in as a certain class.

set x to read (choose file) as «class ktxt»
class of x

However, even tho that doesn’t error on me it results in a weird class for x that I don’t know is going to work for sending a string to Acrobat, or if the class of the string will be maintained by AppleScript 2.0.1 when it does.

Dunno if it’s a solution, but I can think of a way you can test different attempts at solving your dilemma of watermarking a PDF using do script and javascript (which will help me solve the same dilemma in about a year when I’m faced with new machines). There’s gotta be a way to coerce AppleScript 2.0.1 strings into 8-bit text, since it appears that’s what Acrobat 8 wants for it’s ‘do script’ action.

tell application "Adobe Acrobat Professional"
	activate
	--file to watermark
	set aFile to (choose file with prompt "Select PDF to watermark")
	set aFile to (quoted form of POSIX path of aFile) as string
	--watermark file
	set waterMarkFile to (choose file with prompt "Select watermark art file")
	set waterMarkFile to (quoted form of POSIX path of waterMarkFile) as string
	
	--assemble javascript, first open the file into which the watermark will be placed
	set javaCommand to "var d = app.openDoc({cPath:" & aFile as string
	set javaCommand to javaCommand & ", bHidden:false});" as string
	--add the watermark (broken up for readability)
	set javaCommand to javaCommand & "d.addWatermarkFromFile({cDIPath: " as string
	set javaCommand to javaCommand & waterMarkFile as string
	set javaCommand to javaCommand & ", nSourcePage: 0, nStart: 0, nEnd: 0" as string
	set javaCommand to javaCommand & ", nHorizValue: 0, nVertValue: 0, nRotation: 45" as string
	set javaCommand to javaCommand & ", nOpacity: .1, bOnTop: false, bOnScreen: false, nScale: 1.5});" as string
	
	--try different coercions
	--simple
	set javaCommandA to javaCommand as text
	display dialog (class of javaCommandA as string)
	do script javaCommandA
	
	--Nigel's method
	set {text:javaCommandB} to javaCommand as text
	display dialog (class of javaCommandB as string)
	do script javaCommandB
	
	--other direct method?
	set javaCommandC to javaCommand as «class ktxt» --this doesn't work at all on AS 1.10.7
	do script javaCommandC
	
	--write to/read in method
	set tempJavaFile to (choose file name default name "tempJavaText")
	set tempFileReference to (open for access tempJavaFile with write permission)
	write javaCommand to tempFileReference --as text??
	close access tempFileReference
	set javaCommandD to read tempJavaFile --as text?? as «class ktxt»??
	do script javaCommandD
	
end tell

Model: iMac (Intel)
AppleScript: 1.10.7
Browser: Safari 525.13
Operating System: Mac OS X (10.4)

looks like were in for a bit of a session if your shouting to :lol:

Just ran all the suggestions on 10.4.11 all work well bar the one that you pointed out that doesn’t, switched over to 10.5.7 iMac Intel AppleScript 2.0.1 and ran all the suggestions again all came back with the original error, I agree with you this is @#$$% frustrating.

Ill keep you posted if I manage to dig up something of use, again cheers for your time and input, some very valuable info has gone down here.

Thanx muchas for testing those - looks like AppleScript TNG needs a little help with getting it’s string theory adapted to misleading requirements (Adobe sez Unicode is needed, but apparently it’s Unicode that fails). Days like these I wish I studied CS instead of art and literature. I’m hopeful that someone creative will find a way to coerce strings into the proper form for Acrobat, especially since I’ve noticed that all Adobe products have favored Javascript over AppleScript.

Meh. I’d rather be frustrated now rather than later. Beat the rush. Hope there’s a solution soon for your workflow. I might be looking into the Adobe boards for solutions plus alternatives to javascript soon but on first blush it looks like we’ve got no choice. Plus, either is a lot cleaner than a GUI script.

Thanks again, Budgie. =)
Captain out!

I have posted at Adobe Acrobat’s scripting forum, heres the link just in case a solution or leads to a solution relating to this issue arise.

http://forums.adobe.com/thread/446762?tstart=0

Sweeeeeeeeet. Thanks for the poke. I’ve let this issue slide a bit as I’m nearing a massive deadline for a catalog and the PDF stuff is still a ways away. I’ll keep an eye out, too; soon as there’s something that works on AS 2.0 I’ll test it on AS 1.7 to see what’s to be done for both.

Thanks again, mon. Mahsive cool of ya.