C#: Add a Stamp to a PDF Document

Adding a stamp to a PDF document can enhance its visual appeal and convey important information, such as approval, confidentiality, or urgency. This simple process allows users to mark documents with customized images or text, making it easier to communicate key messages. Whether for professional use or personal projects, stamping PDFs ensures clarity and adds a professional touch.

In this article, you will learn how to add a stamp to a PDF document using C# with 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 DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Prerequisite Knowledge

In a PDF, a stamp is an annotation or graphical element that adds supplementary information to a document. Spire.PDF for .NET includes the PdfRubberStampAnnotation class, which represents a rubber stamp. To create the appearance for the rubber stamp, you can use the PdfTemplate class, which serves as a canvas for drawing text, images, shapes, and date/time elements.

Add a Dynamic Stamp to PDF in C#

A dynamic stamp is a customizable annotation added to a PDF document to convey specific statuses, approvals, or other information. Unlike static stamps, dynamic stamps include variables or fields that can be updated in real time, such as the current date, time, username, or other custom data.

Here are the steps to add a dynamic stamp to a PDF using Spire.PDF for .NET:

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Create a PdfTemplate object with the desired size.
  • Draw strings, including dynamic information like date and time, on the template using PdfTemplate.Graphics.DrawString().
  • Create a PdfRubberStampAnnotation object and set the template as its appearance.
  • Add the stamp to a specific PDF page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;

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

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

            // Get a specific page
            PdfPageBase page = doc.Pages[1];

            // Create a PdfTemplate object
            PdfTemplate template = new PdfTemplate(220, 50);

            // Create two fonts
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 16f, FontStyle.Bold),true);  
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Times New Roman", 10f, FontStyle.Bold),true);

            // Create a solid brush and a gradient brush
            PdfSolidBrush solidBrush = new PdfSolidBrush(Color.Blue);
            RectangleF rectangle1 = new RectangleF(new PointF(0, 0), template.Size);
            PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rectangle1, new PdfRGBColor(Color.White), new PdfRGBColor(Color.Blue), PdfLinearGradientMode.Horizontal);

            // Create a pen
            PdfPen pen = new PdfPen(solidBrush);

            // Create a rounded rectangle path
            int CornerRadius = 10;
            PdfPath path = new PdfPath();
            path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, 180, 90);
            path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, 270, 90);
            path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
            path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
            path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / 2);

            // Draw path on the template
            template.Graphics.DrawPath(pen, path);
            template.Graphics.DrawPath(linearGradientBrush, path);

            // Draw text on the template
            String string1 = "APPROVED\n";
            String string2 = "By Marketing Manager at " + DateTime.Now.ToString("HH:mm, MMM dd, yyyy");
            template.Graphics.DrawString(string1, font1, solidBrush, new PointF(5, 5));
            template.Graphics.DrawString(string2, font2, solidBrush, new PointF(2, 28));

            // Create a rubber stamp, specifying its size and location
            RectangleF rectangle2 = new RectangleF(55, page.ActualSize.Height - 55 - 70, 240, 55);
            PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rectangle2);

            // Create a PdfAppearance object and apply the template as its normal state
            PdfAppearance apprearance = new PdfAppearance(stamp);
            apprearance.Normal = template;

            // Apply the appearance to stamp
            stamp.Appearance = apprearance;

            // Add the stamp annotation to annotation collection
            page.Annotations.Add(stamp);

            // Save the file
            doc.SaveToFile("DynamicStamp.pdf", FileFormat.PDF);

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add a Stamp to a PDF Document

Add an Image Stamp to PDF in C#

An image stamp in a PDF is a graphical element that is added to a document as an annotation or overlay. This stamp typically consists of an image that can be positioned at a specific location on a PDF page.

Here are the steps to add an image stamp to a PDF using Spire.PDF for .NET:

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Create a PdfTemplate object with the desired size.
  • Draw strings, including dynamic information like date and time, on the template using PdfTemplate.Graphics.DrawString().
  • Create a PdfRubberStampAnnotation object and set the template as its appearance.
  • Add the stamp to a specific PDF page using PdfPageBase.Annotations.Add() method.
  • Save the document to a different PDF file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
using Spire.Pdf.Graphics;
using System.Drawing;

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

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

            // Get a specific page
            PdfPageBase page = doc.Pages[1];

            // Load an image file
            PdfImage image = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\confidential.png");

            // Get the width and height of the image
            int width = image.Width;
            int height = image.Height;

            // Create a PdfTemplate object based on the size of the image
            PdfTemplate template = new PdfTemplate(width, height, true);

            // Draw image on the template
            template.Graphics.DrawImage(image, 0, 0, width, height);

            // Create a rubber stamp annotation, specifying its location and position
            RectangleF rect = new RectangleF(page.ActualSize.Width - width - 50, page.ActualSize.Height - height - 50, width, height);
            PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect);

            // Create a PdfAppearance object
            PdfAppearance pdfAppearance = new PdfAppearance(stamp);

            // Set the template as the normal state of the appearance
            pdfAppearance.Normal = template;

            // Apply the appearance to the stamp
            stamp.Appearance = pdfAppearance;

            // Add the stamp annotation to PDF
            page.Annotations.Add(stamp);

            // Save the file
            doc.SaveToFile("ImageStamp.pdf");

            // Dispose resources
            doc.Dispose();
        }
    }
}

C#: Add a Stamp to a PDF Document

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.