Attempt to flatten a field causes an exception

We are trying to flatten the fields in the attached document, and doing so causes an exception. Am I doing something wrong?



The attached document has two fields - one called “Next”, one called “Prev” and both of them are buttons (Aspose.Pdf.Facades.FieldType.PushButton).



To repro, run the following code:



Dim Document = New Aspose.Pdf.Document(SourceFile)

Dim Form = New Aspose.Pdf.Facades.Form(Document)

Form.FlattenField(“Next”)



Running this code on the attached document will cause an Invalid Cast exception with the following message:



Unable to cast object of type ‘Aspose.Pdf.InteractiveFeatures.Annotations.WidgetAnnotation’ to type ‘Aspose.Pdf.InteractiveFeatures.Forms.Field’.



The stack trace is:



at Aspose.Pdf.InteractiveFeatures.Forms.Field.›(String )

at Aspose.Pdf.InteractiveFeatures.Forms.Field.(String )

at Aspose.Pdf.InteractiveFeatures.Forms.Form.(String )

at Aspose.Pdf.InteractiveFeatures.Forms.Form.Delete(String fieldName)

at Aspose.Pdf.InteractiveFeatures.Forms.Form.Delete(Field field)

at Aspose.Pdf.InteractiveFeatures.Forms.Field.Flatten()

at Aspose.Pdf.InteractiveFeatures.Forms.Field.Flatten()

at Aspose.Pdf.Facades.Form.FlattenField(String fieldName)

at Aspose.Pdf_Investigation.AsposeInvestigationForm.RunTextButton_Click(Object sender, EventArgs e) in AsposeInvestigationForm.vb:line 30



Are we doing something wrong? Other discussions I’ve found indicate that flattening a button should result in it being disabled - is that correct, or should we avoid trying to flatten buttons?



Thanks,

Michael Whalen

Hi Michael,


Thanks
for using our API’s.
<o:p></o:p>

I
have tested the scenario and I am able to notice the same problem. For the sake
of correction, I have logged this problem as PDFNEWNET-38624 in
our issue tracking system. We will further look into the details of this
problem and will keep you updated on the status of correction. Please be
patient and spare us little time. We are sorry for this inconvenience.

Hi Michael,


In addition to above reply. As a workaround you try flatten all from fields with FlattenAllFields() method. It will help you to accomplish the task. Please check following code snippet to flatten all field

Document doc = new
Aspose.Pdf.Document(myDir + “FlattenFields_Sample.pdf”);<o:p></o:p>

//create a pdf form...bind pdf using source filename

Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form(doc);

// bind the source PDF file

pdfForm.BindPdf(myDir+"FlattenFields_Sample.pdf");

// flatten form fields

pdfForm.FlattenAllFields();

// save update PDF file

pdfForm.Save(myDir+"FlattenField.pdf");

Please feel free to contact us for any further assistance.


Best Regards,

The issues you have found earlier (filed as PDFNET-38624) have been fixed in Aspose.PDF for .NET 22.7.