How To Add Watermark On PDF Files

Introduction

There are two kinds of PDF watermarks: text watermark and image watermark. Text watermark is generally used in commercial field to remind readers that the document is copyrighted and other people mustn’t copy or use it freely. Besides the security feature, the watermark can also be used to mark the basic status of the document, such as draft status or the final version of the document. Image watermark is a good choice to beautify PDF files since it fills the background with colorful and distinctive pictures, then how to add a watermark into a PDF file programmatically? It can be achieved in various ways, and one of the quick and easy solutions might be used a third party tool like Spire.PDF. This article will demonstrate how to add image watermark and text watermark into the PDF files using free Spire.PDF.

Introduction of Free Spire.PDF

Free Spire.PDF is a free and standalone PDF controls which provides rich PDF features for developers to create, write, edit, handle, read and convert PDF files via C# or VB.NET. Please note that the free version has 10 normal pages limits and 3 pages conversion limits.

Firstly, please download it from E-iceblue website and install it. Don’t worry if it requires administrator rights because it’s clean. After that, you can take advantage of "SampleCenter" and API Help to get started quickly, for which have plenty of code snippet and detailed introduction of API properties.

Here comes to the code snippet of how to add image watermark and text watermark for PDF files. I will split it into two parts. One for image watermark and the other for text watermark.

Part 1: Add Image Watermark.

Firstly, prepare an image to be set as watermark for PDF file. It is quite convenient that we only need to load the image by the method Image.FromFile(string filename) and then, set the image for the PDF background.

Step 1: Create a new instance of PDF document and load the document from file.
  1. PdfDocument pdf = new PdfDocument();  
  2. pdf.LoadFromFile("sample.pdf");
Step 2: Get the first page of the PDF
  1. PdfPageBase page = pdf.Pages[0];
Step 3: Load the image from file and set it as the PDF background.
  1. Image img = Image.FromFile("img.jpg");  
  2. page.BackgroundImage = img; 
Step 4: Save the document to file.
  1. pdf.SaveToFile("ImageWaterMark.pdf");
The effective screenshot of the image watermark:



Figure 1: Watermark

Part 2: Add Text Watermark

Compared with the image watermark, adding text watermark is more complicated. We need to draw the watermark text on PDF and then set the text font, color, position and text format to best fit the PDF page. 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 following code snippet:

Step 1: Create a new instance of PDF document and load the document from file.
  1. PdfDocument pdf= new PdfDocument();  
  2. pdf.LoadFromFile("sample.pdf");  
Step 2: Get the first page of the PDF.
  1. PdfPageBase page = pdf.Pages[0];
Step 3: Add the text watermark to the first page and set the format for the text.
  1. PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));  
  2. brush.Graphics.SetTransparency(0.3f);  
  3. brush.Graphics.Save();  
  4. brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);  
  5. brush.Graphics.RotateTransform(-45);  
  6. brush.Graphics.DrawString("Draft Version"new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Blue,0, 0, new PdfStringFormat(PdfTextAlignment.Center));  
  7. brush.Graphics.Restore();  
  8. brush.Graphics.SetTransparency(1);  
  9. page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
Step 4: Save the document to file.
  1. pdf.SaveToFile("TextWaterMark.pdf"); 
The effective screenshot of the text watermark:



Figure 2: Text Watermark

Conclusion

There are plenty of articles introducing the method to add watermark programmatically with no need of third party library. Here I used free Spire.PDF because I have other needs more than watermark like creating, converting, printing and protecting PDF, which are all supported by the library. It works like a charm and is a boost to my efficiency. If you are also interested, you could give it a try. Thank you for your reading and I hope that you could benefit from this article.


Similar Articles