Add watermark to PDF

I’m not sure if this is relevant. please forgive me if it’s not.

This works for me with 10.5.7 and acrobat pro 9.1.2:


tell application "Finder"
	set myFolder to (container of mypath) as string
	set myFolderPOSIX to POSIX path of ((container of (path to me)) as alias)
end tell

set myDesktop to path to desktop as string
set FormattedPDF to myDesktop & "formatted.pdf"
set WMscript to "this.addWatermarkFromFile('" & myFolderPOSIX & "watermark.pdf');"

tell application "Adobe Acrobat Pro"
	activate
	open FormattedPDF
	do script WMscript
end tell

Hope this helps,
Jeremiah

Indeed it helps, Jeremiah! Many thanks. It’s encouraging, since it looks like Acrobat’s problem with Unicode text is solved in version 9. This bodes well for my impending introduction of 10.5 and Acrobat Pro, but it leaves me with a need to upgrade all instances of Acrobat 8 if I upgrade the Macs from 10.4 which I don’t want to do if I don’t have to. Bah!

I have been doing some digging, and think I have found a solution that works for both Tiger and Leopard, tho I can only test it on Tiger. It uses an “osaxen” type application from has:

http://osaxen.com/files/textcommands1.1.3.html

Neato! Much easier than trying to figure out textutil and -stdout or various terminal commands and encodings or resorting to file writing and reading… Here’s an example of what should work on both Tiger and Leopard with Acrobat Professional 8.

set aPDFfile to (choose file of type {"com.adobe.pdf"})

set pdfPosixPath to quoted form of POSIX path of aPDFfile
--the posix path string is always Unicode text
--even when combined with ascii strings (8-bit) in Tiger
--solution was to use hack by Nigel, but doesn't work on Leopard
set pdfPosixPath to my NigelFix(pdfPosixPath) --comment out if not needed

--the following string cannot be classed as Unicode text for Acrobat Pro 8
set javaCmd to "d = app.openDoc({cPath:" & pdfPosixPath & ", bHidden:false});" as string
--javaCmd is combination of ascii strings and Unicode text in Tiger
--javaCmd is all Unicode text in Tiger
--fails when passed to Acrobat 8's do script

--faceless app called "TextCommands" will do the trick
--to the entire string, apparently
set javaCmd to my txtCmdsCnvt(javaCmd)

tell application "Adobe Acrobat Professional"
	do script javaCmd
end tell

--Nigel's solution to Unicode text => ascii text problem in Acrobat8's do script command
on NigelFix(unicodeText)
	set {text:unicodeText} to unicodeText as string
	return unicodeText
end NigelFix

--TextCommands installed in Scripting Additions
on txtCmdsCnvt(unicodeText)
	tell application "TextCommands"
		return convert from unicode unicodeText
	end tell
end txtCmdsCnvt

Did you know Automator has an action to watermark PDF’s? You just need to set it up with an image to use as the watermark. It has several controls to help you resize, rotate, and lighten the watermark image.

Note: the action has a flaw. There are controls which allow you to rotate the watermark image which don’t work… or so it seems. When you enter a rotation amount automator seems to lock-up and stop working. What I found is just type the rotation amount into the box even though you can’t see what you are typing because of the seemingly lock-up situation. Then you can press command-s to save the automator workflow and then command-q to quit automator. Then when you re-open your automator action you will see that the rotation angle you entered has been saved. So you need to do this routine a couple times until you get the action setup how you want.

Ya, I looked into it originally and it seemed easy enough to process files from a Folder Action attached to a NAS folder with Automator, tho not entirely straightforward. To be honest I’ve not delved into Automator much at all - in the context of scripting I’m a Automator newbie and see it as more macro/user oriented than batch/production oriented.

My case is where I have one Mac to serve custom file processing on PDFs and other formats to the entire company, not just the Macs. Watermarking is only one automated workflow, and the others are only possible with Acrobat Pro, and especially its javascript API, that become even more elaborate if I can mix it and other applications together with AppleScript (even stuff on a couple of very old Macs in concert with one relatively new Mac). Makes it cheaper and easy-to-train solutions for users who don’t need to know the in’s and out’s of graphic art and AppleScript, just drop dot-files into dat-folder. Plus, I’m planning on scaling the watermarking feature to include variable data, which is more easily achieved with text watermarking than image watermarking.

Thanks for the tip on making the prefab watermarking bug. I can see it of use for local Mac use if it doesn’t change the RIP or security of the PDF other than adding a watermark (I presume it uses either print2pdf or quartz something??).

Yep, it does the trick, but the rendering can be weird sometimes : PDF in Automator are treated by Apple Quartz. It results in bad transparency if you deal with complex PDFs… I tried everything with Indesign PDF presets (make a PDF 1.3 or 1.4 etc. and other settings…) without getting 100% of good PDFs when I add watermaks in Automator.

I’d like doing it with javascript and Acrobat, unfortunately the website to get “TextCommands” is down for instance.

Ok I found this and It works in Acrobat 9.4.1 :

tell application "Adobe Acrobat Pro"
		tell active doc
		set The_String to "watermark"
		my Add_WaterMarkText(The_String, 1, 1, 1, 1, 3, 0, 0, 0)
		do script the result
	end tell
end tell
-- 
on Add_WaterMarkText(cText, nS, nE, nTA, nHA, nVA, nHV, nVV, nR)
	set Params to "cText: '" & cText & "'," & return
	if class of nS is integer then set Params to Params & "nStart: " & (nS - 1) & "," & return
	if class of nE is integer then set Params to Params & "nEnd: " & (nE - 1) & "," & return
	-- 0 = left, 1 = centre, 2 = right 
	if class of nHA is integer then set Params to Params & "nHorizAlign: " & 0 & "," & return
	-- 3 = top, 4 = bottom 
	if class of nVA is integer then set Params to Params & "nVertAlign: " & 4 & "," & return
	-- offset in points 
	if class of nHV is integer then set Params to Params & "nHorizValue: " & nHV & "," & return
	-- offset in points 
	if class of nVV is integer then set Params to Params & "nVertValue: " & nVV & "," & return
	if class of nR is integer then set Params to Params & "nRotation: " & 90 & "," & return
	set JavaScript to "this.addWatermarkFromText({" & Params & "});"
	return JavaScript
end Add_WaterMarkText

But how to do the same from a File ?
Thank you !

Do somebody know when it is available again ?
Thank you !

Hey !
The script below works for Snow Leopard 10.6.8 and Acrobat Pro 9.4.5.


set watermark_pdf to "path:to:your:watermark.pdf"
	set watermark_pdf to quoted form of POSIX path of watermark_pdf
	set watermark_javascript to "this.addWatermarkFromFile({
 cDIPath: " & watermark_pdf & ",
 nHorizAlign: app.constants.align.left,
 nVertAlign: app.constants.align.bottom,
 nHorizValue: 0, nVertValue: 0,
 nRotation: 0,
 nScale: -0.33
 });"
		
	tell application "Adobe Acrobat Pro"
		do script watermark_javascript
	end tell



it’s a beautiful thing alitaliano, great stuff, glad to see it’s finally fixed

Budgie

Hey,
it doesn’t work anymore in Acrobat X (with 10.7 Lion) :rolleyes:, any solution ?
Thank you !