Reply
Golden
emarcus
Posts: 232
Registered: 05-28-2010
0

Watermarking PDF documents

[ Edited ]

(Originally posted March 21, 2011)

 

About this extension

  • Author:       Owen Garrett
  • Released:   21/03/2011 - 9:10am
  • License:      BSD

 

Content 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 extension contains a Java module that can be used to uniquely watermark and encrypt every PDF document as it is served from your web site.

 



For more information, please refer to the article ‘Watermarking PDF documents dynamically’.

 

Installing the Extension

 

Requirements:

 

This extension has been tested against Java 1.6 and Zeus Traffic Manager 7.1.

 

Installation:

 

Download the extension and extract the PdfWatermark.jar file.  This contains the Zeus Java Extension and all the third-party dependencies.

 

Upload the PdfWatermark.jar file to your Java Extensions catalog:

 

pdf2

 

Add the enclosed PdfWatermark.zts rule as a response rule to your virtual server (or copy-and-paste one of the code samples in the article below):

 

pdf2b

 

Testing the extension

 

Download a PDF document from your website, managed by the virtual server configured above.  Verify that the PDF document has been watermarked with the URL, client IP address, and time of download.

 

Troubleshooting

 

The Java Extension applies the watermark to PDF documents, and then encrypts them to make the watermark difficult to remove.

 

The Java Extension will not be able to apply a watermark to PDF documents that are already encrypted, or which are served with a mime type that does not begin ‘application/pdf’.

 

Customizing the extension

 

The behaviour of the extension is controlled by the parameters passed into the Java extension by the ‘java.run()’ function.

 

The following example applies a simple watermark:

 

    if( http.getresponseheader( "Content-Type" ) != "application/pdf" ) break;

    $msg1 = http.getHostHeader() . http.getPath();
    $msg2 = "Downloaded by ".http.getRemoteIP();
    $msg3 = sys.gmtime.format( "%a, %d %b %Y %T GMT" );

    java.run( "PdfWatermark",
       "drawText", $msg1,
       "drawText", $msg2,
       "drawText", $msg3,
    );

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

 

pdf3

 



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.

 

 

 test

 

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 ".sys.time.year(),
       "drawText", "For restricted distribution"
    );

 

Dependencies and Licenses

 

For convenience, the .jar extension contains the iText 5.0.6 library from iText software corp (http://www.itextpdf.com) and the bcprov-146 and bcmail-146 libraries from The Legion of the Bouncy Castle (http://www.bouncycastle.org), in addition to the Zeus Java Extension.

 

Alternatively, you may upload the enclosed PdfWatermark.class file and suitable version of the iText, bcprov and bcmail libraries.

 

Source code for the Zeus Java Extension is provided in the included PdfWatermark.java file.



Download this extension below:

‬‪‬‪‬‪