Watermark Integration in C# and .NET for PDFs

Watermarking is a common method. With the help of the SautinSoft library, you can easily add them to your PDF documents using C# and .NET.

Watermarks serve several purposes:

  • Document protection. Preventing unauthorized use or distribution of confidential documents.
  • Creating a corporate identity. Add company logos or text to preserve the corporate identity.
  • Specify the status. Mark the documents as "Confidential", "Draft" or "Approved".
  • Copyright protection. Protect intellectual property by using visible watermarks.

The process of adding watermarks to an existing file consists of several steps:

  1. Add SautinSoft.PDF from NuGet.
  2. Load a PDF Document.
  3. Download a PDF document.
  4. Upload an image and set a watermark from a file for each page in the document.
  5. Place the watermark in the center of the page.
  6. Scale the watermark to fit the page size.
  7. Draw a watermark.
  8. Save the document.

Input file:

Output result:

Complete code

using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Watermarks.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/watermarks.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Before starting this example, please get a free 100-day trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            string pdfFile = Path.GetFullPath(@"..\..\..\simple text.pdf");

            using (var document = PdfDocument.Load(pdfFile))
            {
                // Load the watermark from a file.
                var image = PdfImage.Load(@"..\..\..\WatermarkImage.png");

                foreach (var page in document.Pages)
                {
                    // Make sure the watermark is correctly transformed even if
                    // the page has a custom crop box origin, is rotated, or has custom units.
                    var transform = page.Transform;
                    transform.Invert();

                    // Center the watermark on the page.
                    var pageSize = page.Size;
                    transform.Translate((pageSize.Width - 1) / 2, (pageSize.Height - 1) / 2);

                    // Calculate the scaling factor so that the watermark fits the page.
                    var cropBox = page.CropBox;
                    var scale = Math.Min(cropBox.Width, cropBox.Height);

                    // Scale the watermark so that it fits the page.
                    transform.Scale(scale, scale, 0.5, 0.5);

                    // Draw the centered and scaled watermark.
                    page.Content.DrawImage(image, transform);
                }

                document.Save("Watermark Images.pdf");
            }

            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Watermark Images.pdf") { UseShellExecute = true });
        }
    }
}

Download

Option Infer On

Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content

Namespace Sample
	Friend Class Sample
		''' <summary>
		''' Watermarks.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/watermarks.php
		''' </remarks>
		Shared Sub Main(ByVal args() As String)
			' Before starting this example, please get a free 100-day trial key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' PdfDocument.SetLicense("...");

			Dim pdfFile As String = Path.GetFullPath("..\..\..\simple text.pdf")

			Using document = PdfDocument.Load(pdfFile)
				' Load the watermark from a file.
				Dim image = PdfImage.Load("..\..\..\WatermarkImage.png")

				For Each page In document.Pages
					' Make sure the watermark is correctly transformed even if
					' the page has a custom crop box origin, is rotated, or has custom units.
					Dim transform = page.Transform
					transform.Invert()

					' Center the watermark on the page.
					Dim pageSize = page.Size
					transform.Translate((pageSize.Width - 1) \ 2, (pageSize.Height - 1) \ 2)

					' Calculate the scaling factor so that the watermark fits the page.
					Dim cropBox = page.CropBox
					Dim scale = Math.Min(cropBox.Width, cropBox.Height)

					' Scale the watermark so that it fits the page.
					transform.Scale(scale, scale, 0.5, 0.5)

					' Draw the centered and scaled watermark.
					page.Content.DrawImage(image, transform)
				Next page

				document.Save("Watermark Images.pdf")
			End Using

			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Watermark Images.pdf") With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.