Add Stamps To PDF Using Spire.PDF In C#


The stamp tool in Adobe Acrobat software allows you to label your PDF document with important information, such as the document’s approval status or confidentiality level. In Adobe, a lot of stamp labels like “Confidential,” “Draft,” or “Approved” are preset for users to easily drag and place any of them to the preferred position on the page.

This article is aimed at introducing how to create a custom stamp and insert it to PDF document programmatically using free Spire.PDF. The sample project in this article requires Spire.Pdf.dll and System.Drawing to be added as reference in .NET assemblies, make sure they are correctly installed if you want to run the following code snippets on your system.

Imports Namespaces

  1. using Spire.Pdf;  
  2. using Spire.Pdf.Annotations;  
  3. using Spire.Pdf.Graphics;  
  4. using Spire.Pdf.Annotations.Appearance;  
  5. using System.Drawing;  
Using the Code

To begin with, I initialized a new instance of PdfDocument class and added a new blank page with the specified page size and margin. You could also load an existing PDF with LoadFromFile method and then create a stamp on it.
  1. PdfDocument document = newPdfDocument();  
  2. PdfPageBase page = document.Pages.Add(PdfPageSize.A4, newPdfMargins(50));  
The following section demonstrates how to create image stamp and text stamp respectively.

                                    Figure 1: Image Stamp

Using Spire.PDF, we’re able to make any image as stamp and insert it at any desire position on PDF. The core thought is simple: Create a template based on an image, initialize a new instance of Annotation class, and then apply the template to the appearance of annotation.
  1. //initialize a new PdfTemplate object  
  2. PdfTemplate template = newPdfTemplate(100, 100);  
  3. //load an image, resize and draw it on template  
  4. PdfImage image = PdfImage.FromFile("Certified.jpg");  
  5. float width=image.Width * 0.4f;  
  6. float height=image.Height * 0.4f;  
  7. template.Graphics.DrawImage(image, 0, 0, width, height);  
  8. //initialize an instance of PdfRubberStamoAnnotation class based on the size and position of RectangleF  
  9. RectangleF rectangle = newRectangleF(newPointF(20, 0), template.Size);  
  10. PdfRubberStampAnnotation stamp = newPdfRubberStampAnnotation(rectangle);  

  11. //set the appearance of the annotation  
  12. PdfAppearanceapprearance = newPdfAppearance(stamp);  
  13. apprearance.Normal = template;  
  14. stamp.Appearance = apprearance;  

  15. //add the stamp annotation to the page and save changes to file  
  16. page.AnnotationsWidget.Add(stamp);  
  17. document.SaveToFile("Stamp.pdf", FileFormat.PDF);  

                                    Figure 2: Text Stamp

The simplified text stamp only contains a word or a phrase, in this part I will introduce how to create a more complicated type - Dynamic Stamp, 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.

The way to create text stamp is quite similar to that of creating an image stamp. You can simply draw some text on the template and then create a stamp based on it, but it’s a bit tricky if you want to create a well-designed text stamp.
  1. //initialize a PdfTemplate object  
  2. PdfTemplate template = newPdfTemplate(200, 50);  
  3. //draw rounded rectangle on it   
  4. PdfSolidBrush brush = newPdfSolidBrush(Color.DarkBlue);  

  5. PdfPen pen = newPdfPen(brush);   
  6. intCornerRadius = 20;  
  7. PdfPath path = newPdfPath();  
  8. path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, 180, 90);  
  9. path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, 270, 90);  
  10. path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);  
  11. path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);  
  12. path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / 2);  
  13. template.Graphics.DrawPath(pen, path);  

  14. //draw some formatted text and data time on the template  
  15. String s1 = "APPROVED\n";  
  16. String s2 = "By John at " + DateTime.Now.ToString("HH:mm, MMM dd, yyyy");  
  17. PdfTrueTypeFont font1 = newPdfTrueTypeFont(newFont("Elephant", 16f, FontStyle.Italic), true);  
  18. template.Graphics.DrawString(s1, font1, brush, newPointF(5, 5));  
  19. PdfTrueTypeFont font2 = newPdfTrueTypeFont(newFont("Gadugi", 12f, FontStyle.Bold), true);  
  20. template.Graphics.DrawString(s2, font2, brush, newPointF(5, 28));  
  21. //create text stamp based on template  
  22. RectangleF rectangle = newRectangleF(newPointF(30, 0), template.Size);  
  23. PdfRubberStampAnnotation stamp = newPdfRubberStampAnnotation(rectangle);  
  24. PdfAppearanceapprearance = newPdfAppearance(stamp);  
  25. apprearance.Normal = template;  
  26. stamp.Appearance = apprearance;  
  27. page.AnnotationsWidget.Add(stamp);  
  28. document.SaveToFile("Stamp.pdf", FileFormat.PDF);  



There are a lot of PDF libraries out there if you search it on Google, only a few support working with annotations in PDF. The free Spire.PDF can meet my requirements well since I only need to process small PDF documents with no more than 10 pages.

If you have any question regarding this article or the used tool, please feel free to leave them in comments. Thanks for your time.

Up Next
    Ebook Download
    View all

    FileInfo in C#

    Read by 9.7k people
    Download Now!
    View all