Working with XFA Forms

XFA stands for XML Forms Architecture, dynamic forms are based on a XML specification. Essential PDF supports both the dynamic and static XFA forms.

  • In a static form the form’s appearance and layout is fixed, regardless of the field content.
  • Dynamic forms can change in appearance in several ways in response to changes in the data.

Creating a new XFA form

Essential PDF allows you to create and manipulate the XFA form in PDF document by using PdfXfaDocument and PdfXfaForm classes. The PdfXfaFieldCollection class represents the entire field collection of the XFA form.

Adding a new page to the XFA document

The following code example explains how to add a new page using PdfXfaPage class in a PDF XFA document.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");           
//Add the field to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//Close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a text element and add the properties
Dim textElement As New PdfXfaTextElement("Hello World!")
'Add the field to the XFA form
mainForm.Fields.Add(textElement)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'Close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Add the field to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Add the field to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Add the field to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

NOTE

XFA documents are created based on the flow layout, so the pages are sequentially added if the doesn’t have space to fit the field it will automatically move on the next page, so we can’t specify the page and page numbers.

NOTE

PDF supports XFA forms only in Windows Forms, WPF, ASP.NET, ASP.NET MVC, UWP platforms, Xamarin (.NETStandard 2.0 and above), and ASP.NET Core (.NETStandard 2.0 and above).

Adding the XFA document settings

Essential PDF supports various XFA page setting options through PdfXfaPageSettings class, to control the page display.

The below sample illustrates how to create a new PDF document with XFA page size.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Set the page size
document.PageSettings.PageSize = PdfPageSize.A4
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a text element and add the properties
Dim textElement As New PdfXfaTextElement("Hello World!!!")
'Set font
textElement.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
'Add the text element to the XFA form
mainForm.Fields.Add(textElement)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

You can create a custom page size to the PDF document by using following code snippet.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = new SizeF(500,700);
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Set the page size
document.PageSettings.PageSize = New SizeF(500,700)
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a text element and add the properties
Dim textElement As New PdfXfaTextElement("Hello World!!!")
'Set font
textElement.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
'Add the text element to the XFA form
mainForm.Fields.Add(textElement)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = new SizeF(500, 700);
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = new SizeF(500, 700);
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = new SizeF(500, 700);
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

You can change page orientation from portrait to landscape by using PdfXfaPageOrientation Enum. The below code snippet explains this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = new SizeF(500,700);
//Change the page orientation to landscape.
document.PageSettings.PageOrientation = PdfXfaPageOrientation.Landscape;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Set the page size
document.PageSettings.PageSize = New SizeF(500,700)
'Change the page orientation to landscape
document.PageSettings.PageOrientation = PdfXfaPageOrientation.Landscape
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a text element and add the properties
Dim textElement As New PdfXfaTextElement("Hello World!!!")
'Set font
textElement.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
'Add the text element to the XFA form
mainForm.Fields.Add(textElement)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = new SizeF(500, 700);
//Change the page orientation to landscape.
document.PageSettings.PageOrientation = PdfXfaPageOrientation.Landscape;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = new SizeF(500, 700);
//Change the page orientation to landscape.
document.PageSettings.PageOrientation = PdfXfaPageOrientation.Landscape;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = new SizeF(500, 700);
//Change the page orientation to landscape.
document.PageSettings.PageOrientation = PdfXfaPageOrientation.Landscape;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Creating dynamic XFA forms

To create a dynamic XFA forms using PdfXfaType Enum and save the dynamic form document using below example code.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document dynamic form.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Set the page size
document.PageSettings.PageSize = PdfPageSize.A4
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1", xfaPage,xfaPage.GetClientSize().Width)
'Create a text element and add the properties
Dim textElement As New PdfXfaTextElement("Hello World!!!")
'Set font
textElement.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
'Add the text element to the XFA form
mainForm.Fields.Add(textElement)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document dynamic form
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Creating static XFA forms

To create a static XFA forms using PdfXfaType Enum and save the static form document using below example code.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document with static form.
document.Save("XfaForm.pdf",PdfXfaType.Static);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Set the page size
document.PageSettings.PageSize = PdfPageSize.A4
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a text element and add the properties
Dim textElement As New PdfXfaTextElement("Hello World!!!")
'Set font
textElement.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
'Add the text element to the XFA form
mainForm.Fields.Add(textElement)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document with static form
document.Save("XfaForm.pdf",PdfXfaType.Static)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Static);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Static);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Set the page size.
document.PageSettings.PageSize = PdfPageSize.A4;
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!!!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Static);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Creating XFA form fields

Adding the XFA text box field

The below code snippet illustrates how to add a textbox field to a new PDF document using PdfXfaTextBoxField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName",new SizeF(200,20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a textbox field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("FirstName",New SizeF(200,20))
'Set the caption text
textBoxField.Caption.Text = "First Name"
'Set the tool tip
textBoxField.ToolTip = "First Name"
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add a textbox field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(textBoxField);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a textbox field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("FirstName", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "First Name"
'Set the tool tip
textBoxField.ToolTip = "First Name"
'Add the field to the existing XFA form
loadedForm.Fields.Add(textBoxField)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(textBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(textBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(textBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA numeric field

The below code snippet illustrates how to add a numeric field to a new PDF document using PdfXfaNumericField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField",new SizeF(200,20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Add the field to the XFA form.
mainForm.Fields.Add(numericField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a numeric field and add the properties
Dim numericField As New PdfXfaNumericField("numericField",New SizeF(200,20))
'Set the caption text
numericField.Caption.Text = "Numeric Field"
'Add the field to the XFA form
mainForm.Fields.Add(numericField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Add the field to the XFA form.
mainForm.Fields.Add(numericField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Add the field to the XFA form.
mainForm.Fields.Add(numericField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Add the field to the XFA form.
mainForm.Fields.Add(numericField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the numeric field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField",new SizeF(200,20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(numericField);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a numeric field and add the properties
Dim numericField As New PdfXfaNumericField("numericField",New SizeF(200,20))
'Set the caption text
numericField.Caption.Text = "Numeric Field"
'Add the field to the existing XFA form
loadedForm.Fields.Add(numericField)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(numericField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(numericField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(numericField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA combo box field

The below code snippet illustrates how to add a combo box field to a new PDF document using PdfXfaComboBoxField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a combo box field and add the properties.
PdfXfaComboBoxField comboBoxField = new PdfXfaComboBoxField("comboField", new SizeF(200, 20));
//Set the caption text.
comboBoxField.Caption.Text = "Job Title";
//Add the combo box items.
comboBoxField.Items.Add("Development.");
comboBoxField.Items.Add("Support.");
comboBoxField.Items.Add("Documentation.");
//Add the field to the XFA form.
mainForm.Fields.Add(comboBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As PdfXfaDocument = New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As PdfXfaForm = New PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width)
'Create a combo box field and add the properties
Dim comboBoxField As PdfXfaComboBoxField = New PdfXfaComboBoxField("comboField", New SizeF(200, 20))
'Set the caption text
comboBoxField.Caption.Text = "Job Title"
'Add the combo box items
comboBoxField.Items.Add("Development.")
comboBoxField.Items.Add("Support.")
comboBoxField.Items.Add("Documentation.")
'Add the field to the XFA form
mainForm.Fields.Add(comboBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'Close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a combo box field and add the properties.
PdfXfaComboBoxField comboBoxField = new PdfXfaComboBoxField("comboField", new SizeF(200, 20));
//Set the caption text.
comboBoxField.Caption.Text = "Job Title";
//Add the combo box items.
comboBoxField.Items.Add("Development.");
comboBoxField.Items.Add("Support.");
comboBoxField.Items.Add("Documentation.");
//Add the field to the XFA form.
mainForm.Fields.Add(comboBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a combo box field and add the properties.
PdfXfaComboBoxField comboBoxField = new PdfXfaComboBoxField("comboField", new SizeF(200, 20));
//Set the caption text.
comboBoxField.Caption.Text = "Job Title";

//Add the combo box items.
comboBoxField.Items.Add("Development.");
comboBoxField.Items.Add("Support.");
comboBoxField.Items.Add("Documentation.");
//Add the field to the XFA form.
mainForm.Fields.Add(comboBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a combo box field and add the properties.
PdfXfaComboBoxField comboBoxField = new PdfXfaComboBoxField("comboField", new SizeF(200, 20));
//Set the caption text.
comboBoxField.Caption.Text = "Job Title";
//Add the combo box items.
comboBoxField.Items.Add("Development.");
comboBoxField.Items.Add("Support.");
comboBoxField.Items.Add("Documentation.");
//Add the field to the XFA form.
mainForm.Fields.Add(comboBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the combo box field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a combo box field and add the properties.
PdfXfaComboBoxField comboBoxField = new PdfXfaComboBoxField("comboField", new SizeF(200, 20));
//Set the caption text.
comboBoxField.Caption.Text = "Job Title";
//Add the combo box items.
comboBoxField.Items.Add("Development.");
comboBoxField.Items.Add("Support.");
comboBoxField.Items.Add("Documentation.");
//Add the field to the existing XFA form.
loadedForm.Fields.Add(comboBoxField);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a combo box field and add the properties
Dim comboBoxField As New PdfXfaComboBoxField("comboField", New SizeF(200, 20))
'Set the caption text
comboBoxField.Caption.Text = "Job Title"
'Add the combo box items
comboBoxField.Items.Add("Development.")
comboBoxField.Items.Add("Support.")
comboBoxField.Items.Add("Documentation.")
'Add the field to the existing XFA form
loadedForm.Fields.Add(comboBoxField)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a combo box field and add the properties.
PdfXfaComboBoxField comboBoxField = new PdfXfaComboBoxField("comboField", new SizeF(200, 20));
//Set the caption text.
comboBoxField.Caption.Text = "Job Title";
//Add the combo box items.
comboBoxField.Items.Add("Development.");
comboBoxField.Items.Add("Support.");
comboBoxField.Items.Add("Documentation.");
//Add the field to the existing XFA form.
loadedForm.Fields.Add(comboBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a combo box field and add the properties.
PdfXfaComboBoxField comboBoxField = new PdfXfaComboBoxField("comboField", new SizeF(200, 20));
//Set the caption text.
comboBoxField.Caption.Text = "Job Title";
//Add the combo box items.
comboBoxField.Items.Add("Development.");
comboBoxField.Items.Add("Support.");
comboBoxField.Items.Add("Documentation.");
//Add the field to the existing XFA form.
loadedForm.Fields.Add(comboBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a combo box field and add the properties.
PdfXfaComboBoxField comboBoxField = new PdfXfaComboBoxField("comboField", new SizeF(200, 20));
//Set the caption text.
comboBoxField.Caption.Text = "Job Title";
//Add the combo box items.
comboBoxField.Items.Add("Development.");
comboBoxField.Items.Add("Support.");
comboBoxField.Items.Add("Documentation.");
//Add the field to the existing XFA form.
loadedForm.Fields.Add(comboBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA list box field

The below code snippet illustrates how to add a list box field to a new PDF document using PdfXfaListBoxField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a list box field and add the properties.
PdfXfaListBoxField listBoxField = new PdfXfaListBoxField("listBoxField", new SizeF(150, 50));
//Set the caption text.
listBoxField.Caption.Text = "Known Languages";
listBoxField.Caption.Position = PdfXfaPosition.Top;
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center;
//Add the list box items.
listBoxField.Items.Add("English");
listBoxField.Items.Add("French");
listBoxField.Items.Add("German");
//Add the field to the XFA form.
mainForm.Fields.Add(listBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a list box field and add the properties
Dim listBoxField As New PdfXfaListBoxField("listBoxField", New SizeF(150, 50))
'Set the caption text
listBoxField.Caption.Text = "Known Languages"
listBoxField.Caption.Position = PdfXfaPosition.Top
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center
'Add the list box items
listBoxField.Items.Add("English")
listBoxField.Items.Add("French")
listBoxField.Items.Add("German")
'Add the field to the XFA form
mainForm.Fields.Add(listBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a list box field and add the properties.
PdfXfaListBoxField listBoxField = new PdfXfaListBoxField("listBoxField", new SizeF(150, 50));
//Set the caption text.
listBoxField.Caption.Text = "Known Languages";
listBoxField.Caption.Position = PdfXfaPosition.Top;
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center;
//Add the list box items.
listBoxField.Items.Add("English");
listBoxField.Items.Add("French");
listBoxField.Items.Add("German");
//Add the field to the XFA form.
mainForm.Fields.Add(listBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a list box field and add the properties.
PdfXfaListBoxField listBoxField = new PdfXfaListBoxField("listBoxField", new SizeF(150, 50));
//Set the caption text.
listBoxField.Caption.Text = "Known Languages";
listBoxField.Caption.Position = PdfXfaPosition.Top;
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center;
//Add the list box items.
listBoxField.Items.Add("English");
listBoxField.Items.Add("French");
listBoxField.Items.Add("German");
//Add the field to the XFA form.
mainForm.Fields.Add(listBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a list box field and add the properties.
PdfXfaListBoxField listBoxField = new PdfXfaListBoxField("listBoxField", new SizeF(150, 50));
//Set the caption text.
listBoxField.Caption.Text = "Known Languages";
listBoxField.Caption.Position = PdfXfaPosition.Top;
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center;
//Add the list box items.
listBoxField.Items.Add("English");
listBoxField.Items.Add("French");
listBoxField.Items.Add("German");
//Add the field to the XFA form.
mainForm.Fields.Add(listBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the list box field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a list box field and add the properties.
PdfXfaListBoxField listBoxField = new PdfXfaListBoxField("listBoxField", new SizeF(150, 50));
//Set the caption text.
listBoxField.Caption.Text = "Known Languages";
listBoxField.Caption.Position = PdfXfaPosition.Top;
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center;
//Add the list box items.
listBoxField.Items.Add("English");
listBoxField.Items.Add("French");
listBoxField.Items.Add("German");
//Add the field to the existing XFA form.
loadedForm.Fields.Add(listBoxField);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a list box field and add the properties
Dim listBoxField As New PdfXfaListBoxField("listBoxField", New SizeF(150, 50))
'Set the caption text
listBoxField.Caption.Text = "Known Languages"
listBoxField.Caption.Position = PdfXfaPosition.Top
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center
'Add the list box items
listBoxField.Items.Add("English")
listBoxField.Items.Add("French")
listBoxField.Items.Add("German")
'Add the field to the existing XFA form
loadedForm.Fields.Add(listBoxField)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a list box field and add the properties.
PdfXfaListBoxField listBoxField = new PdfXfaListBoxField("listBoxField", new SizeF(150, 50));
//Set the caption text.
listBoxField.Caption.Text = "Known Languages";
listBoxField.Caption.Position = PdfXfaPosition.Top;
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center;
//Add the list box items.
listBoxField.Items.Add("English");
listBoxField.Items.Add("French");
listBoxField.Items.Add("German");
//Add the field to the existing XFA form.
loadedForm.Fields.Add(listBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a list box field and add the properties.
PdfXfaListBoxField listBoxField = new PdfXfaListBoxField("listBoxField", new SizeF(150, 50));
//Set the caption text.
listBoxField.Caption.Text = "Known Languages";
listBoxField.Caption.Position = PdfXfaPosition.Top;
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center;
//Add the list box items.
listBoxField.Items.Add("English");
listBoxField.Items.Add("French");
listBoxField.Items.Add("German");
//Add the field to the existing XFA form.
loadedForm.Fields.Add(listBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a list box field and add the properties.
PdfXfaListBoxField listBoxField = new PdfXfaListBoxField("listBoxField", new SizeF(150, 50));
//Set the caption text.
listBoxField.Caption.Text = "Known Languages";
listBoxField.Caption.Position = PdfXfaPosition.Top;
listBoxField.Caption.HorizontalAlignment = PdfXfaHorizontalAlignment.Center;
//Add the list box items.
listBoxField.Items.Add("English");
listBoxField.Items.Add("French");
listBoxField.Items.Add("German");
//Add the field to the existing XFA form.
loadedForm.Fields.Add(listBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding XFA check box field

The below code snippet illustrates how to add a check box field to a new PDF document using PdfXfaCheckBoxField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a check box field and add the properties.
PdfXfaCheckBoxField checkBoxField = new PdfXfaCheckBoxField("checkBoxField", new SizeF(100, 20));
//Set the caption text.
checkBoxField.Caption.Text = "Check box Field";
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross;
//Add the field to the XFA form.
mainForm.Fields.Add(checkBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a check box field and add the properties
Dim checkBoxField As New PdfXfaCheckBoxField("checkBoxField", New SizeF(100, 20))
'Set the caption text
checkBoxField.Caption.Text = "Check box Field"
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross
'Add the field to the XFA form
mainForm.Fields.Add(checkBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a check box field and add the properties.
PdfXfaCheckBoxField checkBoxField = new PdfXfaCheckBoxField("checkBoxField", new SizeF(100, 20));
//Set the caption text.
checkBoxField.Caption.Text = "Check box Field";
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross;
//Add the field to the XFA form.
mainForm.Fields.Add(checkBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a check box field and add the properties.
PdfXfaCheckBoxField checkBoxField = new PdfXfaCheckBoxField("checkBoxField", new SizeF(100, 20));
//Set the caption text.
checkBoxField.Caption.Text = "Check box Field";
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross;
//Add the field to the XFA form.
mainForm.Fields.Add(checkBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a check box field and add the properties.
PdfXfaCheckBoxField checkBoxField = new PdfXfaCheckBoxField("checkBoxField", new SizeF(100, 20));
//Set the caption text.
checkBoxField.Caption.Text = "Check box Field";
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross;
//Add the field to the XFA form.
mainForm.Fields.Add(checkBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the check box field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a check box field and add the properties.
PdfXfaCheckBoxField checkBoxField = new PdfXfaCheckBoxField("checkBoxField", new SizeF(100, 20));
//Set the caption text.
checkBoxField.Caption.Text = "Check box Field";
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross;
//Add the field to the existing XFA form.
loadedForm.Fields.Add(checkBoxField);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a check box field and add the properties
Dim checkBoxField As New PdfXfaCheckBoxField("checkBoxField", New SizeF(100, 20))
'Set the caption text
checkBoxField.Caption.Text = "Check box Field"
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross
'Add the field to the existing XFA form
loadedForm.Fields.Add(checkBoxField)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a check box field and add the properties.
PdfXfaCheckBoxField checkBoxField = new PdfXfaCheckBoxField("checkBoxField", new SizeF(100, 20));
//Set the caption text.
checkBoxField.Caption.Text = "Check box Field";
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross;
//Add the field to the existing XFA form.
loadedForm.Fields.Add(checkBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a check box field and add the properties.
PdfXfaCheckBoxField checkBoxField = new PdfXfaCheckBoxField("checkBoxField", new SizeF(100, 20));
//Set the caption text.
checkBoxField.Caption.Text = "Check box Field";
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross;
//Add the field to the existing XFA form.
loadedForm.Fields.Add(checkBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a check box field and add the properties.
PdfXfaCheckBoxField checkBoxField = new PdfXfaCheckBoxField("checkBoxField", new SizeF(100, 20));
//Set the caption text.
checkBoxField.Caption.Text = "Check box Field";
checkBoxField.CheckedStyle = PdfXfaCheckedStyle.Cross;
//Add the field to the existing XFA form.
loadedForm.Fields.Add(checkBoxField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA radio button field

The below code snippet illustrates how to add a radio button field to a new PDF document using PdfXfaRadioButtonField class and add it into PdfXfaRadioButtonGroup.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a radio button group.
PdfXfaRadioButtonGroup group = new PdfXfaRadioButtonGroup("radioGroup");
group.FlowDirection = PdfXfaFlowDirection.Vertical;
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField1 = new PdfXfaRadioButtonField("r1", new SizeF(80, 20));
//Set the caption text.
radioButtonField1.Caption.Text = "Male";
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField2 = new PdfXfaRadioButtonField("r2", new SizeF(80, 20));
radioButtonField2.Caption.Text = "Female";
//Add the radio button fields to the radio button group.
group.Items.Add(radioButtonField1);
group.Items.Add(radioButtonField2);
//Add the field to the XFA form.
mainForm.Fields.Add(group);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a radio button group
Dim group As New PdfXfaRadioButtonGroup("radioGroup")
group.FlowDirection = PdfXfaFlowDirection.Vertical
'Create a radio button field and add the properties
Dim radioButtonField1 As New PdfXfaRadioButtonField("r1", New SizeF(80, 20))
'Set the caption text
radioButtonField1.Caption.Text = "Male"
'Create a radio button field and add the properties
Dim radioButtonField2 As New PdfXfaRadioButtonField("r2", New SizeF(80, 20))
radioButtonField2.Caption.Text = "Female"
'Add the radio button fields to the radio button group
group.Items.Add(radioButtonField1)
group.Items.Add(radioButtonField2)
'Add the field to the XFA form
mainForm.Fields.Add(group)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a radio button group.
PdfXfaRadioButtonGroup group = new PdfXfaRadioButtonGroup("radioGroup");
group.FlowDirection = PdfXfaFlowDirection.Vertical;
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField1 = new PdfXfaRadioButtonField("r1", new SizeF(80, 20));
//Set the caption text.
radioButtonField1.Caption.Text = "Male";
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField2 = new PdfXfaRadioButtonField("r2", new SizeF(80, 20));
radioButtonField2.Caption.Text = "Female";
//Add the radio button fields to the radio button group.
group.Items.Add(radioButtonField1);
group.Items.Add(radioButtonField2);
//Add the field to the XFA form.
mainForm.Fields.Add(group);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a radio button group.
PdfXfaRadioButtonGroup group = new PdfXfaRadioButtonGroup("radioGroup");
group.FlowDirection = PdfXfaFlowDirection.Vertical;
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField1 = new PdfXfaRadioButtonField("r1", new SizeF(80, 20));
//Set the caption text.
radioButtonField1.Caption.Text = "Male";
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField2 = new PdfXfaRadioButtonField("r2", new SizeF(80, 20));
radioButtonField2.Caption.Text = "Female";
//Add the radio button fields to the radio button group.
group.Items.Add(radioButtonField1);
group.Items.Add(radioButtonField2);
//Add the field to the XFA form.
mainForm.Fields.Add(group);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a radio button group.
PdfXfaRadioButtonGroup group = new PdfXfaRadioButtonGroup("radioGroup");
group.FlowDirection = PdfXfaFlowDirection.Vertical;
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField1 = new PdfXfaRadioButtonField("r1", new SizeF(80, 20));
//Set the caption text.
radioButtonField1.Caption.Text = "Male";
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField2 = new PdfXfaRadioButtonField("r2", new SizeF(80, 20));
radioButtonField2.Caption.Text = "Female";
//Add the radio button fields to the radio button group.
group.Items.Add(radioButtonField1);
group.Items.Add(radioButtonField2);
//Add the field to the XFA form.
mainForm.Fields.Add(group);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the radio button field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a radio button group.
PdfXfaRadioButtonGroup group = new PdfXfaRadioButtonGroup("radioGroup");
group.FlowDirection = PdfXfaFlowDirection.Vertical;
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField1 = new PdfXfaRadioButtonField("r1", new SizeF(80, 20));
//Set the caption text.
radioButtonField1.Caption.Text = "Male";
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField2 = new PdfXfaRadioButtonField("r2", new SizeF(80, 20));
radioButtonField2.Caption.Text = "Female";
//Add the radio button fields to the radio button group.
group.Items.Add(radioButtonField1);
group.Items.Add(radioButtonField2);
//Add the field to the existing XFA form.
loadedForm.Fields.Add(group);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a radio button group 
Dim group As New PdfXfaRadioButtonGroup("radioGroup")
group.FlowDirection = PdfXfaFlowDirection.Vertical
'Create a radio button field and add the properties
Dim radioButtonField1 As New PdfXfaRadioButtonField("r1", New SizeF(80, 20))
'Set the caption text
radioButtonField1.Caption.Text = "Male"
'Create a radio button field and add the properties
Dim radioButtonField2 As New PdfXfaRadioButtonField("r2", New SizeF(80, 20))
radioButtonField2.Caption.Text = "Female"
'Add the radio button fields to the radio button group
group.Items.Add(radioButtonField1)
group.Items.Add(radioButtonField2)
'Add the field to the existing XFA form
loadedForm.Fields.Add(group)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a radio button group.
PdfXfaRadioButtonGroup group = new PdfXfaRadioButtonGroup("radioGroup");
group.FlowDirection = PdfXfaFlowDirection.Vertical;
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField1 = new PdfXfaRadioButtonField("r1", new SizeF(80, 20));
//Set the caption text.
radioButtonField1.Caption.Text = "Male";
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField2 = new PdfXfaRadioButtonField("r2", new SizeF(80, 20));
radioButtonField2.Caption.Text = "Female";
//Add the radio button fields to the radio button group.
group.Items.Add(radioButtonField1);
group.Items.Add(radioButtonField2);
//Add the field to the existing XFA form.
loadedForm.Fields.Add(group);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a radio button group.
PdfXfaRadioButtonGroup group = new PdfXfaRadioButtonGroup("radioGroup");
group.FlowDirection = PdfXfaFlowDirection.Vertical;
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField1 = new PdfXfaRadioButtonField("r1", new SizeF(80, 20));
//Set the caption text.
radioButtonField1.Caption.Text = "Male";
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField2 = new PdfXfaRadioButtonField("r2", new SizeF(80, 20));
radioButtonField2.Caption.Text = "Female";
//Add the radio button fields to the radio button group.
group.Items.Add(radioButtonField1);
group.Items.Add(radioButtonField2);
//Add the field to the existing XFA form.
loadedForm.Fields.Add(group);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a radio button group.
PdfXfaRadioButtonGroup group = new PdfXfaRadioButtonGroup("radioGroup");
group.FlowDirection = PdfXfaFlowDirection.Vertical;
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField1 = new PdfXfaRadioButtonField("r1", new SizeF(80, 20));
//Set the caption text.
radioButtonField1.Caption.Text = "Male";
//Create a radio button field and add the properties.
PdfXfaRadioButtonField radioButtonField2 = new PdfXfaRadioButtonField("r2", new SizeF(80, 20));
radioButtonField2.Caption.Text = "Female";
//Add the radio button fields to the radio button group.
group.Items.Add(radioButtonField1);
group.Items.Add(radioButtonField2);
//Add the field to the existing XFA form.
loadedForm.Fields.Add(group);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA date time field

The below code snippet illustrates how to add a date time field to a new PDF document using PdfXfaDateTimeField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("date", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date Time Field";
//Set the tool tip.
dateTimeField.ToolTip = "Date Time";
//Add the field to the XFA form.
mainForm.Fields.Add(dateTimeField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a date time field and add the properties
Dim dateTimeField As New PdfXfaDateTimeField("date", New SizeF(200, 20))
'Set the caption text
dateTimeField.Caption.Text = "Date Time Field"
'Set the tool tip
dateTimeField.ToolTip = "Date Time"
'Add the field to the XFA form
mainForm.Fields.Add(dateTimeField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("date", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date Time Field";
//Set the tool tip.
dateTimeField.ToolTip = "Date Time";
//Add the field to the XFA form.
mainForm.Fields.Add(dateTimeField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("date", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date Time Field";
//Set the tool tip.
dateTimeField.ToolTip = "Date Time";
//Add the field to the XFA form.
mainForm.Fields.Add(dateTimeField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("date", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date Time Field";
//Set the tool tip.
dateTimeField.ToolTip = "Date Time";
//Add the field to the XFA form.
mainForm.Fields.Add(dateTimeField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the date time field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("date", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date Time Field";
//Set the tool tip.
dateTimeField.ToolTip = "Date Time";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(dateTimeField);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a date time field and add the properties
Dim dateTimeField As New PdfXfaDateTimeField("date", New SizeF(200, 20))
'Set the caption text
dateTimeField.Caption.Text = "Date Time Field"
'Set the tool tip
dateTimeField.ToolTip = "Date Time"
'Add the field to the existing XFA form
loadedForm.Fields.Add(dateTimeField)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("date", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date Time Field";
//Set the tool tip.
dateTimeField.ToolTip = "Date Time";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(dateTimeField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("date", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date Time Field";
//Set the tool tip.
dateTimeField.ToolTip = "Date Time";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(dateTimeField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("date", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date Time Field";
//Set the tool tip.
dateTimeField.ToolTip = "Date Time";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(dateTimeField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA button field

The below code snippet illustrates how to add a button field to a new PDF document using PdfXfaButtonField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a button field and add the properties.
PdfXfaButtonField buttonField = new PdfXfaButtonField("buttonField", new SizeF(70, 20));
//Set the caption text.
buttonField.Content = "Click";
//Add the field to the XFA form.
mainForm.Fields.Add(buttonField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a button field and add the properties
Dim buttonField As New PdfXfaButtonField("buttonField", New SizeF(70, 20))
'Set the caption text
buttonField.Content = "Click"
'Add the field to the XFA form
mainForm.Fields.Add(buttonField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a button field and add the properties.
PdfXfaButtonField buttonField = new PdfXfaButtonField("buttonField", new SizeF(70, 20));
//Set the caption text.
buttonField.Content = "Click";
//Add the field to the XFA form.
mainForm.Fields.Add(buttonField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a button field and add the properties.
PdfXfaButtonField buttonField = new PdfXfaButtonField("buttonField", new SizeF(70, 20));
//Set the caption text.
buttonField.Content = "Click";
//Add the field to the XFA form.
mainForm.Fields.Add(buttonField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a button field and add the properties.
PdfXfaButtonField buttonField = new PdfXfaButtonField("buttonField", new SizeF(70, 20));
//Set the caption text.
buttonField.Content = "Click";
//Add the field to the XFA form.
mainForm.Fields.Add(buttonField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the button field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a button field and add the properties.
PdfXfaButtonField buttonField = new PdfXfaButtonField("buttonField", new SizeF(70, 20));
//Set the caption text.
buttonField.Content = "Click";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(buttonField);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a button field and add the properties
Dim buttonField As New PdfXfaButtonField("buttonField", New SizeF(70, 20))
'Set the caption text
buttonField.Content = "Click"
'Add the field to the existing XFA form
loadedForm.Fields.Add(buttonField)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a button field and add the properties.
PdfXfaButtonField buttonField = new PdfXfaButtonField("buttonField", new SizeF(70, 20));
//Set the caption text.
buttonField.Content = "Click";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(buttonField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a button field and add the properties.
PdfXfaButtonField buttonField = new PdfXfaButtonField("buttonField", new SizeF(70, 20));
//Set the caption text.
buttonField.Content = "Click";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(buttonField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a button field and add the properties.
PdfXfaButtonField buttonField = new PdfXfaButtonField("buttonField", new SizeF(70, 20));
//Set the caption text.
buttonField.Content = "Click";
//Add the field to the existing XFA form.
loadedForm.Fields.Add(buttonField);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

Adding the XFA text element

The below code snippet illustrates how to add a text element to a new PDF document using PdfXfaTextElement class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a text element and add the properties
Dim textElement As New PdfXfaTextElement("Hello World!")
'Set font
textElement.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
'Add the text element to the XFA form
mainForm.Fields.Add(textElement)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "output.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the XFA form.
mainForm.Fields.Add(textElement);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add a text element to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a text element and add the properties.
PdfXfaTextElement textElement  = new PdfXfaTextElement("Hello World!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the existing XFA form.
loadedForm.Fields.Add(textElement);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a text element and add the properties
Dim textElement As New PdfXfaTextElement("Hello World!")
'Set font
textElement.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold)
'Add the text element to the existing XFA form
loadedForm.Fields.Add(textElement)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the existing XFA form.
loadedForm.Fields.Add(textElement);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the existing XFA form.
loadedForm.Fields.Add(textElement);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a text element and add the properties.
PdfXfaTextElement textElement = new PdfXfaTextElement("Hello World!");
//Set font.
textElement.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Add the text element to the existing XFA form.
loadedForm.Fields.Add(textElement);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA rectangle field

The below code snippet illustrates how to add the rectangle field to a new PDF document using PdfXfaRectangleField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a rectangle field and add the properties.
PdfXfaRectangleField rectangle = new PdfXfaRectangleField("rect1",new SizeF (100,50));
//Set the fill color.
rectangle.Border.FillColor = new PdfXfaSolidBrush(Color.Red);
//Add the rectangle field to the XFA form.
mainForm.Fields.Add(rectangle);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a rectangle field and add the properties
Dim rectangle As New PdfXfaRectangleField("rect1",New SizeF(100,50))
'Set the fill color
rectangle.Border.FillColor = New PdfXfaSolidBrush(Color.Red)
'Add the rectangle field to the XFA form
mainForm.Fields.Add(rectangle)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a rectangle field and add the properties.
PdfXfaRectangleField rectangle = new PdfXfaRectangleField("rect1", new SizeF(100, 50));
//Set the fill color.
rectangle.Border.FillColor = new PdfXfaSolidBrush(Color.FromArgb(0,255,0,0));
//Add the rectangle field to the XFA form.
mainForm.Fields.Add(rectangle);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a rectangle field and add the properties.
PdfXfaRectangleField rectangle = new PdfXfaRectangleField("rect1", new SizeF(100, 50));
//Set the fill color.
rectangle.Border.FillColor = new PdfXfaSolidBrush(Color.FromArgb(0,255,0,0));
//Add the rectangle field to the XFA form.
mainForm.Fields.Add(rectangle);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a rectangle field and add the properties.
PdfXfaRectangleField rectangle = new PdfXfaRectangleField("rect1", new SizeF(100, 50));
//Set the fill color.
rectangle.Border.FillColor = new PdfXfaSolidBrush(Syncfusion.Drawing.Color.FromArgb(0,255,0,0));
//Add the rectangle field to the XFA form.
mainForm.Fields.Add(rectangle);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the rectangle field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a rectangle field and add the properties.
PdfXfaRectangleField rectangle = new PdfXfaRectangleField("rect1", new SizeF(100, 50));
//Set the fill color.
rectangle.Border.FillColor = new PdfXfaSolidBrush(Color.Red);
//Add the rectangle to the existing XFA form.
loadedForm.Fields.Add(rectangle);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a rectangle field and add the properties
Dim rectangle As New PdfXfaRectangleField("rect1", New SizeF(100, 50))
'Set the fill color
rectangle.Border.FillColor = New PdfXfaSolidBrush(Color.Red)
'Add the rectangle to the existing XFA form
loadedForm.Fields.Add(rectangle)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a rectangle field and add the properties.
PdfXfaRectangleField rectangle = new PdfXfaRectangleField("rect1", new SizeF(100, 50));
//Set the fill color.
rectangle.Border.FillColor = new PdfXfaSolidBrush(Color.FromArgb(0,255,0,0));
//Add the rectangle to the existing XFA form.
loadedForm.Fields.Add(rectangle);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a rectangle field and add the properties.
PdfXfaRectangleField rectangle = new PdfXfaRectangleField("rect1", new SizeF(100, 50));
//Set the fill color.
rectangle.Border.FillColor = new PdfXfaSolidBrush(Color.FromArgb(0,255,0,0));
//Add the rectangle to the existing XFA form.
loadedForm.Fields.Add(rectangle);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a rectangle field and add the properties.
PdfXfaRectangleField rectangle = new PdfXfaRectangleField("rect1", new SizeF(100, 50));
//Set the fill color.
rectangle.Border.FillColor = new PdfXfaSolidBrush(Syncfusion.Drawing.Color.FromArgb(0,255,0,0));
//Add the rectangle to the existing XFA form.
loadedForm.Fields.Add(rectangle);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA circle field

The below code snippet illustrates how to add the circle field to a new PDF document using PdfXfaCircleField class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a circle field and add the properties.
PdfXfaCircleField circle = new PdfXfaCircleField("circle", new SizeF(100, 100));
//Set the fill color.
circle.Border.FillColor = new PdfXfaSolidBrush(Color.Red);
//Add the text element to the XFA form.
mainForm.Fields.Add(circle);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a circle field and add the properties
Dim circle As New PdfXfaCircleField("circle", New SizeF(100, 100))
'Set the fill color
circle.Border.FillColor = New PdfXfaSolidBrush(Color.Red)
'Add the text element to the XFA form
mainForm.Fields.Add(circle)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a circle field and add the properties.
PdfXfaCircleField circle = new PdfXfaCircleField("circle", new SizeF(100, 100));
//Set the fill color.
circle.Border.FillColor = new PdfXfaSolidBrush(Color.FromArgb(0,255,0,0));
//Add the text element to the XFA form.
mainForm.Fields.Add(circle);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a circle field and add the properties.
PdfXfaCircleField circle = new PdfXfaCircleField("circle", new SizeF(100, 100));
//Set the fill color.
circle.Border.FillColor = new PdfXfaSolidBrush(Color.FromArgb(0,255,0,0));
//Add the text element to the XFA form.
mainForm.Fields.Add(circle);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a circle field and add the properties.
PdfXfaCircleField circle = new PdfXfaCircleField("circle", new SizeF(100, 100));
//Set the fill color.
circle.Border.FillColor = new PdfXfaSolidBrush(Syncfusion.Drawing.Color.FromArgb(0,255,0,0));
//Add the text element to the XFA form.
mainForm.Fields.Add(circle);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add the circle field to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a circle field and add the properties.
PdfXfaCircleField circle = new PdfXfaCircleField("circle", new SizeF(100, 100));
//Set the fill color.
circle.Border.FillColor = new PdfXfaSolidBrush(Color.Red);
//Add the circle to the existing XFA form.
loadedForm.Fields.Add(circle);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a circle field and add the properties
Dim circle As New PdfXfaCircleField("circle", New SizeF(100, 100))
'Set the fill color
circle.Border.FillColor = New PdfXfaSolidBrush(Color.Red)
'Add the circle to the existing XFA form
loadedForm.Fields.Add(circle)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a circle field and add the properties.
PdfXfaCircleField circle = new PdfXfaCircleField("circle", new SizeF(100, 100));
//Set the fill color.
circle.Border.FillColor = new PdfXfaSolidBrush(Color.FromArgb(0,255,0,0));
//Add the circle to the existing XFA form.
loadedForm.Fields.Add(circle);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a circle field and add the properties.
PdfXfaCircleField circle = new PdfXfaCircleField("circle", new SizeF(100, 100));
//Set the fill color.
circle.Border.FillColor = new PdfXfaSolidBrush(Color.FromArgb(0,255,0,0));
//Add the circle to the existing XFA form.
loadedForm.Fields.Add(circle);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a circle field and add the properties.
PdfXfaCircleField circle = new PdfXfaCircleField("circle", new SizeF(100, 100));
//Set the fill color.
circle.Border.FillColor = new PdfXfaSolidBrush(Syncfusion.Drawing.Color.FromArgb(0,255,0,0));
//Add the circle to the existing XFA form.
loadedForm.Fields.Add(circle);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA line

The below code snippet illustrates how to add a line to a new PDF document using PdfXfaLine class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a line and add the properties.
PdfXfaLine line = new PdfXfaLine(new PointF(0,0),new PointF (200,0),3);
//Set the line color.
line.Color = new PdfColor(Color.Red);
//Add the text line to the XFA form.
mainForm.Fields.Add(line);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a line and add the properties
Dim line As New PdfXfaLine(New PointF(0,0),New PointF(200,0),3)
'Set the line color
line.Color = New PdfColor(Color.Red)
'Add the text line to the XFA form
mainForm.Fields.Add(line)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a line and add the properties.
PdfXfaLine line = new PdfXfaLine(new PointF(0, 0), new PointF(200, 0), 3);
//Set the line color.
line.Color = new PdfColor(Color.FromArgb(0,255,0,0));
//Add the text line to the XFA form.
mainForm.Fields.Add(line);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a line and add the properties.
PdfXfaLine line = new PdfXfaLine(new PointF(0, 0), new PointF(200, 0), 3);
//Set the line color.
line.Color = new PdfColor(Color.FromArgb(0,255,0,0));
//Add the text line to the XFA form.
mainForm.Fields.Add(line);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Create a line and add the properties.
PdfXfaLine line = new PdfXfaLine(new PointF(0, 0), new PointF(200, 0), 3);
//Set the line color.
line.Color = new PdfColor(Syncfusion.Drawing.Color.FromArgb(0,255,0,0));
//Add the text line to the XFA form.
mainForm.Fields.Add(line);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add a line to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a line and add the properties.
PdfXfaLine line = new PdfXfaLine(new PointF(0, 0), new PointF(200, 0), 3);
//Set the line color.
line.Color = new PdfColor(Color.Red);
//Add the line to the existing XFA form.
loadedForm.Fields.Add(line);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a line and add the properties
Dim line As New PdfXfaLine(New PointF(0, 0), New PointF(200, 0), 3)
'Set the line color
line.Color = New PdfColor(Color.Red)
'Add the line to the existing XFA form
loadedForm.Fields.Add(line)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a line and add the properties.
PdfXfaLine line = new PdfXfaLine(new PointF(0, 0), new PointF(200, 0), 3);
//Set the line color.
line.Color = new PdfColor(Color.FromArgb(0,255,0,0));
//Add the line to the existing XFA form.
loadedForm.Fields.Add(line);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a line and add the properties.
PdfXfaLine line = new PdfXfaLine(new PointF(0, 0), new PointF(200, 0), 3);
//Set the line color.
line.Color = new PdfColor(Color.FromArgb(0,255,0,0));
//Add the line to the existing XFA form.
loadedForm.Fields.Add(line);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a line and add the properties.
PdfXfaLine line = new PdfXfaLine(new PointF(0, 0), new PointF(200, 0), 3);
//Set the line color.
line.Color = new PdfColor(Syncfusion.Drawing.Color.FromArgb(0,255,0,0));
//Add the line to the existing XFA form.
loadedForm.Fields.Add(line);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding the XFA image

The below code snippet illustrates how to add an image to a new PDF document using PdfXfaImage class.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();   
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize ().Width);
//Create a image and add the properties.
PdfXfaImage image = new PdfXfaImage("image1", "image.jpg");
//Add the image to the XFA form.
mainForm.Fields.Add(image);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf",PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add a new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form
Dim mainForm As New PdfXfaForm("subform1",xfaPage,xfaPage.GetClientSize().Width)
'Create a image and add the properties
Dim image As New PdfXfaImage("image1", "image.jpg")
'Add the image to the XFA form
mainForm.Fields.Add(image)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf",PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Load the image as stream.
Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.image.jpg");
//Create a image and add the properties.
PdfXfaImage image = new PdfXfaImage("image1", imageStream);
//Add the image to the XFA form.
mainForm.Fields.Add(image);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Load the image as stream.
MemoryStream imageStream = new MemoryStream(File.ReadAllBytes(imageFileName));
//Create a image and add the properties.
PdfXfaImage image = new PdfXfaImage("image1", imageStream);
//Add the image to the XFA form.
mainForm.Fields.Add(image);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add a new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form.
PdfXfaForm mainForm = new PdfXfaForm("subform1", xfaPage, xfaPage.GetClientSize().Width);
//Load the image as stream.
Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.image.jpg");
//Create a image and add the properties.
PdfXfaImage image = new PdfXfaImage("image1", imageStream);
//Add the image to the XFA form.
mainForm.Fields.Add(image);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream, PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

The below code snippet illustrates how to add an image to an existing PDF document.

//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("XfaForm1.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Create a image and add the properties.
PdfXfaImage image = new PdfXfaImage("imgage1", "image.jpg");            
//Add the image to the existing XFA form.
loadedForm.Fields.Add(image);

//Save the document.
loadedDocument.Save("XfaForm.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing XFA document
Dim loadedDocument As New PdfLoadedXfaDocument("XfaForm1.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Create a image and add the properties
Dim image As New PdfXfaImage("imgage1", "image.jpg")
'Add the image to the existing XFA form
loadedForm.Fields.Add(image)

'Save the document
loadedDocument.Save("XfaForm.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.XfaForm.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Load the image as stream.
Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.image.jpg");
//Create a image and add the properties.
PdfXfaImage image = new PdfXfaImage("imgage1", imageStream);
//Add the image to the existing XFA form.
loadedForm.Fields.Add(image);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Load the image as stream.
MemoryStream imageStream = new MemoryStream(File.ReadAllBytes(imageFileName));
//Create a image and add the properties.
PdfXfaImage image = new PdfXfaImage("imgage1", imageStream);
//Add the image to the existing XFA form.
loadedForm.Fields.Add(image);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Load the image as stream.
Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.image.jpg");
//Create a image and add the properties.
PdfXfaImage image = new PdfXfaImage("imgage1", imageStream);
//Add the image to the existing XFA form.
loadedForm.Fields.Add(image);

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Working with XFA form flow directions

There are two types of flow directions available in XFA forms.

  1. Horizontal
  2. Vertical

Horizontal flow direction

You can set the flow direction to an XFA form while creating, using PdfXfaFlowDirection Enum. The below sample illustrate how to set the horizontal flow direction in XFA forms.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField1 = new PdfXfaTextBoxField("LastName", new SizeF(200, 20));
//Set the caption text.
textBoxField1.Caption.Text = "Last Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a textbox field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("FirstName", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "First Name"
'Set the tool tip
textBoxField.ToolTip = "First Name"
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Create a textbox field and add the properties
Dim textBoxField1 As New PdfXfaTextBoxField("LastName", New SizeF(200, 20))
'Set the caption text
textBoxField1.Caption.Text = "Last Name"
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField1)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField1 = new PdfXfaTextBoxField("LastName", new SizeF(200, 20));
//Set the caption text.
textBoxField1.Caption.Text = "Last Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField1 = new PdfXfaTextBoxField("LastName", new SizeF(200, 20));
//Set the caption text.
textBoxField1.Caption.Text = "Last Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Add the field to the XFA form.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField1 = new PdfXfaTextBoxField("LastName", new SizeF(200, 20));
//Set the caption text.
textBoxField1.Caption.Text = "Last Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Vertical flow direction

You can set the flow direction to an XFA form while creating, using PdfXfaFlowDirection Enum. The below sample illustrate how to set the vertical flow direction in XFA forms.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with vertical flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Vertical, xfaPage.GetClientSize ().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField1 = new PdfXfaTextBoxField("LastName", new SizeF(200, 20));
//Set the caption text.
textBoxField1.Caption.Text = "Last Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with vertical flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Vertical, xfaPage.GetClientSize().Width)
'Create a textbox field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("FirstName", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "First Name"
'Set the tool tip
textBoxField.ToolTip = "First Name"
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Create a textbox field and add the properties
Dim textBoxField1 As New PdfXfaTextBoxField("LastName", New SizeF(200, 20))
'Set the caption text
textBoxField1.Caption.Text = "Last Name"
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField1)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with vertical flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Vertical, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField1 = new PdfXfaTextBoxField("LastName", new SizeF(200, 20));
//Set the caption text.
textBoxField1.Caption.Text = "Last Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with vertical flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Vertical, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField1 = new PdfXfaTextBoxField("LastName", new SizeF(200, 20));
//Set the caption text.
textBoxField1.Caption.Text = "Last Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with vertical flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Vertical, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField1 = new PdfXfaTextBoxField("LastName", new SizeF(200, 20));
//Set the caption text.
textBoxField1.Caption.Text = "Last Name";
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Rotating XFA form fields

You can rotate a form field in XFA document, using PdfXfaRotateAngle Enum. The following code snippet explains this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Set the fields rotation.
textBoxField.Rotate = PdfXfaRotateAngle.RotateAngle90;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a textbox field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("FirstName", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "First Name"
'Set the tool tip
textBoxField.ToolTip = "First Name"
'Set the fields rotation
textBoxField.Rotate = PdfXfaRotateAngle.RotateAngle90
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Set the fields rotation.
textBoxField.Rotate = PdfXfaRotateAngle.RotateAngle90;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Set the fields rotation.
textBoxField.Rotate = PdfXfaRotateAngle.RotateAngle90;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a textbox field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("FirstName", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "First Name";
//Set the tool tip.
textBoxField.ToolTip = "First Name";
//Set the fields rotation.
textBoxField.Rotate = PdfXfaRotateAngle.RotateAngle90;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Validating the date time field

You can validate the date time fields of the input text by enabling the RequireValidation property of PdfXfaDateTimeField. The following code snippet illustrates this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("dateTimeField", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date of Birth";
//Set the date pattern.
dateTimeField.DatePattern = PdfXfaDatePattern.DDMMMMYYYY;
//Enable the validation.
dateTimeField.RequireValidation = true;
//Add the field to the XFA form.
mainForm.Fields.Add(dateTimeField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a date time field and add the properties
Dim dateTimeField As New PdfXfaDateTimeField("dateTimeField", New SizeF(200, 20))
'Set the caption text
dateTimeField.Caption.Text = "Date of Birth"
'Set the date pattern
dateTimeField.DatePattern = PdfXfaDatePattern.DDMMMMYYYY
'Enable the validation
dateTimeField.RequireValidation = True
'Add the field to the XFA form
mainForm.Fields.Add(dateTimeField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("dateTimeField", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date of Birth";
//Set the date pattern.
dateTimeField.DatePattern = PdfXfaDatePattern.DDMMMMYYYY;
//Enable the validation.
dateTimeField.RequireValidation = true;
//Add the field to the XFA form.
mainForm.Fields.Add(dateTimeField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("dateTimeField", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date of Birth";
//Set the date pattern.
dateTimeField.DatePattern = PdfXfaDatePattern.DDMMMMYYYY;
//Enable the validation.
dateTimeField.RequireValidation = true;
//Add the field to the XFA form.
mainForm.Fields.Add(dateTimeField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a date time field and add the properties.
PdfXfaDateTimeField dateTimeField = new PdfXfaDateTimeField("dateTimeField", new SizeF(200, 20));
//Set the caption text.
dateTimeField.Caption.Text = "Date of Birth";
//Set the date pattern.
dateTimeField.DatePattern = PdfXfaDatePattern.DDMMMMYYYY;
//Enable the validation.
dateTimeField.RequireValidation = true;
//Add the field to the XFA form.
mainForm.Fields.Add(dateTimeField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Customizing the numeric field value

You can customize the numeric field input value to the specific pattern through PatternString property of PdfXfaNumericField class. The following code snippet explains this.
Please refer the below link for numeric pattern format in detail,
Numeric Pattern.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Set the pattern string.
numericField.PatternString = "zzzzzzzzz9";
//Add the field to the XFA form.
mainForm.Fields.Add(numericField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a numeric field and add the properties
Dim numericField As New PdfXfaNumericField("numericField", New SizeF(200, 20))
'Set the caption text
numericField.Caption.Text = "Numeric Field"
'Set the pattern string
numericField.PatternString = "zzzzzzzzz9"
'Add the field to the XFA form
mainForm.Fields.Add(numericField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Set the pattern string.
numericField.PatternString = "zzzzzzzzz9";
//Add the field to the XFA form.
mainForm.Fields.Add(numericField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Set the pattern string.
numericField.PatternString = "zzzzzzzzz9";
//Add the field to the XFA form.
mainForm.Fields.Add(numericField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a numeric field and add the properties.
PdfXfaNumericField numericField = new PdfXfaNumericField("numericField", new SizeF(200, 20));
//Set the caption text.
numericField.Caption.Text = "Numeric Field";
//Set the pattern string.
numericField.PatternString = "zzzzzzzzz9";
//Add the field to the XFA form.
mainForm.Fields.Add(numericField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding nested sub forms

You can add the nested sub forms by using PdfXfaForm class. The below code snippet explains this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a font.
PdfFont font = new PdfStandardFont(xfaPage,PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold);
//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a text element.
PdfXfaTextElement element = new PdfXfaTextElement("Main Form", font, 400, 20);                
//Add the field to the XFA form.
mainForm.Fields.Add(element);
//Create a form.
PdfXfaForm form1 = new PdfXfaForm(PdfXfaFlowDirection.Vertical, 400);
//Create a text element.
PdfXfaTextElement element1 = new PdfXfaTextElement("First Form", font, 400, 20);
form1.Fields.Add(element1);
//Create a form.
PdfXfaForm form2 = new PdfXfaForm(PdfXfaFlowDirection.Horizontal, 400);
//Create a text element.
PdfXfaTextElement element2 = new PdfXfaTextElement ("Second Form", font,400,20);
form2.Fields.Add(element2);
form1.Fields.Add(form2);
//Add the field to the XFA form.
mainForm.Fields.Add(form1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Static);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a font
Dim font As PdfFont = New PdfStandardFont(xfaPage,PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold)
'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a text element
Dim element As New PdfXfaTextElement("Main Form", font, 400, 20)
'Add the field to the XFA form
mainForm.Fields.Add(element)
'Create a form
Dim form1 As New PdfXfaForm(PdfXfaFlowDirection.Vertical, 400)
'Create a text element
Dim element1 As New PdfXfaTextElement("First Form", font, 400, 20)
form1.Fields.Add(element1)
'Create a new PDF XFA form with horizontal flow direction
Dim form2 As New PdfXfaForm(PdfXfaFlowDirection.Horizontal, 400)
'Create a text element
Dim element2 As New PdfXfaTextElement("Second Form", font,400,20)
form2.Fields.Add(element2)
form1.Fields.Add(form2)
'Add the field to the XFA form
mainForm.Fields.Add(form1)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Static)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold);
//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text element.
PdfXfaTextElement element = new PdfXfaTextElement("Main Form", font, 400, 20);
//Add the field to the XFA form.
mainForm.Fields.Add(element);
//Create a form.
PdfXfaForm form1 = new PdfXfaForm(PdfXfaFlowDirection.Vertical, 400);
//Create a text element.
PdfXfaTextElement element1 = new PdfXfaTextElement("First Form", font, 400, 20);
form1.Fields.Add(element1);
//Create a form.
PdfXfaForm form2 = new PdfXfaForm(PdfXfaFlowDirection.Horizontal, 400);
//Create a text element.
PdfXfaTextElement element2 = new PdfXfaTextElement("Second Form", font, 400, 20);
form2.Fields.Add(element2);
form1.Fields.Add(form2);
//Add the field to the XFA form.
mainForm.Fields.Add(form1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold);
//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text element.
PdfXfaTextElement element = new PdfXfaTextElement("Main Form", font, 400, 20);
//Add the field to the XFA form.
mainForm.Fields.Add(element);
//Create a form.
PdfXfaForm form1 = new PdfXfaForm(PdfXfaFlowDirection.Vertical, 400);
//Create a text element.
PdfXfaTextElement element1 = new PdfXfaTextElement("First Form", font, 400, 20);
form1.Fields.Add(element1);
//Create a form.
PdfXfaForm form2 = new PdfXfaForm(PdfXfaFlowDirection.Horizontal, 400);
//Create a text element.
PdfXfaTextElement element2 = new PdfXfaTextElement("Second Form", font, 400, 20);
form2.Fields.Add(element2);
form1.Fields.Add(form2);
//Add the field to the XFA form.
mainForm.Fields.Add(form1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold);
//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text element.
PdfXfaTextElement element = new PdfXfaTextElement("Main Form", font, 400, 20);
//Add the field to the XFA form.
mainForm.Fields.Add(element);
//Create a form.
PdfXfaForm form1 = new PdfXfaForm(PdfXfaFlowDirection.Vertical, 400);
//Create a text element.
PdfXfaTextElement element1 = new PdfXfaTextElement("First Form", font, 400, 20);
form1.Fields.Add(element1);
//Create a form.
PdfXfaForm form2 = new PdfXfaForm(PdfXfaFlowDirection.Horizontal, 400);
//Create a text element.
PdfXfaTextElement element2 = new PdfXfaTextElement("Second Form", font, 400, 20);
form2.Fields.Add(element2);
form1.Fields.Add(form2);
//Add the field to the XFA form.
mainForm.Fields.Add(form1);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Formatting XFA form fields

Working with alignments

XFA support both the horizontal and vertical alignments.

Horizontal alignment

You can add the horizontal alignments in XFA form fields through HorizontalAlignment property by using PdfXfaHorizontalAlignment Enum. The below code snippet illustrates this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the horizontal alignment.
textBoxField.HorizontalAlignment = PdfXfaHorizontalAlignment.Right;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a text box field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("textBoxField", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "Fist Name"
'Set the horizontal alignment 
textBoxField.HorizontalAlignment = PdfXfaHorizontalAlignment.Right
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the horizontal alignment.
textBoxField.HorizontalAlignment = PdfXfaHorizontalAlignment.Right;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the horizontal alignment.
textBoxField.HorizontalAlignment = PdfXfaHorizontalAlignment.Right;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the horizontal alignment.
textBoxField.HorizontalAlignment = PdfXfaHorizontalAlignment.Right;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Vertical alignment

You can add the vertical alignments in XFA form fields through VerticalAlignment property by using PdfXfaVerticalAlignment Enum. The below code snippet explains this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(XfaPagePdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the vertical alignment.
textBoxField.VerticalAlignment = PdfXfaVerticalAlignment.Bottom;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(XfaPagePdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a text box field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("textBoxField", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "Fist Name"
'Set the vertical alignment 
textBoxField.VerticalAlignment = PdfXfaVerticalAlignment.Bottom
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the vertical alignment.
textBoxField.VerticalAlignment = PdfXfaVerticalAlignment.Bottom;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the vertical alignment.
textBoxField.VerticalAlignment = PdfXfaVerticalAlignment.Bottom;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the vertical alignment.
textBoxField.VerticalAlignment = PdfXfaVerticalAlignment.Bottom;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding margins to the XFA fields

You can add margin to the XFA form fields by using Margins property. The below code snippet explains this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the margins.
textBoxField.Margins.Left = 10;
textBoxField.Margins.Right = 5;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a text box field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("textBoxField", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "Fist Name"
'Set the margins 
textBoxField.Margins.Left = 10
textBoxField.Margins.Right = 5
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the margins.
textBoxField.Margins.Left = 10;
textBoxField.Margins.Right = 5;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the margins.
textBoxField.Margins.Left = 10;
textBoxField.Margins.Right = 5;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the margins.
textBoxField.Margins.Left = 10;
textBoxField.Margins.Right = 5;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding padding to the XFA fields

You can add padding to the XFA form fields by using padding property. The below code snippet explains this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the paddings.
textBoxField.Padding.All = 2;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a text box field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("textBoxField", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "Fist Name"
'Set the paddings 
textBoxField.Padding.All = 2
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the paddings.
textBoxField.Padding.All = 2;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the paddings.
textBoxField.Padding.All = 2;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "XfaForm.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the paddings.
textBoxField.Padding.All = 2;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Adding border to the XFA fields

You can add border to the XFA fields by using Border property. The below code snippet explains this.

//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize ().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the border color and width.
textBoxField.Border.Color = new PdfColor (200,123,0);
textBoxField.Border.Width = 2;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Save the document.
document.Save("XfaForm.pdf", PdfXfaType.Dynamic);
//close the document.
document.Close();
'Create a new PDF XFA document
Dim document As New PdfXfaDocument()
'Add new XFA page
Dim xfaPage As PdfXfaPage = document.Pages.Add()

'Create a new PDF XFA form with horizontal flow direction
Dim mainForm As New PdfXfaForm(xfaPage,PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width)
'Create a text box field and add the properties
Dim textBoxField As New PdfXfaTextBoxField("textBoxField", New SizeF(200, 20))
'Set the caption text
textBoxField.Caption.Text = "Fist Name"
'Set the border color and width 
textBoxField.Border.Color = New PdfColor(200,123,0)
textBoxField.Border.Width = 2
'Add the field to the XFA form
mainForm.Fields.Add(textBoxField)
'Add the XFA form to the document
document.XfaForm = mainForm

'Save the document
document.Save("XfaForm.pdf", PdfXfaType.Dynamic)
'close the document
document.Close()
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the border color and width.
textBoxField.Border.Color = new PdfColor(200, 123, 0);
textBoxField.Border.Width = 2;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//Close the document.
document.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "XfaForm.pdf");
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the border color and width.
textBoxField.Border.Color = new PdfColor(200, 123, 0);
textBoxField.Border.Width = 2;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new PDF XFA document.
PdfXfaDocument document = new PdfXfaDocument();
//Add new XFA page.
PdfXfaPage xfaPage = document.Pages.Add();

//Create a new PDF XFA form with horizontal flow direction.
PdfXfaForm mainForm = new PdfXfaForm(xfaPage, PdfXfaFlowDirection.Horizontal, xfaPage.GetClientSize().Width);
//Create a text box field and add the properties.
PdfXfaTextBoxField textBoxField = new PdfXfaTextBoxField("textBoxField", new SizeF(200, 20));
//Set the caption text.
textBoxField.Caption.Text = "Fist Name";
//Set the border color and width.
textBoxField.Border.Color = new PdfColor(200, 123, 0);
textBoxField.Border.Width = 2;
//Add the field to the XFA form.
mainForm.Fields.Add(textBoxField);
//Add the XFA form to the document.
document.XfaForm = mainForm;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
document.Save(stream,PdfXfaType.Dynamic);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Filling form fields in an existing XFA document

Essential PDF allows you to fill the XFA form fields using PdfLoadedXfaform class.

Filling the XFA text box field

Please refer the below sample for filling the XFA text box field using PdfLoadedXfaTextBoxField class.

//Load the existing PDF document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("input.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded text box field.
PdfLoadedXfaTextBoxField loadedTextBox = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["text[0]"] as PdfLoadedXfaTextBoxField;
//fill the text box.
loadedTextBox.Text = "First Name";

//Save the document.
loadedDocument.Save("output.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing PDF document
Dim loadedDocument As New PdfLoadedXfaDocument("input.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Get the loaded text box field
Dim loadedTextBox As PdfLoadedXfaTextBoxField = TryCast((TryCast(loadedForm.Fields("subform1[0]"), PdfLoadedXfaForm)).Fields("text[0]"), PdfLoadedXfaTextBoxField)
'fill the text box
loadedTextBox.Text = "First Name"

'Save the document 
loadedDocument.Save("output.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded text box field.
PdfLoadedXfaTextBoxField loadedTextBox = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["text[0]"] as PdfLoadedXfaTextBoxField;
//fill the text box.
loadedTextBox.Text = "First Name";

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "output.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded text box field.
PdfLoadedXfaTextBoxField loadedTextBox = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["text[0]"] as PdfLoadedXfaTextBoxField;
//fill the text box.
loadedTextBox.Text = "First Name";

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded text box field.
PdfLoadedXfaTextBoxField loadedTextBox = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["text[0]"] as PdfLoadedXfaTextBoxField;
//fill the text box.
loadedTextBox.Text = "First Name";

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Filling the XFA numeric field

You can fill the XFA numeric field by using PdfLoadedXfaNumericField class. The below code snippet illustrates this.

//Load the existing PDF document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("input.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded numeric field.
PdfLoadedXfaNumericField loadedNumericField = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["numericField[0]"] as PdfLoadedXfaNumericField;
//fill the numeric field.
loadedNumericField.NumericValue = 945322;

//Save the document.
loadedDocument.Save("output.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing PDF document
Dim loadedDocument As New PdfLoadedXfaDocument("input.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Get the loaded numeric field
Dim loadedNumericField As PdfLoadedXfaNumericField = TryCast((TryCast(loadedForm.Fields("subform1[0]"), PdfLoadedXfaForm)).Fields("numericField[0]"), PdfLoadedXfaNumericField)
'fill the numeric field
loadedNumericField.NumericValue = 945322

'Save the document 
loadedDocument.Save("output.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded numeric field.
PdfLoadedXfaNumericField loadedNumericField = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["numericField[0]"] as PdfLoadedXfaNumericField;
//fill the numeric field.
loadedNumericField.NumericValue = 945322;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "output.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded numeric field.
PdfLoadedXfaNumericField loadedNumericField = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["numericField[0]"] as PdfLoadedXfaNumericField;
//fill the numeric field.
loadedNumericField.NumericValue = 945322;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded numeric field.
PdfLoadedXfaNumericField loadedNumericField = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["numericField[0]"] as PdfLoadedXfaNumericField;
//fill the numeric field.
loadedNumericField.NumericValue = 945322;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("XfaForm.pdf", "application/pdf", fdfStream);
}

You can download a complete working sample from GitHub.

Filling the XFA combo box field

You can fill the XFA combo box field by using PdfLoadedXfaComboBoxField class. The below code snippet explains this.

//Load the existing PDF document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument("input.pdf");
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded combo box field.
PdfLoadedXfaComboBoxField loadedComboBoxField = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["comboBoxField[0]"] as PdfLoadedXfaComboBoxField;
//Set the combo box selected index.
loadedComboBoxField.SelectedIndex = 1;

//Save the document.
loadedDocument.Save("output.pdf");
//Close the document.
loadedDocument.Close();
'Load the existing PDF document
Dim loadedDocument As New PdfLoadedXfaDocument("input.pdf")
'Load the existing XFA form
Dim loadedForm As PdfLoadedXfaForm = loadedDocument.XfaForm

'Get the loaded combo box field
Dim loadedComboBoxField As PdfLoadedXfaComboBoxField = TryCast((TryCast(loadedForm.Fields("subform1[0]"), PdfLoadedXfaForm)).Fields("comboBoxField[0]"), PdfLoadedXfaComboBoxField)
'Set the combo box selected index
loadedComboBoxField.SelectedIndex = 1

'Save the document 
loadedDocument.Save("output.pdf")
'Close the document
loadedDocument.Close()
//Load the PDF document as stream.
Stream docStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded combo box field.
PdfLoadedXfaComboBoxField loadedComboBoxField = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["comboBoxField[0]"] as PdfLoadedXfaComboBoxField;
//Set the combo box selected index.
loadedComboBoxField.SelectedIndex = 1;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//Close the document.
loadedDocument.Close();
//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.
Save(stream, "output.pdf");
//Load the PDF document.
FileStream docStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded combo box field.
PdfLoadedXfaComboBoxField loadedComboBoxField = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["comboBoxField[0]"] as PdfLoadedXfaComboBoxField;
//Set the combo box selected index.
loadedComboBoxField.SelectedIndex = 1;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Defining the ContentType for pdf file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Load the PDF document.
Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.pdf");
//Load the existing XFA document.
PdfLoadedXfaDocument loadedDocument = new PdfLoadedXfaDocument(docStream);
//Load the existing XFA form.
PdfLoadedXfaForm loadedForm = loadedDocument.XfaForm;

//Get the loaded combo box field.
PdfLoadedXfaComboBoxField loadedComboBoxField = (loadedForm.Fields["subform1[0]"] as PdfLoadedXfaForm).Fields["comboBoxField[0]"] as PdfLoadedXfaComboBoxField;
//Set the combo box selected index.
loadedComboBoxField.SelectedIndex = 1;

//Create memory stream.
MemoryStream stream = new MemoryStream();
//Save the document to memory stream.
loadedDocument.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
loadedDocument.Close();
//Save the stream into XFDF file.
//The operation in Save under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the PDF/Xamarin section for respective code samples.