C#/VB.NET: Add Image Watermarks to PDF

An image watermark is usually a logo or sign that appears on the background of digital documents, indicating the copyright owner of the content. Watermarking your PDF document with an image can prevent your data from being reused or modified. This article demonstrates how to add an image watermark 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 an Image Watermark to PDF

The following are the main steps to add an image watermark to a PDF document.

  • Create a PdfDocument object, and load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load an image file using Image.FromFile() method.
  • Loop through the pages in the document, and get the specific page through PdfDocument.Pages[] property.
  • Set the image as background/watermark image of the current page through PdfPageBase.BackgroundImage property. Set the image position and size through PdfPageBase.BackgroundRegion property.
  • Save the document to a different PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using System.Drawing;

namespace AddImageWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument document = new PdfDocument();

            //Load a sample PDF document
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Load an image
            Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");

            //Get the image width and height
            int imgWidth = image.Width;
            int imgHeight = image.Height;
         
            //Loop through the pages
            for (int i = 0; i < document.Pages.Count; i++)
            {
                //Get the page width and height
                float pageWidth = document.Pages[i].ActualSize.Width;
                float pageHeight = document.Pages[i].ActualSize.Height;

                //Set the background opacity
                document.Pages[i].BackgroudOpacity = 0.3f;

                //Set the background image of current page
                document.Pages[i].BackgroundImage = image;

                //Position the background image at the center of the page
                Rectangle rect = new Rectangle((int)(pageWidth - imgWidth) / 2, (int)(pageHeight - imgHeight) / 2, imgWidth, imgHeight);
                document.Pages[i].BackgroundRegion = rect;
            }

            //Save the document to file
            document.SaveToFile("AddImageWatermark.pdf");
            document.Close();
        }
    }
}
Imports Spire.Pdf
Imports System.Drawing
 
Namespace AddImageWatermark
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Create a PdfDocument object
            Dim document As PdfDocument =  New PdfDocument() 
 
            'Load a sample PDF document
            document.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
 
            'Load an image
            Dim image As Image =  Image.FromFile("C:\Users\Administrator\Desktop\logo.png") 
 
            'Get the image width and height
            Dim imgWidth As Integer =  image.Width 
            Dim imgHeight As Integer =  image.Height 
 
            'Loop through the pages
            Dim i As Integer
            For  i = 0 To  document.Pages.Count- 1  Step  i + 1
                'Get the page width and height
                Dim pageWidth As single =  document.Pages(i).ActualSize.Width 
                Dim pageHeight As single =  document.Pages(i).ActualSize.Height 
 
                'Set the background opacity
                document.Pages(i).BackgroudOpacity = 0.3f
 
                'Set the background image of current page
                document.Pages(i).BackgroundImage = image
 
                'Position the background image at the center of the page
                Dim rect As Rectangle =  New Rectangle(CType((pageWidth - imgWidth) / 2,(Integer)(pageHeight - imgHeight) / 2,imgWidth,imgHeight, Integer)) 
                document.Pages(i).BackgroundRegion = rect
            Next
 
            'Save the document to file
            document.SaveToFile("AddImageWatermark.pdf")
            document.Close()
        End Sub
    End Class
End Namespace

C#/VB.NET: Add Image Watermarks to PDF

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.