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);


STEP 5. Save to file.

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




Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s