This sample shows how to create and use watermarks in Docotic.Pdf library.

Please use PdfDocument.CreateXObject() method to add a PdfXObject to your document. This object will be used as a watermark. After a PdfXObject is added to your document you can setup its appearance using methods and properties of the object's canvas (check PdfXObject.Canvas property).

You can add watermark to a page using methods from PdfCanvas.DrawXObject group. The same watermark can be applied to any number of pages.

If you want to add background to a page then set DrawOnBackground property of created PdfXObject to true before using a PdfCanvas.DrawXObject method.

C#
using System.Diagnostics;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class Watermarks
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "Watermarks.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.AddPage();

                PdfXObject watermark = pdf.CreateXObject();

                // uncomment following line if you want the watermark to be placed before
                // any other contents of a page.
                // watermark.DrawOnBackground = true;

                PdfCanvas watermarkCanvas = watermark.Canvas;
                watermarkCanvas.TextAngle = -45;
                watermarkCanvas.FontSize = 36;
                watermarkCanvas.DrawString(100, 100, "This text is a part of the watermark");

                foreach (PdfPage page in pdf.Pages)
                {
                    // draw watermark at the top left corner over the page contents
                    // this will also add watermark to the collection of page XObjects
                    page.Canvas.DrawXObject(watermark, 0, 0);
                }

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class Watermarks
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "Watermarks.pdf"

            Using pdf As New PdfDocument()
                pdf.AddPage()

                Dim watermark As PdfXObject = pdf.CreateXObject()

                ' uncomment following line if you want the watermark to be placed before
                ' any other contents of a page.
                ' watermark.DrawOnBackground = True

                Dim watermarkCanvas As PdfCanvas = watermark.Canvas
                watermarkCanvas.TextAngle = -45
                watermarkCanvas.FontSize = 36
                watermarkCanvas.DrawString(100, 100, "This text is a part of the watermark")

                For i As Integer = 0 To pdf.PageCount - 1
                    ' draw watermark at the top left corner over the page contents
                    ' this will also add watermark to the collection of page XObjects
                    pdf.Pages(i).Canvas.DrawXObject(watermark, 0, 0)
                Next

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace