To prevent your PDF document from being used in an unauthorized manner, you can watermark the document with text or an image. In this article, you will learn how to programmatically add text watermarks (single-line and multi-line watermarks) to PDF in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
- Package Manager
PM> Install-Package Spire.PDF
Add a Text Watermark to PDF
Spire.PDF does not provide an interface or a class to deal with watermarks in PDF files. You could, however, draw text like "confidential", "internal use", or "draft" on each page to mimic the watermark effect. The following are the main steps to add a text watermark to all pages of a PDF document.
- Create a PdfDocument object and load a sample PDF document using PdfDocument.LoadFromFile() method.
- Create a PdfTrueTypeFont object, specify the watermark text, and measure the text size using PdfFontBase.MeasureString() method.
- Traverse all the pages in the document.
- Translate the coordinate system of a certain page by specified coordinates using PdfPageBase.Canvas.TraslateTransform() method, and rotate the coordinate system 45 degrees counterclockwise using PdfPageBase.Canvas.RotateTransform() method. This ensures that the watermark will appear in the middle of the page at a 45-degree angle.
- Draw the watermark text on the page using PdfPageBase.Canvas.DrawString() method.
- Save the document to a different PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace AddTextWatermarkToPdf { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a sample PDF document pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf"); //Create a PdfTrueTypeFont object PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 50f), true); //Set the watermark text string text = "CONFIDENTIAL"; //Measure the text size SizeF textSize = font.MeasureString(text); //Calculate the values of two offset variables, //which will be used to calculate the translation amount of the coordinate system float offset1 = (float)(textSize.Width * System.Math.Sqrt(2) / 4); float offset2 = (float)(textSize.Height * System.Math.Sqrt(2) / 4); //Traverse all the pages in the document foreach (PdfPageBase page in pdf.Pages) { //Set the page transparency page.Canvas.SetTransparency(0.8f); //Translate the coordinate system by specified coordinates page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2); //Rotate the coordinate system 45 degrees counterclockwise page.Canvas.RotateTransform(-45); //Draw watermark text on the page page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0); } //Save the changes to another file pdf.SaveToFile("TextWatermark.pdf"); } } }
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace AddTextWatermarkToPdf Class Program Shared Sub Main(ByVal args() As String) 'Create a PdfDocument object Dim pdf As PdfDocument = New PdfDocument() 'Load a sample PDF document pdf.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf") 'Create a PdfTrueTypeFont object Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial",50f),True) 'Set the watermark text Dim text As String = "CONFIDENTIAL" 'Measure the text size Dim textSize As SizeF = font.MeasureString(text) 'Calculate the values of two offset variables, 'which will be used to calculate the translation amount of the coordinate system Dim offset1 As single = CType((textSize.Width * System.Math.Sqrt(2) / 4), single) Dim offset2 As single = CType((textSize.Height * System.Math.Sqrt(2) / 4), single) 'Traverse all the pages in the document Dim page As PdfPageBase For Each page In pdf.Pages 'Set the page transparency page.Canvas.SetTransparency(0.8f) 'Translate the coordinate system by specified coordinates page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2) 'Rotate the coordinate system 45 degrees counterclockwise page.Canvas.RotateTransform(-45) 'Draw watermark text on the page page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0) Next 'Save the changes to another file pdf.SaveToFile("TextWatermark.pdf") End Sub End Class End Namespace
Add Multi-Line Text Watermarks to PDF
There are times when you may want to add more than one line of text watermarks to your document. To achieve the tiled watermark effect, you can make use of PdfTilingBrush class, which produces a tiled pattern that is repeated to fill a graphics area. The following are the main steps to add multi-line watermarks to a PDF document.
- Create a PdfDocument object and load a sample PDF document using PdfDocument.LoadFromFile() method.
- Create a custom method InsertMultiLineTextWatermark(PdfPageBase page, String watermarkText, PdfTrueTypeFont font, int rowNum, int columnNum) to add multi-line text watermarks to a PDF page. The parameters rowNum and columnNum specify the row and column number of the tiled watermarks.
- Traverse all pages in the document, and call the custom method InsertMultiLineTextWatermark() to apply watermarks to each page.
- Save the document to another file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using System; using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace AddMultiLineTextWatermark { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a sample PDF document pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf"); //Create a PdfTrueTypeFont object PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 20f), true); //Traverse all the pages for (int i = 0; i < pdf.Pages.Count; i++) { //Call InsertMultiLineTextWatermark() method to add text watermarks to the specified page InsertMultiLineTextWatermark(pdf.Pages[i], "E-ICEBLUE CO LTD", font, 3, 3); } //Save the document to another file pdf.SaveToFile("MultiLineTextWatermark.pdf"); } //Create a custom method to insert multi-line text watermarks to a page static void InsertMultiLineTextWatermark(PdfPageBase page, String watermarkText, PdfTrueTypeFont font, int rowNum, int columnNum) { //Measure the text size SizeF textSize = font.MeasureString(watermarkText); //Calculate the values of two offset variables, which will be used to calculate the translation amount of coordinate system float offset1 = (float)(textSize.Width * System.Math.Sqrt(2) / 4); float offset2 = (float)(textSize.Height * System.Math.Sqrt(2) / 4); //Create a tile brush PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.ActualSize.Width / columnNum, page.ActualSize.Height / rowNum)); brush.Graphics.SetTransparency(0.3f); brush.Graphics.Save(); brush.Graphics.TranslateTransform(brush.Size.Width / 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2); brush.Graphics.RotateTransform(-45); //Draw watermark text on the tile brush brush.Graphics.DrawString(watermarkText, font, PdfBrushes.Violet, 0, 0); brush.Graphics.Restore(); //Draw a rectangle (that covers the whole page) using the tile brush page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.ActualSize)); } } }
Imports System Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace AddMultiLineTextWatermark Class Program Shared Sub Main(ByVal args() As String) 'Create a PdfDocument instance Dim pdf As PdfDocument = New PdfDocument() 'Load a sample PDF document pdf.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf") 'Create a PdfTrueTypeFont object Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial",20f),True) 'Traverse all the pages Dim i As Integer For i = 0 To pdf.Pages.Count- 1 Step i + 1 'Call InsertMultiLineTextWatermark() method to add text watermarks to the specified page InsertMultiLineTextWatermark(pdf.Pages(i), "E-ICEBLUE CO LTD", font, 3, 3) Next 'Save the document to another file pdf.SaveToFile("MultiLineTextWatermark.pdf") End Sub 'Create a custom method to insert multi-line text watermarks to a page Shared Sub InsertMultiLineTextWatermark(ByVal page As PdfPageBase, ByVal watermarkText As String, ByVal font As PdfTrueTypeFont, ByVal rowNum As Integer, ByVal columnNum As Integer) 'Measure the text size Dim textSize As SizeF = font.MeasureString(watermarkText) 'Calculate the values of two offset variables, which will be used to calculate the translation amount of coordinate system Dim offset1 As single = CType((textSize.Width * System.Math.Sqrt(2) / 4), single) Dim offset2 As single = CType((textSize.Height * System.Math.Sqrt(2) / 4), single) 'Create a tile brush Dim brush As PdfTilingBrush = New PdfTilingBrush(New SizeF(page.ActualSize.Width / columnNum,page.ActualSize.Height / rowNum)) brush.Graphics.SetTransparency(0.3f) brush.Graphics.Save() brush.Graphics.TranslateTransform(brush.Size.Width / 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2) brush.Graphics.RotateTransform(-45) 'Draw watermark text on the tile brush brush.Graphics.DrawString(watermarkText, font, PdfBrushes.Violet, 0, 0) brush.Graphics.Restore() 'Draw a rectangle (that covers the whole page) using the tile brush page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.ActualSize)) End Sub End Class End Namespace
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.