Add PDF Form Fields
Add Form Field in an Existing PDF file
In order to add a form field in an existing PDF file, you need to use AddField method of FormEditor class. This method requires you to specify the type of the field you want to add along with the name and location of the field. You need to create an object of FormEditor class, use AddField method to add a new field in the PDF, Also, you can specify a limit on the number of characters in your field with SetFieldLimit and finally use Save method to save the updated PDF file. The following code snippet shows you how to add form field in an existing PDF file.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddField()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Create an instance of FormEditor to manipulate form fields
using (var editor = new Aspose.Pdf.Facades.FormEditor())
{
// Bind PDF document
editor.BindPdf(dataDir + "Sample-Form-01.pdf");
// Add a text field named "Country" to the first page of the PDF
// Specify the coordinates of the field (left, bottom, right, top)
editor.AddField(Aspose.Pdf.Facades.FieldType.Text, "Country", 1, 232.56f, 496.75f, 352.28f, 514.03f);
// Set a character limit for the "Country" field to 20 characters
editor.SetFieldLimit("Country", 20);
// Save PDF document
editor.Save(dataDir + "Sample-Form-01-mod.pdf");
}
}
Add Submit Button URL in an Existing PDF File
AddSubmitBtn method allows you to set the submit button’s URL in a PDF file. This is the URL where the data is posted when the submit button is clicked. In our example code, we specify the URL, the name of our field, the page number to which we want to add, and the button placement coordinates. AddSubmitBtn method required the name of the submit button field and the URL. This method is provided by FormEditor class. The following code snippet shows you how to set submit button URL.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddSubmitBtn()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Create an instance of FormEditor to manipulate form fields
using (var editor = new Aspose.Pdf.Facades.FormEditor())
{
// Bind PDF document
editor.BindPdf(dataDir + "Sample-Form-01.pdf");
// Add a submit button named "Submit" to the first page of the PDF
// Specify the button text ("Submit"), the URL to which the form data will be submitted,
// and the coordinates of the button (left, bottom, right, top)
editor.AddSubmitBtn("Submit", 1, "Submit", "http://localhost:3000", 232.56f, 466.75f, 352.28f, 484.03f);
// Save PDF document
editor.Save(dataDir + "Sample-Form-01-mod.pdf");
}
}
Add JavaScript for Push Button
AddFieldScript method allows you to add JavaScript to a push button in a PDF file. This method requires the name of the push button and the JavaScript . This method is provided by FormEditor class. The following code snippet shows you how to set JavaScript to a push button.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddFieldScript()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Create an instance of FormEditor to manipulate form fields
using (var editor = new Aspose.Pdf.Facades.FormEditor())
{
// Bind PDF document
editor.BindPdf(dataDir + "Sample-Form-01.pdf");
// Add a JavaScript action to the field named "Last Name"
// The script displays an alert box with the message "Only one last name"
editor.AddFieldScript("Last Name", "app.alert(\"Only one last name\",3);");
// Save PDF document
editor.Save(dataDir + "Sample-Form-01-mod.pdf");
}
}