Watermarks

With GemBox.Pdf, you can add a watermark to PDF from your C# or VB.NET application.

The following example shows how you can easily add a watermark to an existing PDF file.

PDF file with watermark added using GemBox.Pdf
Screenshot of PDF file with watermark added using GemBox.Pdf
Upload your file (Drag file here)
using System;
using GemBox.Pdf;
using GemBox.Pdf.Content;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = PdfDocument.Load("%InputFileName%"))
        {
            using (var formattedText = new PdfFormattedText())
            {
                // Format the watermark text.
                formattedText.FontFamily = new PdfFontFamily("Calibri");
                formattedText.Color = PdfColor.FromGray(0.75);
                formattedText.Opacity = 0.5;

                // Set the watermark text.
                formattedText.Append("CONFIDENTIAL");

                foreach (var page in document.Pages)
                {
                    // Make sure the watermark is correctly transformed even if
                    // the page has a custom crop box origin, is rotated, or has custom units.
                    var transform = page.Transform;
                    transform.Invert();

                    // Center the watermark on the page.
                    var pageSize = page.Size;
                    transform.Translate((pageSize.Width - formattedText.Width) / 2, 
                        (pageSize.Height - formattedText.Height) / 2);

                    // Rotate the watermark so it goes from the bottom-left to the top-right of the page.
                    var angle = Math.Atan2(pageSize.Height, pageSize.Width) * 180 / Math.PI;
                    transform.Rotate(angle, formattedText.Width / 2, formattedText.Height / 2);

                    // Calculate the bounds of the rotated watermark.
                    var watermarkBounds = new PdfQuad(new PdfPoint(0, 0),
                        new PdfPoint(formattedText.Width, 0),
                        new PdfPoint(formattedText.Width, formattedText.Height),
                        new PdfPoint(0, formattedText.Height));
                    transform.Transform(ref watermarkBounds);

                    // Calculate the scaling factor so that rotated watermark fits the page.
                    var cropBox = page.CropBox;
                    var scale = Math.Min(cropBox.Width / (watermarkBounds.Right - watermarkBounds.Left),
                        cropBox.Height / (watermarkBounds.Top - watermarkBounds.Bottom));

                    // Scale the watermark so that it fits the page.
                    transform.Scale(scale, scale, formattedText.Width / 2, formattedText.Height / 2);

                    // Draw the centered, rotated, and scaled watermark.
                    page.Content.DrawText(formattedText, transform);
                }
            }

            document.Save("Watermarks.%OutputFileType%");
        }
    }
}
Imports System
Imports GemBox.Pdf
Imports GemBox.Pdf.Content

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = PdfDocument.Load("%InputFileName%")

            Using formattedText = New PdfFormattedText()

                'Format the watermark text.
                formattedText.FontFamily = New PdfFontFamily("Calibri")
                formattedText.Color = PdfColor.FromGray(0.75)
                formattedText.Opacity = 0.5

                ' Set the watermark text.
                formattedText.Append("CONFIDENTIAL")

                For Each page In document.Pages

                    ' Make sure the watermark is correctly transformed even if
                    ' the page has a custom crop box origin, is rotated, or has custom units.
                    Dim transform = page.Transform
                    transform.Invert()

                    ' Center the watermark on the page.
                    Dim pageSize = page.Size
                    transform.Translate((pageSize.Width - formattedText.Width) / 2,
                        (pageSize.Height - formattedText.Height) / 2)

                    ' Rotate the watermark so it goes from the bottom-left to the top-right of the page.
                    Dim angle = Math.Atan2(pageSize.Height, pageSize.Width) * 180 / Math.PI
                    transform.Rotate(angle, formattedText.Width / 2, formattedText.Height / 2)

                    ' Calculate the bounds of the rotated watermark.
                    Dim watermarkBounds = New PdfQuad(New PdfPoint(0, 0),
                        New PdfPoint(formattedText.Width, 0),
                        New PdfPoint(formattedText.Width, formattedText.Height),
                        New PdfPoint(0, formattedText.Height))
                    transform.Transform(watermarkBounds)

                    ' Calculate the scaling factor so that rotated watermark fits the page.
                    Dim cropBox = page.CropBox
                    Dim scale = Math.Min(cropBox.Width / (watermarkBounds.Right - watermarkBounds.Left),
                        cropBox.Height / (watermarkBounds.Top - watermarkBounds.Bottom))

                    ' Scale the watermark so that it fits the page.
                    transform.Scale(scale, scale, formattedText.Width / 2, formattedText.Height / 2)

                    ' Draw the centered, rotated, and scaled watermark.
                    page.Content.DrawText(formattedText, transform)
                Next
            End Using

            document.Save("Watermarks.%OutputFileType%")
        End Using
    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Pdf yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.