A watermark is a text or image that appears in front of or behind existing content in a document. It is often used to prevent your document from being used illegally. In this article, I am going to introduce how to add one-line or multi-line text watermark to PDF in C# and VB.NET using Spire.PDF for .NET.
INSTALLING SPIRE.PDF
First of all, you need to download the latest version of Spire.PDF from this link, and manually add the DLL files in your .NET application as references. Or, you can install it directly via NuGet.
Now, let’s take a look at the code part.
Example 1. Add a Text Watermark to PDF
[C#]
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", 55f), true);
//Set the watermark text
string text = "Internal Use";
//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 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 to the lower right corner
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");
}
}
}
[VB.NET]
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",55f),True)
'Set the watermark text
Dim text As String = "Internal Use"
'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 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 to the lower right corner
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
Example 2. Add Multi-Line Text Watermark to PDF
[C#]
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 PDF document
pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
//Create a PdfTrueTypeFont object
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 20f), true);
//loop through the pages
for (int i = 0; i < pdf.Pages.Count; i++)
{
//add a text watermark to the specified page
InsertTextWatermark(pdf.Pages[i], "DotNetFiles", font, 3, 3);
}
//save the document to file
pdf.SaveToFile("MultiLineTextWaterMark.pdf");
System.Diagnostics.Process.Start("MultiLineTextWaterMark.pdf");
}
static void InsertTextWatermark(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 tiling 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 tiling brush
brush.Graphics.DrawString(watermarkText, font, PdfBrushes.Violet, 0, 0);
brush.Graphics.Restore();
//Draw a rectangle (that covers the whole page) using the tiling brush
page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.ActualSize));
}
}
}
[VB.NET]
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 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)
'loop through the pages
Dim i As Integer
For i = 0 To pdf.Pages.Count- 1 Step i + 1
'add a text watermark to the specified page
InsertTextWatermark(pdf.Pages(i), "DotNetFiles", font, 3, 3)
Next
'save the document to file
pdf.SaveToFile("MultiLineTextWaterMark.pdf")
System.Diagnostics.Process.Start("MultiLineTextWaterMark.pdf")
End Sub
Shared Sub InsertTextWatermark(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 tiling 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 tiling brush
brush.Graphics.DrawString(watermarkText, font, PdfBrushes.Violet, 0, 0)
brush.Graphics.Restore()
'Draw a rectangle (that covers the whole page) using the tiling brush
page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.ActualSize))
End Sub
End Class
End Namespace