Create PDF form

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Creation >

Create PDF form

In this section we present example code to create a new PDF document containing a form. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

 

Given below is the sample code.

 

// Create a new PDF document
using (var pdfDoc = new PDF())
{
   // Create a text form field
   var textField = new PDFFormTextField
   {
      FieldName = "ftf_name",
      Rectangle = new SimpleRect(100, 25, 100, 20),
      FieldValue = "",
      BorderWidth = 1
   };
 
   // Create a list box form field
   var listboxField = new PDFFormListBox
   {
      FieldName = "flb_country",
      Rectangle = new SimpleRect(100, 55, 100, 45),
      BorderWidth = 1
   };

 
   // Add options to the list box
   listboxField.AddItem("India");
   listboxField.AddItem("United States");
   listboxField.AddItem("United Kingdom");

 

   // Specify option that is selected by default
   listboxField.DefaultSelectedIndex = 1;
 
   // Create a push button form field
   var pushbuttonField = new PDFFormPushButton
   {
      FieldName = "flb_submit",
      Rectangle = new SimpleRect(100, 125, 100, 20),
      BorderWidth = 1,
      NormalCaption = "Submit",
      // Specify action for the button
      ActionType = PDFFormFieldActionType.Submit_Action
   };
   
   // Specify website URL where form contents should be submitted
   pushbuttonField.SubmitAction.Url = "http://www.host.com/formsubmit.asp";
 
   // Create a blank page and set the measurement unit to points
   var pdfPage = new PDFPage();
   pdfPage.MeasurementUnit = MeasurementUnit.Points;
 
   // Add the form fields to the page
   pdfPage.AddFormField(textField);
   pdfPage.AddFormField(listboxField);
   pdfPage.AddFormField(pushbuttonField);
 
   // Write the labels for the form fields
   pdfPage.WriteText("Name: ", 10, 30);
   pdfPage.WriteText("Country: ", 10, 55);
 
   // Add the page to document
   pdfDoc.AddPage(pdfPage);
 
   // Save the PDF document to a file
   pdfDoc.Save("forms_doc.pdf");
}