Add Watermark in PDF in C#, VB.NET

There are two kinds of PDF watermarks: text watermark and image watermark. Text watermark is generally used in commercial field to show the background information of the PDF documents, while image watermark is really a good choice to beautify PDF files. This section will introduce solutions to add both of the two PDF watermarks via a .NET PDF component in C#, VB.NET.

Spire.PDF for .NET, with rich features to handle PDF files, enables you to add watermark in PDF with C#, VB.NET. Before introducing solutions of how to add two PDF watermarks, please see screenshot of image watermark as picture below:

Now, you can download Spire.PDF for .NET and view the solutions to add text watermark and image watermark.

Add Watermarks in PDF

In the solution, an image is necessary to be prepared. You are only required to load your image by the method Image FromFile(string filename) and then, set the PDF background image to be the image you just imported from system.

Image img = Image.FromFile(@"..\flower.jpg");
            page.BackgroundImage = img;
Dim img As Image = Image.FromFile("..\flower.jpg")
           page.BackgroundImage = img

Compared with image watermark, text watermark is not so easy since it requires you to draw the text which you want to set as text watermark yourself. Besides, you need to set the text font, color, position and text format to fit the PDF page suitably. All the two tasks can be quickly realized by calling the method: DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format). Please see the key code below:

  PdfTilingBrush brush
                = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
            brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
            brush.Graphics.DrawString("Spire.Pdf Demo",
                new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0,
                new PdfStringFormat(PdfTextAlignment.Center));
            page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
Dim brush As New PdfTilingBrush(New SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3))
brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2)
brush.Graphics.DrawString("Spire.Pdf Demo", New PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0, New PdfStringFormat(PdfTextAlignment.Center))
page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.ClientSize))

Spire.PDF is a professional third party library to manipulating PDF files with rich capabilities. As a stand-alone PDF component, it enables you to read, write, edit PDF files on .NET, Silverlight and WPF.