The HiQPdf software component allows you to load a PDF document with interactive forms using the methods described in the Load Documents section. After a PDF document containing a form was loaded, it is possible to fill the form fields with values and then save the modified PDF document.
Fill and Save Forms Demo
In this demo you can learn how to load a PDF document with a form, enter some values from code and then save the modified PDF document. After the PDF document was loaded you can iterate the fields in the Document.Form.Fields collection or you can get a field from collection by index or by name.
Demo Source Code
C#
private void buttonFillAndSavePdf_Click(object sender, EventArgs e) { // load the PDF document with form from file PdfDocument document = PdfDocument.FromFile(textBoxPdf.Text); // get the Check Box field by name from form fields collection and set its value PdfFormField checkBoxField = document.Form.Fields["cb"]; if (checkBoxField != null) checkBoxField.Value = checkBoxChecked.Checked; // get the Text Box field by name from form fields collection and set its value PdfFormField textBoxField = document.Form.Fields["tb"]; if (textBoxField != null) textBoxField.Value = textBoxText.Text; // get the List Box field by name from form fields collection and set its value PdfFormField listBoxField = document.Form.Fields["lb"]; if (listBoxField != null) listBoxField.Value = comboBoxListBoxValue.SelectedItem; // get the Combo Box field by name from form fields collection and set its value PdfFormField comboBoxField = document.Form.Fields["combo"]; if (comboBoxField != null) comboBoxField.Value = comboBoxComboBoxValue.SelectedItem; // get the Radio Buttons Group field by name from form fields collection and set its value PdfFormField radioGroupField = document.Form.Fields["rg"]; if (radioGroupField != null) radioGroupField.Value = radioButton1.Checked ? "rb1" : "rb2"; Cursor = Cursors.WaitCursor; string pdfFile = Application.StartupPath + @"\DemoOutput\FillForms.pdf"; try { document.WriteToFile(pdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message)); return; } finally { document.Close(); Cursor = Cursors.Arrow; } // open the created PDF document try { System.Diagnostics.Process.Start(pdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message)); } }
See Also