Vb.net: Need to loop through all pdf checkboxes and change check to X

In vb.net, how do I identify the checkboxes in a pdf form, look through them, and replace any that are checked with an X?

Hi Regan,

Thanks for your inquiry. You can iterate through the form fields and check formfield type to know about the checkbox fields and change the BoxStyle to cross as following. Hopefully it will help you to accomplish the task.

Dim pdf1 = New Document(myDir + "input_checkbox.pdf")
Dim pdfForm = New Aspose.Pdf.Facades.Form(pdf1)

For Each field As var In pdf1.Form.Fields
       Dim fieldFullName = field.FullName
       Dim facadeField = pdfForm.GetFieldFacade(fieldFullName)
       Dim fieldType = pdfForm.GetFieldType(fieldFullName)
       If fieldType.ToString() = "CheckBox" Then
              TryCast(pdf1.Form(fieldFullName), CheckboxField).Style = BoxStyle.Cross
              Console.WriteLine("Field Name: {0}, Field Type: {1}", fieldFullName, fieldType)
       End If
Next
pdf1.Save(myDir + "output_checkbox.pdf")

Please feel free to contact us for any further assistance.

Best Regards,

Thank you for the info. Unfortunately, I’m getting an error when I implement the code below. (Note that my nonSignatureToken array below provides the field name.) The error is: An unhandled exception of type ‘System.StackOverflowException’ occurred in mscorlib.dll


The code:

For Each nonSignatureToken As String In nonSignatureTokens.ToArray()
Dim fieldType = pdfForm.GetFieldType(nonSignatureToken)
If fieldType.ToString() = “CheckBox” Then
Try
TryCast(pdfDocument.Form(nonSignatureToken), CheckboxField).Style = BoxStyle.Cross
Catch ex As Exception
End Try
End If
Next
pdfDocument.Save(“C:\facadesIn\facadesResult2.pdf”)

Thanks for you thoughts on why this is happening.

Hi Regan,


Thanks for your feedback. We will appreciate it if you please share your sample document and sample working code here, so we will test it and provide you information accordingly.

We are sorry for the inconvenience.

Best Regards,

I have sent an email to you with the requested information. Thank you.

Hi Regan,


Thanks for sharing the details via email. I will test the scenario at my end and will share my findings soon.

Best Regards,

I’ve identified the issue, but not the solution: When you have multiple checkboxes with the same name, adobe automatically numbers them with a #Number afterward. (For example: FieldName#1, FieldName#2). In this case, you will get the error mentioned at the top of this thread.


I’ve attached a sample document so you can test the issue yourself. You can use the code I previously sent you and change the file name to see the problem. Please let me know if there is a solution for this.

Thanks.

Hi Regan,


Thanks for your inquiry. I have tested your above shared TestCheckBox.pdf with code shared in email and unable to notice the reported exception. Please confirm you environment details and we will appreciate it if you please share a sample console project to replicate the issue at our end, so we will provide you information accordingly.

We are sorry for the inconvenience caused.

Best Regards,