Except for creating signature, Spire.PDF also allows us to add signature field to PDF using the PdfSignatureField class and the PdfFieldCollection.Add (PdfField field) method in Spire.Pdf.Fields namespace. Once added, we can click on the field to add signature manually to the PDF document.
This article explains how to add a signature field to the specified page of a PDF document using Spire.PDF.
Detail steps and code snippets:
Step 1: Create a new PDF document and add a page to it.
PdfDocument pdfdoc = new PdfDocument(); PdfPageBase page = pdfdoc.Pages.Add();
Step 2: Use PdfSignatureField class to add a named signature field to the specified page by passing two parameters: page and name of the signature field.
PdfSignatureField signaturefield = new PdfSignatureField(page, "Signature");
Step 3: Set border width, style, color, highlight mode and bounds for the signature field.
signaturefield.BorderWidth = 1.0f; signaturefield.BorderStyle = PdfBorderStyle.Solid; signaturefield.BorderColor = new PdfRGBColor(System.Drawing.Color.Black); signaturefield.HighlightMode = PdfHighlightMode.Outline; signaturefield.Bounds = new RectangleF(100, 100, 100, 100);
Step 4: Add the signature field to the document's root fields.
pdfdoc.Form.Fields.Add(signaturefield);
Step 5: Save the document.
pdfdoc.SaveToFile("AddSignField.pdf", FileFormat.PDF);
After running the code, we'll get the result PDF file with a signature field on the first page, effective screenshot as shown below:
Full codes:
using System.Drawing; using Spire.Pdf; using Spire.Pdf.Fields; using Spire.Pdf.Graphics; namespace Add_Signature_Filed_to_PDF { class Program { static void Main(string[] args) { PdfDocument pdfdoc = new PdfDocument(); PdfPageBase page = pdfdoc.Pages.Add(); PdfSignatureField signaturefield = new PdfSignatureField(page, "Signature"); signaturefield.BorderWidth = 1.0f; signaturefield.BorderStyle = PdfBorderStyle.Solid; signaturefield.BorderColor = new PdfRGBColor(System.Drawing.Color.Black); signaturefield.HighlightMode = PdfHighlightMode.Outline; signaturefield.Bounds = new RectangleF(100, 100, 100, 100); pdfdoc.Form.Fields.Add(signaturefield); pdfdoc.SaveToFile("AddSignField.pdf", FileFormat.PDF); } } }