Create a Dynamic Stamp in PDF in C#/VB.NET

Dynamic stamp is one form of PDF stamps, which obtains information from your computer and from the identity panel of the preferences dialog box, allowing you to indicate name, date, and time information on the stamp. In this article, I’ll introduce how to create a dynamic stamp in PDF using Spire.PDF.

How to Create a Dynamic Stamp in PDF in C#, VB.NET

Code Snippet:

Step 1: Initialize a new instance of PdfDocument class and add a new page. You could also load an existing PDF document and create a dynamic stamp on it.

PdfDocument document = new PdfDocument();
PdfPageBase page = document.Pages.Add();

Step 2: Create a new PdfTemplate object, drew a rounded rectangle on PDF template.

PdfTemplate template = new PdfTemplate(140, 50);
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 16f, FontStyle.Italic), true);
PdfSolidBrush brush = new PdfSolidBrush(Color.DarkRed);
PdfPen pen = new PdfPen(brush);          
RectangleF rectangle = new RectangleF(new PointF(0, 0), template.Size);
int CornerRadius = 20;
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);
template.Graphics.DrawPath(pen, path);

Step 3: Draw "REVISED" and the current data time as text on template.

String s1 = "REVISED\n";
String s2 = DateTime.Now.ToString(" HH:mm, MMM dd, yyyy");
template.Graphics.DrawString(s1, font1, brush, new PointF(5, 5));
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Gadugi", 12f, FontStyle.Bold), true);
template.Graphics.DrawString(s2, font2, brush, new PointF(2, 28));

Step 4: Initialize a new instance of PdfRubberStampAnnotation class, apply the template to Stamp.Appearance.

PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rectangle);
PdfAppearance apprearance = new PdfAppearance(stamp);
apprearance.Normal = template;
stamp.Appearance = apprearance;

Step 5: Add the stamp to the annotation field collection.

page.AnnotationsWidget.Add(stamp);

Step 6: Save and launch the file.

document.SaveToFile("DynamicStamp.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("DynamicStamp.pdf");

Output:

How to Create a Dynamic Stamp in PDF in C#, VB.NET

Full Code:

C#
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;


namespace DynamicStamp
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();
            PdfPageBase page = document.Pages.Add();

            PdfTemplate template = new PdfTemplate(140, 50);
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 16f, FontStyle.Italic), true);
            PdfSolidBrush brush = new PdfSolidBrush(Color.DarkRed);
            PdfPen pen = new PdfPen(brush);
            RectangleF rectangle = new RectangleF(new PointF(0, 0), template.Size);
            int CornerRadius = 20;
            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);
            template.Graphics.DrawPath(pen, path);
            String s1 = "REVISED\n";
            String s2 = DateTime.Now.ToString("HH:mm, MMM dd, yyyy");
            template.Graphics.DrawString(s1, font1, brush, new PointF(5, 5));
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Gadugi", 12f, FontStyle.Bold), true);
            template.Graphics.DrawString(s2, font2, brush, new PointF(2, 28));

            PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rectangle);
            PdfAppearance apprearance = new PdfAppearance(stamp);
            apprearance.Normal = template;
            stamp.Appearance = apprearance;
            page.AnnotationsWidget.Add(stamp);

            document.SaveToFile("DynamicStamp.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("DynamicStamp.pdf");
        }
    }
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports Spire.Pdf.Annotations.Appearance
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace DynamicStamp
	Class Program
		Private Shared Sub Main(args As String())
			Dim document As New PdfDocument()
			Dim page As PdfPageBase = document.Pages.Add()

			Dim template As New PdfTemplate(140, 50)
			Dim font1 As New PdfTrueTypeFont(New Font("Elephant", 16F, FontStyle.Italic), True)
			Dim brush As New PdfSolidBrush(Color.DarkRed)
			Dim pen As New PdfPen(brush)
			Dim rectangle As New RectangleF(New PointF(0, 0), template.Size)
			Dim CornerRadius As Integer = 20
			Dim path As 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)
			template.Graphics.DrawPath(pen, path)
			Dim s1 As [String] = "REVISED" & vbLf
			Dim s2 As [String] = DateTime.Now.ToString("HH:mm, MMM dd, yyyy")
			template.Graphics.DrawString(s1, font1, brush, New PointF(5, 5))
			Dim font2 As New PdfTrueTypeFont(New Font("Gadugi", 12F, FontStyle.Bold), True)
			template.Graphics.DrawString(s2, font2, brush, New PointF(2, 28))

			Dim stamp As New PdfRubberStampAnnotation(rectangle)
			Dim apprearance As New PdfAppearance(stamp)
			apprearance.Normal = template
			stamp.Appearance = apprearance
			page.AnnotationsWidget.Add(stamp)

			document.SaveToFile("DynamicStamp.pdf", FileFormat.PDF)
			System.Diagnostics.Process.Start("DynamicStamp.pdf")
		End Sub
	End Class
End Namespace