Frequently Asked Question:
How does the FormFieldCount function work?
How does the FormFieldCount function work?
The FormFieldCount function always returns the total number form fields in the document at the time the function is called. When FlattenFormField succeeds (returns 1) the field will be deleted from the PDF. This means that the field index of subsequent form fields will be reduced by 1. So the correct way to flatten the form fields is to keep a variable which is the current form field number and only increase it if the flattening fails.
Something like this:
Dim FieldIndex As Integer
Dim FieldCount As Integer
Dim LoopNumber As Integer
FieldCount = QP.FormFieldCount()
FieldIndex = 1
For LoopNumber = 1 to FieldCount
If QP.FlattenFormField(FieldIndex) = 0 Then
FieldIndex = FieldIndex + 1
End If
Next
Or alternatively, another way to do it is like this:
Dim TotalFormFields As Integer
QP.LoadFromFile(FileName);
TotalFormFields = PDFLibrary.FormFieldCount()
While TotalFormFields > 0
QP.FlattenFormField(TotalFormFields);
TotalFormfields = TotalFormFields - 1;
End While
QP.SaveToFile(FileName);