How to add watermarks to PDF using C#

As I mentioned in a previous post about adding your own converters using our fancy new plug-in architecture, we prioritise new functionality based on the number of customers requesting a particular feature. The feature I will be discussing today is one that has been on our radar for a long time, Watermarking.

We have previously provided some sample code to watermark documents using our Workflow Power Pack and although that solution is working well for many of our customers, it was only an interim solution that doesn’t come close to providing the functionality in our new watermarking engine, which is totally awesome

Note that at this moment we have only added native support for watermarking to the back-end conversion engine. Although it can be accessed using the Workflow Power Pack, we have not yet released a dedicated Workflow Action to control watermarking from SharePoint, which will be released in the next version.

Right, so back to the topic at hand. Listed below is an overview of our new Watermarking architecture as well as some sample code. More information and a full description of the object model can be found in the PDF Converter Services’s comprehensive User & Developer Guide. Note that you need version 3.5 or newer to make use of the new watermarking functionality.


The Muhimbi Document Conversion Service contains a very flexible system for applying watermarks to documents. Multiple watermarks can be applied to the same page and watermarks can be applied to page ranges or certain page types such as odd, even, portrait or landscape.

Watermarks are passed as part of the ConversionSettings object, a parameter of the Convert method. An overview of the watermarking related Web Services interface is provided below. For full details see the full User & Developer Guide.

ClassDiagram-Watermarking

Individual Element Types

As all individual elements / shapes inherit from the Element class, they largely share the same properties.

The supported element types are as follows.

  • Line: Represents a single line.

  • Rectangle: Represents a rectangle..

  • Ellipse: Represents an ellipse.

  • Rtf: Represents a piece of text encoded in RTF format.

  • Image: Represents an image. The following image types are supported: BMP, JPG, GIF, PNG, TIFF, WMF, EMF and EMF+.

  • Pdf: Represents an existing PDF file that is used as the watermark.

  • Text: Represents a text box that allows plain text to be specified with full control over horizontal and vertical alignment, font face and size as well as word wrapping. This field also allows field codes such as page number to be embedded (see below).

Embedding merge fields in the Text element

The Text element allows merge codes to be embedded such as the number of pages or the current date. This makes it very simple to use watermarks to automatically generate headers and footers on each page, while taking orientation and page interval (Odd / Even pages) into account.

The following merge fields are available for use:

  • {LONG_DATE}: The long representation of the current date.
  • {LONG_TIME}: The long representation of the current time.
  • {DATE}: The short representation of the current date.
  • {TIME}: The short representation of the current time.
  • {PAGE}: The number of the current page in the PDF file.
  • {NUMPAGES}: The total number of pages in the PDF file.

Date and time fields are formatted using the locale used by the account the Document Conversion Service is running under. More details about merge fields is available here.

Sample Code

As described previously, the PDF Conversion Service contains a powerful watermarking engine that can be used to add visible and invisible watermarks to pages as well as adding headers, footers and other recurring items.

The following C# example shows how to decorate a document with the following watermarks:

  1. Add the word ‘Confidential’ in the background of the cover page.
  2. Add page numbers in the right-hand side of the footer as well as a horizontal line to all even pages.
  3. Add page numbers in the left-hand side of the footer as well as a horizontal line to all odd pages.

Watermarking

The sample code expects the path of the PDF file on the command line. If the path is omitted then the first MS-Word file found in the current directory will be used.

Follow the steps described below to create the sample watermarking application. Please note that this sample code is also available via the Windows Start Menu.

  1. Create a new Visual Studio C# Console application named Watermarking.

  2. Add a Service Reference to the following URL and specify ConversionService as the namespace

    http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl

  3. Paste the following code into Program.cs.

  4. Make sure the output folder contains an MS-Word file.

  5. Compile and execute the application.

As all this functionality is exposed via a Web Services interface, it works equally well from Java and other web services enabled environments.

This code is merely an example of what is possible, feel free to adapt it to you own needs, add more complex watermarks or your company logo to each page. The possibilities are endless.

Labels: Articles, pdf, PDF Converter, PDF Converter Services, Watermarking

Have a Question?
We’re Always Happy to Help.

© Muhimbi Ltd. 2008 - 2024
This website uses cookies to ensure you get the best experience. Learn more