Aspose.Pdf.Facades.Form. ImportFdf unable to cast object of type 'aspose.pdf.annotations.widgetannotation' to type

When using the Aspose.Pdf.dll ( version 25.1 and 25.5) , calling the Aspose.Pdf.Facades.Form.ImportFdf method results in error "unable to cast object of type ‘aspose.pdf.annotations.widgetannotation’ to type ‘aspose.pdf.forms.field’ ". Please see the code below:

internal void ImportFdf()
{
FileStream fdfStream = new FileStream(“C:\29.fdf”, FileMode.Open);

string pdfFile = "C:\\29.pdf";

// Get the form from the document
Aspose.Pdf.Facades.Form form = new Aspose.Pdf.Facades.Form(pdfFile);
form.ImportFdf(fdfStream);

fdfStream.Dispose();

}

Attached is a zip file containing two files: 29.pdf, 29.fdf

29.zip (241.5 KB)

@ShazAccessGroup

The error you are encountering, “unable to cast object of type ‘aspose.pdf.annotations.widgetannotation’ to type ‘aspose.pdf.forms.field’,” typically indicates a compatibility issue between the FDF data and the PDF form fields. Here are some steps you can take to troubleshoot and potentially resolve this issue:

  1. Check PDF and FDF Compatibility: Ensure that the fields defined in your FDF file correspond correctly to the fields in your PDF form. If there are discrepancies, such as missing fields or mismatched field types, it can lead to casting errors.

  2. Update Aspose.PDF Library: Since you are using versions 25.1 and 25.5, consider updating to the latest version of the Aspose.PDF library. Newer versions often include bug fixes and improvements that may resolve your issue.

  3. Inspect the FDF File: Open the FDF file in a text editor to verify its structure. Ensure that it correctly references the fields in the PDF. If the FDF file is malformed or contains unexpected data types, it could lead to casting issues.

  4. Simplify the FDF Data: If possible, try importing a simpler FDF file with fewer fields to see if the issue persists. This can help isolate whether the problem is with specific fields or the overall structure.

  5. Error Handling: Implement error handling in your code to catch exceptions and log detailed error messages. This can provide more insight into what might be going wrong during the import process.

Here is a modified version of your code with basic error handling:

internal void ImportFdf()
{
    try
    {
        using (FileStream fdfStream = new FileStream("C:\\29.fdf", FileMode.Open))
        {
            string pdfFile = "C:\\29.pdf";
            Aspose.Pdf.Facades.Form form = new Aspose.Pdf.Facades.Form(pdfFile);
            form.ImportFdf(fdfStream);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("An error occurred: " + ex.Message);
    }
}

This code will help you catch any exceptions that occur during the import process and provide a clearer understanding of the issue.

If the problem persists after trying these suggestions, consider reaching out to Aspose support with detailed information about your PDF and FDF files for further assistance.

The error still occurs after making suggested changes

@ShazAccessGroup

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-59919

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.