Introduction
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
- using Spire.Pdf;
- using Spire.Pdf.Annotations;
- using Spire.Pdf.Graphics;
- using Spire.Pdf.Annotations.Appearance;
- 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.
- PdfDocument document = newPdfDocument();
- 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.
-
- PdfTemplate template = newPdfTemplate(100, 100);
-
- PdfImage image = PdfImage.FromFile("Certified.jpg");
- float width=image.Width * 0.4f;
- float height=image.Height * 0.4f;
- template.Graphics.DrawImage(image, 0, 0, width, height);
-
- RectangleF rectangle = newRectangleF(newPointF(20, 0), template.Size);
- PdfRubberStampAnnotation stamp = newPdfRubberStampAnnotation(rectangle);
-
- PdfAppearanceapprearance = newPdfAppearance(stamp);
- apprearance.Normal = template;
- stamp.Appearance = apprearance;
-
- page.AnnotationsWidget.Add(stamp);
- document.SaveToFile("Stamp.pdf", FileFormat.PDF);
Output
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.
-
- PdfTemplate template = newPdfTemplate(200, 50);
-
- PdfSolidBrush brush = newPdfSolidBrush(Color.DarkBlue);
- PdfPen pen = newPdfPen(brush);
- intCornerRadius = 20;
- PdfPath path = newPdfPath();
- 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 = "APPROVED\n";
- String s2 = "By John at " + DateTime.Now.ToString("HH:mm, MMM dd, yyyy");
- PdfTrueTypeFont font1 = newPdfTrueTypeFont(newFont("Elephant", 16f, FontStyle.Italic), true);
- template.Graphics.DrawString(s1, font1, brush, newPointF(5, 5));
- PdfTrueTypeFont font2 = newPdfTrueTypeFont(newFont("Gadugi", 12f, FontStyle.Bold), true);
- template.Graphics.DrawString(s2, font2, brush, newPointF(5, 28));
-
- RectangleF rectangle = newRectangleF(newPointF(30, 0), template.Size);
- PdfRubberStampAnnotation stamp = newPdfRubberStampAnnotation(rectangle);
- PdfAppearanceapprearance = newPdfAppearance(stamp);
- apprearance.Normal = template;
- stamp.Appearance = apprearance;
- page.AnnotationsWidget.Add(stamp);
- document.SaveToFile("Stamp.pdf", FileFormat.PDF);
Output
Conclusion
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.