Frequently Asked Question:
Sample code for filling in form fields in a PDF?
I'm having trouble filling in form fields in my PDF. Do you have any samples for how I can accomplish this task?
This sample code demonstrates how to fill in text form fields.
The data that we're going to use to fill in these form fields could be pulled out of a database or some other source relatively easily, but for this example, we're going to keep it simple.
This sample code also demonstrates how to create text form fields. So the first step will be to create the form fields in our new PDF and then insert data into the form fields and save the PDF to disk.
// Tell the library that all co-ordinates should
// begin from the top left corner of the page.
QP.SetOrigin(1);
// Add the first new form field
var iDf1 = QP.NewFormField("First Name", 1);
QP.SetFormFieldValue(iDf1, "Jane");
QP.SetNeedAppearances(1);
QP.SetFormFieldBounds(iDf1, 20, 20, 100, 20);
QP.SetFormFieldAlignment(iDf1, 2);
// Add the second new form field
var iDf2 = QP.NewFormField("Second Name", 1);
QP.SetFormFieldValue(iDf2, "Doe");
QP.SetNeedAppearances(1);
QP.SetFormFieldBounds(iDf2, 20, 50, 100, 20);
fID = QP.AddTrueTypeFont("Myriad Pro Cond", 1);
ffID = QP.AddFormFont(fID);
QP.SetFormFieldFont(iDf2, ffID);
// Add the third new form field
var iDf3 = QP.NewFormField("Age", 1);
QP.SetFormFieldValue(iDf3, "31");
QP.SetNeedAppearances(1);
QP.SetFormFieldBounds(iDf3, 20, 80, 100, 20);
// Add the fourth new form field
var iDf3 = QP.NewFormField("Nationality", 1);
QP.SetFormFieldValue(iDf3, "Australian");
QP.SetNeedAppearances(1);
QP.SetFormFieldBounds(iDf3, 20, 110, 100, 20);
// Save the new PDF form to the hard disk
QP.SaveToFile("new_pdf_form.pdf");