Hidden fields with pushButton widget become visible when using FlattenFormFields method to flatten fields.
Workaround: do not flatten fields if the IsHidden property of any of its Widget's is set to True:
var fieldsList = formFields.ToList();
for (int i = 0; i < fieldsList.Count; i++)
{
var field = fieldsList[i];
bool flattenCurrentField = true;
if (field.FieldType == FormFieldType.PushButton)
{
PushButtonField pushButtonField = (PushButtonField)field;
PushButtonWidget[] widgets = pushButtonField.Widgets.ToArray();
foreach (var widget in widgets)
{
var baseType = typeof(PushButtonWidget).BaseType.BaseType.BaseType;
PropertyInfo[] properties = baseType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo isHidden = properties.FirstOrDefault(p => p.Name == "IsHidden");
bool value = (bool)isHidden.GetValue(widget);
if (value == true)
{
flattenCurrentField = false;
break;
}
}
}
if (flattenCurrentField)
{
document.AcroForm.FlattenFormField(field);
}
}