Modifing AcroForm

The following code snippet also work with Aspose.PDF.Drawing library.

Get or Set Field Limit

The FormEditor class SetFieldLimit(field, limit) method allows you to set a field limit, the maximum number of characters that can be entered into a field.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

// Adding Field with limit
FormEditor form = new FormEditor();

form.BindPdf(dataDir + "input.pdf");
form.SetFieldLimit("textbox1", 15);

form.Save(dataDir + "SetFieldLimit_out.pdf");

Similarly, Aspose.PDF has a method that gets the field limit using the DOM approach. The following code snippet shows the steps.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Getting maximum field limit using DOM
Document document = new Document(dataDir + "FieldLimit.pdf");
Console.WriteLine("Limit: " + (document.Form["textbox1"] as TextBoxField).MaxLen);

You can also get the same value using the Aspose.Pdf.Facades namespace using the following code snippet.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Getting maximum field limit using Facades
Form form = new Form();
form.BindPdf(dataDir + "FieldLimit.pdf");
Console.WriteLine("Limit: " + form.GetFieldLimit("textbox1"));

Set Custom Font for the Form Field

Form fields in Adobe PDF files can be configured to use specific default fonts. In the early versions of Aspose.PDF, only the 14 default fonts were supported. Later releases allowed developers to apply any font. To set and update the default font used for form fields, use the DefaultAppearance(Font font, double size, Color color) class. This class can be found under the Aspose.Pdf.InteractiveFeatures namespace. To use this object, use the Field class DefaultAppearance property.

The following code snippet shows how to set the default font for PDF form fields.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

// Open document
Document document = new Document(dataDir + "FormFieldFont14.pdf");

// Get particular form field from document
Field field = document.Form["textbox1"] as Field;

// Create font object
Font font = FontRepository.FindFont("ComicSansMS");

// Set the font information for form field
// Field.DefaultAppearance = new DefaultAppearance(font, 10, System.Drawing.Color.Black);

// Save updated document
document.Save(dataDir + "FormFieldFont14_out.pdf");

Add/remove fields in existing form

All the form fields are contained in the Document object’s Form collection. This collection provides different methods that manage form fields, including the Delete method. If you want to delete a particular field, pass the field name as a parameter to the Delete method and then save the updated PDF document. The following code snippet shows how to delete a particular field from a PDF document.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

// Open document
Document document = new Document(dataDir + "DeleteFormField.pdf");

// Delete a particular field by name
document.Form.Delete("textbox1");

// Save modified document
document.Save(dataDir + "DeleteFormField_out.pdf");