DEV Community

Gia
Gia

Posted on

How to Add Watermarks to PDF in C#

A watermark is a transparent mark or pattern that is typically used for copyright protection, branding, anti-piracy, and providing additional information. We have previously discussed how to add a watermark to a PDF using Java. This article will demonstrate how to achieve it by using C#.

Tool:

This is a free version of Spire.PDF for Java, supports editing and converting PDF on .NET platforms freely. You can also download the commercial version from this link and trial it for 30 days.

Installation:

  • Download and install Free Spire.PDF for .NET.
  • Create a new C# project and open it.
  • Right-click " References " in the " Solution Explorer ".
  • And then select " Add Reference " > " Browse ".
  • Find the DLL file in the BIN folder under the installation path and click " OK " to add it as a reference.

Sample Code:

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(@"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], "Confidential", font, 3, 3);
            }

            //Save the document to another file
            pdf.SaveToFile("Watermark.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.5f);
            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));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image description
This library also supports adding image watermark to PDF. This article will help you do that.

Top comments (0)