How to Create a Signature Field in PDF in C#

A signature field enables the user to place his signature in a form. This field type allows instant validation from a customer through his digital signature, eliminating the need of paper work. To prepare a document for digital signing, add one or more digital signature fields (depending on how many signatures are needed). This article presents how to create signature fields in a PDF document using Spire.PDF in C#.

Adding Necessary Namespaces

using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;

Using the code

STEP 1. Create a PDF document, create a blank page.

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

STEP 2. Draw some text in PDF.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);
PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
string s1 = "Representative #1";
string s2 = "Representative #2";
SizeF size = font.MeasureString(s1);
int x = 180;
int y1 = 120, y2 = 180;
page.Canvas.DrawString(s1, font, brush, new PointF(x, y1));
page.Canvas.DrawString(s2, font, brush, new PointF(x, y2));

STEP 3. Draw two line shapes on PDF.

PdfPen pen = new PdfPen(Color.Black, 0.5f);
page.Canvas.DrawLine(pen, new PointF(x + size.Width + 2, y1 +size.Height+ 2), new PointF(x + size.Width + 126, y1 + size.Height + 2));
page.Canvas.DrawLine(pen, new PointF(x + size.Width + 2, y2 + size.Height + 2), new PointF(x + size.Width + 126, y2 + size.Height + 2));

STEP 4. Create two signature fields in PDF and set the properties respectively.

PdfSignatureField signaturefield1 = new PdfSignatureField(page, "Signature1");
signaturefield1.BorderWidth = 1.0f;
signaturefield1.BorderStyle = PdfBorderStyle.Solid;
signaturefield1.BorderColor = new PdfRGBColor(System.Drawing.Color.White);
signaturefield1.HighlightMode = PdfHighlightMode.Outline;
signaturefield1.Bounds = new RectangleF(x+size.Width+5, y1 - 50 + size.Height, 120, 50);

PdfSignatureField signaturefield2 = new PdfSignatureField(page, "Signature2");
signaturefield2.BorderWidth = 1.0f;
signaturefield2.BorderStyle = PdfBorderStyle.Solid;
signaturefield2.BorderColor = new PdfRGBColor(System.Drawing.Color.White);
signaturefield2.HighlightMode = PdfHighlightMode.Outline;
signaturefield2.Bounds = new RectangleF(x+size.Width+5, y2 - 50 + size.Height, 120, 50);

doc.Form.Fields.Add(signaturefield1);
doc.Form.Fields.Add(signaturefield2);

STEP 5. Save to file.

doc.SaveToFile("SignatureField.pdf", FileFormat.PDF);

Output

addsignature