The PDF-based fillable form, also known as AcroForm, is an interactive form with a collection of fields, such as TextBox, Button, CheckBox, etc. These fields can be filled with data programmatically or manually in order to take information as input from the user. For more information on AcroForm, see PDF specification 1.7 (Section 12.7).
GcPdf allows you to create AcroForms using AcroForm class and define common properties of AcroForm using Fields property of AcroForm class. The library lets you add, get, modify, and delete different form fields. You can use the following fields in AcroForms.
To add AcroForm fields in a PDF document using GcPdf:
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { var doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; TextFormat tf = new TextFormat(); tf.Font = StandardFonts.Times; tf.FontSize = 14; PointF ip = new PointF(72, 72); float fldOffset = 72 * 2; float fldHeight = tf.FontSize * 1.2f; float dY = 32; // Add TextField g.DrawString("Text field:", tf, ip); var fldText = new TextField(); fldText.Value = "Initial TextField value"; fldText.Widget.Page = page; fldText.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight); fldText.Widget.TextFormat.Font = tf.Font; fldText.Widget.TextFormat.FontSize = tf.FontSize; doc.AcroForm.Fields.Add(fldText); ip.Y += dY; // Add Checkbox: g.DrawString("Checkbox:", tf, ip); var fldCheckbox = new CheckBoxField(); fldCheckbox.Value = true; fldCheckbox.Widget.Page = page; fldCheckbox.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight); doc.AcroForm.Fields.Add(fldCheckbox); ip.Y += dY; // Save Document doc.Save(stream); } |
To modify form fields in PDF document, get the particular form field by specifying its index and set a new value for the field using Value property. This property fills the field with the specified string value.
C# |
Copy Code
|
---|---|
doc.AcroForm.Fields[0].Value = "Sample Text";
|
To delete a particular form field in PDF document, use RemoveAt method to remove any field by specifying its index value. Apart from this, Clear method can be used to remove all the AcroForm fields from the document.
C# |
Copy Code
|
---|---|
// Delete a particular field doc.AcroForm.Fields.RemoveAt(0); // Delete all the fields doc.AcroForm.Fields.Clear(); |
For more information about implementation of AcroForms using GcPdf, see GcPdf sample browser.