Read XFA form fields and fill data using Aspose.PDF for .NET

I just downloaded version 7 demo of the pdf component for .NET. I have an XFA PDF Form that needs to be filled out. I can read the xml in the Form.XFA.Datasets property, and I can see a list of values in Form.XFA.FieldNames (which i’m not 100% sure is correct), but I can’t figure out how to read/write to individual fields. Is there an example I can reference?

Hi Alex,

Thanks for your interest in our products.

Kindly check the following documentation links for details and code snippets as per your requirement.

Please feel free to contact support in case you need any further assistance.

Thanks & Regard,

Hi Rashid.


I don’t see an answer to my question in the links you provided. I need to fill out fields in an XFA form. Where is an example that shows that? I see AcroForm filling examples and examples dealing with XML import/export processes but nothing on filling out XFA Forms.

Please advise.

Alex
p.s. What’s the difference between regular pdf namespace and facades in your component?

Hi Alex,

Thanks for sharing the details.

alexfeldman:

I need to fill out fields in an XFA form.

I am pleased to inform you that, this issue already logged in our issue tracking system with ID: PDFNEWNET-33677. Now, the issue has been fixed and will be a part of our next release (Aspose.Pdf for .NET v7.1). Your request has also been linked to the appropriate issue and you will be notified once the version is available for download. Sorry for the inconvenience.

The following code will be used to change the field value.

[C#]
doc.Form.XFA[FieldPath] = FieldValue;
For example:
doc.Form.XFA["GSIT_PICA_GRO_SOLLICITUD.requeridor.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.TXT_NOM"] = "Abcd";

alexfeldman:

p.s. What's the difference between regular pdf namespace and facades in your component?

I would like to inform you that Aspose.Pdf for .NET and Aspose.Pdf.Kit for .NET have been merged into a single product and Aspose.Pdf.Kit for .NET has been discontinued as a separate product. All the features of Aspose.Pdf.Kit for .NET are available under Aspose.Pdf.Facades namespace of Aspose.Pdf for .NET v6.x.

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

Thank you for the update. When do you expect the 7.1 release?

Hi Alex,

Aspose.Pdf for .NET v7.1 will be released in June as per our monthly release plan after following the complete testing and release cycle. Please be patient and we will notify you once the version is available for download.

We apologize for your inconvenience.

Thanks & Regards,

Hi Alex,

Thanks for your patience.

We have further investigated this scenario and following are our observations.

Your source document has JavaScript code which manages field visibility, checks if field was filled correctly etc. Incorrect information may cause error messages or some other effects.

Aspose.PDf allows to fill document form:

1. Document must be edited with incremental update approach. Please try using the code snippet shared over Preserve Extended Rights feature while working with Forms

2. To get all document fields, the following code may be used:

[C#]

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val=“–”/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–>

Document doc = new
Document(src);

XmlNode node = doc.Form.XFA.Template;

XmlNodeList fields = node.SelectNodes(“//tpl:field”,
doc.Form.XFA.NamespaceManager);

foreach (XmlNode field in
fields)

{


string

fieldname = field.Attributes[“name”].Value;


string

templatePath = fieldname;

XmlNode
parent = field.ParentNode;


while
(parent
!= null)

{


if
(parent.Name
== “subform”)

{

string parentName = null;

if (parent.Attributes[“name”]
!= null)

{
parentName = parent.Attributes[“name”].Value;
templatePath = parentName + “.” +
templatePath; }

}

parent
= parent.ParentNode;

}

Console.WriteLine(templatePath);

}


3. Field template has bind entries. So in order to change field value, the following code may be used:

//doc.Form.XFA[FieldPath] = FieldValue;

doc.Form.XFA["GSIT_PICA_GRO_SOLLICITUD.requeridor.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.TXT_NOM"] = "Abcd";


This capability will be available in v7.1.0.

The issues you have found earlier (filed as PDFNEWNET-33677) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi!,

I just test the code posted by Codewarrior and i confirm you that, it works!

Thank you very much.

Best regards.

Hi again Codewarrior...

I have a problem when i am filling an specific form field type.

The form field name are... (to select the state)

GSIT_PICA_GRO_SOLLICITUD.presentador.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.ADRECA_ESTR.LLISTA_PROVINCIES

When i open the PDF to see the result, this error message is showed:

scoGSITAdreca::initProvinciaEsp
message - arrValors[pos] is undefined
fileName - 1
lineNumber - 44

stack -
carregaSeguentLlista([object XFAObject],[object XFAObject],[object Array], "scoGSITMunEspInfo", false)@1:44
carregaSeguentLlista([object XFAObject],[object XFAObject],[object Array], "scoGSITMunEspInfo", false)@:0
initProvinciaEsp([object xFAObject])@1:451
initProvinciaEsp([object xFAObject])@:0
defPanellAdrecaEstr([object XFAObject])@1:1002
defPanellAdrecaEstr([object XFAObject])@:0
defPanellAdrecaEstr([object XFAObject])@1:1868
defPanellAdrecaEstr([object XFAObject])@:0
@XFA:GSIT_PICA_GRO_SOLLICITUD[0]:presentador0]:PERSONA_FISICA_O_JURIDICA[0]:PERSONA_FISICA[0]:DADES_IDENTIFICACIO_PARTICULAR[0]:CONTACTE[0]:ADRECA_ESTR[0]:initialize8

name - TypeError

After this, the PDF is finally opened and i can see the form fiels well done filled.

Why this error happend?.

I attach you the result PDF with form fields filled out. If you try open it you can show these errors.

The same problem happend with equals form fields types, for example with the form field name (to select the city)

GSIT_PICA_GRO_SOLLICITUD.presentador.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.ADRECA_ESTR.LLISTA_PROVINCIES

Why happend this errors?. i'm doing any wrong?. Thanks in advance.

PD.- When you open the PDF, please choose "Actuo en nom de la persona sollicitant (presentador)"


I attach you too the original PDF (with empty form fields) for your tests.

Hi Jose,


Thanks for contacting support. I am able to notice the error message which is occurring when opening the fw5±+copia.pdf file. I am afraid I could not replicate this issue over my end. Can you please share the code snippet which you are using so that we can test the scenario at our end. We are sorry for this inconvenience.

Hi Codewarrior,

The "fw5 - copia.pdf" pdf file is the original pdf (without form field filled out). This file has no problem and i attached you because with it you can start your test trying to fill out these fields. So, instead opening "fw5 - copia.pdf" try to open "fw5.pdf" attached in previous post.

The problem is with "fw5.pdf" file (pdf form filled out). This pdf has filled the state ("provincia") and city ("municipi") and when you open the PDF, these errors happend.

I followed your instructions to fill out the form and it works, but only with these fields when you open the result pdf, it crashes (althought fields were fillout well finally).

Codesnippet:

public const string PRESENTADOR_FISICA_CONTACTE_ADRECA_LLISTA_PROVINCIES = @"GSIT_PICA_GRO_SOLLICITUD.presentador.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.ADRECA_ESTR.LLISTA_PROVINCIES";

public const string PRESENTADOR_FISICA_CONTACTE_ADRECA_LLISTA_MUNICIPIS = @"GSIT_PICA_GRO_SOLLICITUD.presentador.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.ADRECA_ESTR.LLISTA_MUNICIPIS";

FileStream fs = new FileStream(@"C:\fw5.pdf", FileMode.Open, FileAccess.ReadWrite);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(fs);

// Fill the state
pdfDocument.Form.XFA[PRESENTADOR_FISICA_CONTACTE_ADRECA_LLISTA_PROVINCIES] = "Barcelona";

// Fill the city
pdfDocument.Form.XFA[PRESENTADOR_FISICA_CONTACTE_ADRECA_LLISTA_MUNICIPIS] = "Alella";

pdfDocument.Save();
fs.Close();

PD.- When you open the result PDF, please choose "Actuo en nom de la persona sollicitant (presentador)"

Thanks in advance.

Hi Jose,

Thanks for sharing the details.

I have again tested the scenario where I have used Aspose.Pdf for .NET 7.1.0 to fill the source
fw5 - copia.pdf file using the code snippet which you have shared and as per my observations, when I have tried viewing the document, I am unable to notice any issue. Also please note that the data is not being populated/inserted in source PDF form. I have tested the scenario in VisualStudio 2010 project running over Windows 7 X64 Enterprise, and I have specified the target platform of application as .NET Framework 3.5. Can you please share some details regarding your working environment. We are sorry for this inconvenience.

For your reference, I have also attached the resultant PDF that I have generated.

[C#]

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–>

FileStream fs = new
FileStream(@“d:\pdftest\fw5_Filled.pdf”,
FileMode.Open, FileAccess.ReadWrite);

Aspose.Pdf.Document
pdfDocument = new Aspose.Pdf.Document(fs);

// Fill the state

pdfDocument.Form.XFA[“PRESENTADOR_FISICA_CONTACTE_ADRECA_LLISTA_PROVINCIES”]
= “Barcelona”;

// Fill the city

pdfDocument.Form.XFA[“PRESENTADOR_FISICA_CONTACTE_ADRECA_LLISTA_MUNICIPIS”]
= “Alella”;

pdfDocument.Save();

fs.Close();

Hi Nayyerv,

I just opened your result pdf [fw5_Filled.pdf] and form fields are empty.

My working environment is:

VisualStudio 2008 project running over Windows 7 X64 Enterprise.

I'm using Aspose.Pdf v7.1.0 (Framework 3.5)

PDF opened with Adobe Reader v10.1.3

I attach you the javascript message error capture.

Also, i tried to open with an older adobe reader version (for example v9.1) and i obtain the same error messages.

Hi Nayyer, any news about this issue?

Hi Jose,

Thanks for sharing the details.

I have been able to notice the JavaScript error message when I have tried viewing fw5.pdf which you have shared at
398334. But as I have shared in my earlier post, I an unable to replicate this issue when I have tried filling the data inside PDF form. As per my understanding, until the data is populated in PROVINCIES and MUNICIPIS fields, the error message might not appear. Nevertheless, we are working on investigating the issue why data is not appearing in resultant PDF.

For testing purposes, I have used VisualStudio 2010 project running over Windows 7 X64 Enterprise and I have specified .NET Framework 3.5 as target platform for my application.

We are sorry for this inconvenience.

Hi again Hi Nayyer,

I have a new issue. In PDF you can fill out the data of two persons (name, surname, address, etc...)

Person 1 (sol-licitant)

Person 2 (presentador) - OPTIONAL

The scenary is this:

I want fillout the form fields of both of them. To do this, i fill out the form fields by code (using codesnippet posted below). When open the result PDF i choose "Actuo en nom de la persona sol-licitant (presentador)" but only data of Person 2 (presentador) is filled and Person 1 (sol-licitant) data is empty.

If i do the same but only filling out the Person 1 form fields and choosing "Soc la persona interessada (sol-licitant)" when i open the PDF, the form fields data of Person 1 is filled.

What could be the problem on this issue?

Codesnippet in C#:

// Person 1 (SOL-LICITANT)
public const string SOLLICITANT_NOM = @"GSIT_PICA_GRO_SOLLICITUD.requeridor.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.TXT_NOM";
public const string SOLLICITANT_COGNOM1 = @"GSIT_PICA_GRO_SOLLICITUD.requeridor.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.TXT_COGNOM1";
public const string SOLLICITANT_COGNOM2 = @"GSIT_PICA_GRO_SOLLICITUD.requeridor.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.TXT_COGNOM2";

// Person 2 (PRESENTADOR)
public const string PRESENTADOR_NOM = @"GSIT_PICA_GRO_SOLLICITUD.presentador.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.TXT_NOM";
public const string PRESENTADOR_COGNOM1 = @"GSIT_PICA_GRO_SOLLICITUD.presentador.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.TXT_COGNOM1";
public const string PRESENTADOR_COGNOM2 = @"GSIT_PICA_GRO_SOLLICITUD.presentador.PERSONA_FISICA_O_JURIDICA.PERSONA_FISICA.DADES_IDENTIFICACIO_PARTICULAR.CONTACTE.TXT_COGNOM2";

...

FileStream fs = new FileStream("C:\\fw5.pdf", FileMode.Open, FileAccess.ReadWrite);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(fs);

// Fill out the Person 1 (Sol-licitant) form fields
pdfDocument.Form.XFA[SOLLICITANT_NOM] = "ALBERTO";
pdfDocument.Form.XFA[SOLLICITANT_COGNOM] = "GARCÍA";
pdfDocument.Form.XFA[SOLLICITANT_COGNOM2] = "SÁNCHEZ";

// Fill out the Person 2 (Presentador) form fields
pdfDocument.Form.XFA[PRESENTADOR_NOM] = "PETER";
pdfDocument.Form.XFA[PRESENTADOR_COGNOM] = "SMITH";
pdfDocument.Form.XFA[PRESENTADOR_COGNOM2] = "BOILE";

pdfDocument.Save();
fs.Close();

Hi Jose,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 7.1.0 while using the code snippet shared above and as per my observations, when I have selected ‘Actuo en nom de la persona sol·licitant (presentador)’ radio button, I am able to see Person 2 (Presentador) information. Whereas when I have selected ‘Sóc la persona interessada (sol·licitant)’ radio button on resultant PDF, I am able to see Person 1 (Sol-licitant) information. Can you please try viewing the resultant PDF that I have generated and in case I am not able to understand this issue properly, please share further details.

We apologize for your inconvenience,.