Do you support XFA forms?

imm5710e (3).pdf (575.9 KB)

We were trying to get the answers privately but your sales team has no answers and they pushed us to post the questions here on the forum which is very unlike a tech company sales process.

Is there anyone experienced enough in the company who can answer our questions?

@caapp

We apologize for any inconvenience you faced. Yes, Aspose.PDF does support XFA Forms. Please check below documentation section about working with XFA Forms.

Please feel free to let us know in case you have further questions or you face any issues while working with the API.

One of the major issues that we are experiencing with some other libraries is the ability to flatten certain PDF form fields. certain form fields should not be able to be filled/populated once “no” has been ticked.

In your case does the flatten function apply to the whole PDF once it is called?

Form is also attached.
imm5710e (3).pdf (575.9 KB)

@caapp

You can flatten the complete PDF document as well as an individual form field as per your desire. For example:

var pdf = new Aspose.Pdf.Document(dataDir + "input.pdf");
pdf.Flatten(); // flatten entire document
var xfaForm = new Aspose.Pdf.Facades.Form(pdf);
var fieldNames = pdf.Form.XFA.FieldNames;

foreach (var fieldName in fieldNames)
{
    xfaForm.FlattenField(fieldName); // flatten one field
}

// OR
xfaForm.FlattenAllFields(); // flatten all fields

If you can please share complete information of the scenario of yours along with the issue details, we will try to share some more feedback after investigating the case in our environment.

Can you give your personal email address? So that we can explain you everything in detail.

@caapp

We appreciate providing support via our dedication forums. You can share all the details here in this forum thread. OR you can also send a private message if you want to share the details directly.
image.png (16.7 KB)

for testing, we just populated the citizenship using your library. you can see how it performed.Is this how it is going to work?
imm5710e_1.pdf (566.0 KB)

@casapp

We are checking it and will get back to you shortly.

Does it take 26 days to reply?

@casapp

First of all, please accept our humble apology for the delay and the inconvenience you faced. We took the time to test and analyze your document with the existing functionality Aspose.PDF has to offer at the moment. Below are our complete findings and investigation results for your kind reference:

Fill XFA Form Fields Programmatically

Your PDF document can be filled using the API with below code snippet where field names are used to assign respective values. As mentioned in your shared recording, you already have fields mappings that you can use in a way like below code snippet:

using (var fs = new FileStream(dataDir + "sampleXFA.pdf", FileMode.Open, FileAccess.ReadWrite))
{
    var pdf = new Aspose.Pdf.Document(fs);
    // Tests
    pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].ServiceIn[0].UCIClientID[0]"] = "123654";
    pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].Name[0].FamilyName[0]"] = "Aspose";
    pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].q3-4-5[0].pob[0].PlaceBirthCountry[0]"] = "205";
    pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].AliasName[0].AliasNameIndicator[0].AliasNameIndicator[0]"] = "N";
    pdf.Save();
}

@casapp

In addition to the above,

Country DropDown should not be filled with incomplete name

The country dropdown in your PDF is able to except the numeric values which are basically IDs of the text values. We tried with numeric values and the dropdown was prepopulated in the filled PDF output.

pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].q3-4-5[0].pob[0].PlaceBirthCountry[0]"] = GetCountryId("{user-input}");

This will help you preventing the form field filled with incomplete or wrong country name.

You can create a generic method with the country names and IDs mappings which will take the input by user as country name and return its corresponding ID used in the form:

private static string GetCountryId(string countryName)
{
    // Assuming you have a dictionary with country IDs and names
    Dictionary<int, string> countries = new Dictionary<int, string>
    {
        { 1, "CountryA" },
        { 2, "CountryB" },
        // Add more countries as needed
    };

    if (string.IsNullOrEmpty(countryName))
    {
        // Handle the case where the input is empty or null
        return null;
    }

    // Search for the country name in the dictionary
    var countryId = countries.FirstOrDefault(kv => kv.Value.Equals(countryName, StringComparison.OrdinalIgnoreCase)).Key;

    // Check if the country name is found
    if (countryId != 0)
    {
        // Return the corresponding country ID
        return countryId.ToString();
    }
    else
    {
        // Handle the case where the country name is not found
        return null;
    }
}

@casapp

For the 3rd scenario from your recording,

Disable certain field if “No” Checkbox is checked

We were able check the checkbox in your PDF but sadly, API was not able to execute the embedded Javascript behind that control that disables respective textboxes in the form. Therefore, we have generated a feature request as PDFNET-56034 in our issue tracking system to implement this functionality.

Disable the form field through code

We also tried to disable the form field for the scenario like:

a) if “No” is checked, make textboxes readOnly
b) if “Canada” country is selected, provinces dropdown should be enabled, else disabled

We used below code snippet to disable the form fields if certain conditions are met:

using (var fs = new FileStream(dataDir + "sampleXFA1.pdf", FileMode.Open, FileAccess.ReadWrite))
{
    var pdf = new Aspose.Pdf.Document(fs);

    System.Xml.XmlNode template = pdf.Form.XFA.GetFieldTemplate("form1[0].Page1[0].PersonalDetails[0].Name[0].FamilyName[0]");
    System.Xml.XmlAttribute access = template.OwnerDocument.CreateAttribute("access");
    access.Value = "readOnly";
    template.Attributes.Append(access);

    pdf.Save();
}

Sadly, above code disabled the JavaScript entirely in the output PDF and lost its interactive features. This bug is also logged as PDFNET-56035 in our issue management system for the sake of correction.

Please note that the functionality regarding XFA form is already under the phase of improvement and we have been working on developing a new engine/module to deal with such scenarios where a secured XFA form is present in the PDF documents. This is also one of the reasons that we took this much time while waiting the new release to be rolled out. We are trying to enrich the API to offer more options and functionality to deal with different kind of scenarios like yours.

We will certainly prioritize the issue that we logged for your case and include them in our on-going enhancements plan. We will inform you via this very forum thread as soon as any of the tickets is resolved or we have some workaround. We highly appreciate your patience and comprehension in this matter.

In summary, it seems that sticking with Spire would be the best option as we would achieve the same results with your service. correct?

@casapp

We are not certain about the Spider PDF Library/API that you are already using. We have explained the functionalities that you can achieve using the Aspose.PDF in the area of XFA forms at the moment. Also, there are some logged tickets which reference have been shared here. They are under the phase of the investigation and as soon as they are resolved, you will definitely be able to achieve more from Aspose.PDF.

With Spire, we can populate the drop downs throughout the form but it does something to the form such that when we upload it to the govt website, their system considers the form as a scanned form. It modifies something in the form.

@casapp

That library may be flattening the PDF document after processing it due to which you are getting the output like it. You can process the PDF using the code snippet that we shared above and test by uploading the resultant file to your system. In case you notice similar issue, please let us know. We will create another ticket to address it.

@casapp

We made a quick investigation related to this issue.

For now we have some problems with script execution - main focus is on working with inner xml data and script support seems not developed enough.

We have similar tasks in work now but in this case it seems extremely difficult to trigger script verification as you desire.

As workaround we can suggest manually checking values and reset them if checkbutton has “N” value

var inputPdf = inputDir + "56034";
var outputPdf = outputDir + "56034";

using (var fs = new FileStream(inputPdf, FileMode.Open, FileAccess.ReadWrite))
{
    var pdf = new Aspose.Pdf.Document(fs);

    // Tests
    var value = pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].AliasName[0].AliasNameIndicator[0].AliasNameIndicator[0]"];

    if (value == "N")
    {
        pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].ServiceIn[0].UCIClientID[0]"] = null;
        pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].Name[0].FamilyName[0]"] = null;
        pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].q3-4-5[0].pob[0].PlaceBirthCountry[0]"] = null;
    }
    else
    {
        pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].ServiceIn[0].UCIClientID[0]"] = "123654";
        pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].Name[0].FamilyName[0]"] = "Aspose";
        pdf.Form.XFA["form1[0].Page1[0].PersonalDetails[0].q3-4-5[0].pob[0].PlaceBirthCountry[0]"] = "205";
    }

    pdf.Save(outputPdf);
}

We have similar task in progress this week, possibly we will have some more information on the issue after that investigation.