Watermarking PDF documents dynamically

PDF WatermarkingContent protection is a key concern for many online services, and watermarking downloaded documents with a unique ID is one way to discourage and track unauthorized sharing. This article describes how to use ZXTM to uniquely watermark every PDF document served from a web site with the details of each client.

In this case, the ZXTM Traffic Manager will run a Java Extension to process all outgoing PDF documents from the web sites it is managing. The Java Extension can be easily configured to watermark each download with a custom message, including details such as the IP address, time of day and authentication credentials (if available) of the client.

Installing the Java Extension

Download the PDF Watermark Java class file, along with the following three third-party Java libraries:

Upload the PdfWatermark.class class file and the three jar files to your Java Extensions catalog. The class file will be treated as a Java Extension, and the catalog will treat the jar files as libraries:

Note that the third party libraries require JRE version 6; this extension will not function when the ZXTM system has JRE 5.

Compiling the Java Extension (optional)

If you prefer, you can compile the Java Extension from the source file: PdfWatermark.java. Compiling a Java Extension can be quite involved; for detailed instructions, refer to the Image Watermarking article.

The compilation will need to be linked against the iText jar file (as well as the zxtm-servlet and servlet jar files); the other jar files are dependencies of iText to encrypt and seal the PDF document, and are only needed at runtime.

Using the Java Extension

To use the PdfWatermark Java Extension, you will need to invoke it from a TrafficScript™ response rule and pass it a list of commands to write the watermark text on every page of the PDF document. The Java Extension will 'seal' the PDF once it has been processed, setting the encryption parameters so that it cannot be modified (preventing users from removing the watermark).

Here is a simple TrafficScript response rule that writes a basic watermark on any PDF document it processes:

if( http.getresponseheader( "Content-Type" ) != "application/pdf" ) break;
$msg1 = "Downloaded by ".request.getRemoteIP();
$msg2 = "at ".sys.gmtime.format( "%a, %d %b %Y %T GMT" );
java.run( "PdfWatermark",
"drawText", $msg1,
"drawText", $msg2
);

When a user downloads a PDF file from your website, it will be watermarked as follows:

Advanced use of the Java Extension

This Java Extension takes a list of commands to control how and where it applies the watermark text:

Command Notes Default
x As a percentage between 0 and 100; places the cursor horizontally on the page. 30
y As a percentage between 0 and 100; places the cursor vertically on the page. 30
textAngle In degrees, sets the angle of the text. 0 is horizontal (left to right); 90 is vertical (upwards). The special value "auto" sets the text angle from bottom-left to top-right in accordance with the aspect ratio of the page. "auto"
textAlign Value is "L" (left), "R" (right), or "C" (center); controls the alignment of the text relative to the cursor placement. "L"
textAlpha As a percentage, sets the alpha of the text when drawn with drawText."0" is completely transparent, "100" is solid (opaque). 75
textColor The color of the text when it is drawn with drawText, as hex value in a string. "0xAAAAAA"
textSize In points, sets the size of the text when it is drawn with drawText. 20
drawText Draw the value (string) using the current cursor placement and text attributes; automatically moves the cursor down one line so that multiple lines of text can be rendered with successive calls to drawText.

Here's a more sophisticated example that illustrates many of the commands above:

if( http.getresponseheader( "Content-Type" ) != "application/pdf" ) break;
java.run( "PdfWatermark",
"x", 10,
"y", 20,
"textAlpha", 30,
"textSize", 50,
"drawText", "Downloaded by ".request.getRemoteIP(),
"textSize", 30,
"drawText", sys.gmtime.format( "%a, %d %b %Y %T GMT" ),
"x", 60,
"y", 25,
"textAlpha", 70,
"textColor", "0x6666FF",
"textSize", 16,
"textAngle", 0,
"drawText", "Copyright Zeus Technology ".sys.time.year(),
"drawText", "For restricted distribution"
);

This watermarks a document rather like this:

Further Reading

Owen Garrett [Zeus Dev Team] 20 March 2009 Bookmark with del.icio.us Post this article to Digg Post this article to reddit Post this article to Facebook Tweet this article 2 comments  

Comments:

This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.

Comment from: Douglas Spooner [Visitor]
Just curious rather than a watermark would it be possible to add some information as a header/footer to the PDF?
Permalink 23 March 2009 @ 17:33
Comment from: pessi [Member]
For static pdf watermarking, you can use this wonderful tool. Hope it is useful to a lot of people.

http://watermark-images.com/pdf-watermark.aspx
Permalink 07 August 2009 @ 21:09
Leave a comment ...
Your email address will not be displayed.
Your URL will be displayed.
This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)

Recently...

Other Resources