PDFsharp Sample: Watermark

Modified on 2009/08/04 00:00 by Administrator — Categorized as: PDFsharp Samples, Samples

<< Previous^UpNext >>
This sample shows three variations how to add a watermark to an existing PDF file.

Note: Technically the watermarks in this sample are simple graphical output. They have nothing to do with the Watermark Annotations introduced in PDF 1.5.

PDF Output File

See the PDF file created by this sample: Watermark.pdf (112 kB)
This sample requires Acrobat Reader 5.0 or higher because it uses / demonstrates transparency.

Screen Shots

Here are screen shots for the three variations of watermarks implemented in this sample (click image to enlarge):

Draw watermark as text string Draw watermark as outlined graphical path Draw watermark as transparent graphical path above text
Click on the images to enlarge.

Source Code

Here's the code that creates the first kind of watermarks:
// Variation 1: Draw watermark as text string
 
// Get an XGraphics object for drawing beneath the existing content
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
 
// Get the size (in point) of the text
XSize size = gfx.MeasureString(watermark, font);
 
// Define a rotation transformation at the center of the page
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
 
// Create a string format
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
 
// Create a dimmed red brush
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
 
// Draw the string
gfx.DrawString(watermark, font, brush,
  new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
  format);
Here's the code that creates the second kind of watermarks:
// Variation 2: Draw watermark as outlined graphical path
 
// Get an XGraphics object for drawing beneath the existing content
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
 
// Get the size (in point) of the text
XSize size = gfx.MeasureString(watermark, font);
 
// Define a rotation transformation at the center of the page
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
 
// Create a graphical path
XGraphicsPath path = new XGraphicsPath();
 
// Add the text to the path
path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
  new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
  XStringFormats.Default);
 
// Create a dimmed red pen
XPen pen = new XPen(XColor.FromArgb(128, 255, 0, 0), 2);
 
// Stroke the outline of the path
gfx.DrawPath(pen, path);
Here's the code that creates the third kind of watermarks:
// Variation 3: Draw watermark as transparent graphical path above text
 
// Get an XGraphics object for drawing above the existing content
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
 
// Get the size (in point) of the text
XSize size = gfx.MeasureString(watermark, font);
 
// Define a rotation transformation at the center of the page
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
 
// Create a graphical path
XGraphicsPath path = new XGraphicsPath();
 
// Add the text to the path
path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
  new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
  XStringFormats.Default);
 
// Create a dimmed red pen and brush
XPen pen = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));
 
// Stroke the outline of the path
gfx.DrawPath(pen, brush, path);