Fill XFA form and export XDP data

The following code opens an XFA form and programmatically fills a text field and selects items from drop-down lists and exports the data as XDP.

using (FileStream inFile = new FileStream("Purchase Order.pdf", FileMode.Open, FileAccess.Read))
{
  // open the source document
  Document document = new Document(inFile);

  TextField textField = document.Fields["form1[0].purchaseOrder[0].header[0].txtOrderedByCompanyName[0]"] 
    as TextField;
  textField.Value = "My Company";

  DropDownListField countryField = document.Fields["form1[0].purchaseOrder[0].header[0].drpOrderedByCountry[0]"] 
    as DropDownListField;
  countryField.DropDownListValue = countryField.Options[1]; // second option ('Canada')

  DropDownListField stateField = document.Fields["form1[0].purchaseOrder[0].header[0].drpOrderedByStateProv[0]"] 
    as DropDownListField;
  stateField.DropDownListValue = stateField.Options[2]; // third option ('Manitoba')

  XdpFormData xdp = document.Export(SubmitFormat.Xdp, false) as XdpFormData;
  xdp.Path = "Purchase Order.pdf";

  using (FileStream xdpFile = new FileStream("Purchase Order_data.xdp", FileMode.Create, FileAccess.Write))
  {
    xdp.Write(xdpFile);
  }
}