PDFsharp Sample: Watermark

Modified on 2018/07/11 11:27 by Thomas Hövel — 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.6.

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 a watermark as a text string.
 
// Get an XGraphics object for drawing beneath the existing content.
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
 
// Get the size (in points) of the text.
var 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.
var 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 a watermark as an outlined graphical path.
// NYI: Does not work in Core build.
 
// Get an XGraphics object for drawing beneath the existing content.
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
 
// Get the size (in points) of the text.
var 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.
var path = new XGraphicsPath();
 
// Create a string format.
var format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
 
// Add the text to the path.
// AddString is not implemented in PDFsharp Core.
path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
    format);
 
// Create a dimmed red pen.
var 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 a watermark as a transparent graphical path above text.
// NYI: Does not work in Core build.
 
// Get an XGraphics object for drawing above the existing content.
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
 
// Get the size (in points) of the text.
var 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.
var path = new XGraphicsPath();
 
// Create a string format.
var format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
 
// Add the text to the path.
// AddString is not implemented in PDFsharp Core.
path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
    new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
    format);
 
// Create a dimmed red pen and brush.
var 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);

Note: The samples on this site usually show and discuss code snippets only. The complete source code of the samples with solutions for Visual Studio is available from the download area on CodePlex.