C#基于Entries类迭代复选框填充PDF表单的问题
Hey there! As someone who’s worked with iTextSharp and Windows Forms controls, let’s work through your two main issues step by step.
You mentioned trying to use reflection to generalize the code but hitting identifier errors. The key here is making sure you’re correctly mapping each CheckBox’s Name property to the matching boolean property in your Entries class. Here’s a working foreach implementation using reflection:
// First, create an instance of your Entries class Entries formEntries = new Entries(); // Loop through all CheckBox controls on your form foreach (var checkbox in this.Controls.OfType<CheckBox>()) { // Use reflection to find the matching property in Entries var targetProperty = typeof(Entries).GetProperty(checkbox.Name); // Only proceed if we found a valid boolean property if (targetProperty != null && targetProperty.PropertyType == typeof(bool)) { targetProperty.SetValue(formEntries, checkbox.Checked); } }
Common pitfalls that cause identifier errors:
- Mismatched names: Ensure your CheckBox’s
Nameproperty (e.g., "CheckBox1") exactly matches the property name inEntries(case-sensitive!). - Missing properties: Double-check that every CheckBox on your form has a corresponding boolean property in
Entries. - Non-boolean properties: The reflection check ensures we only target boolean properties, avoiding errors from other property types.
Your text/dropdown fields work, but checkboxes don’t—this almost always boils down to PDF checkbox export values. Unlike your original code that used "Yes"/"No", iTextSharp requires you to use the exact export value defined in the PDF template.
Step 1: Find your PDF checkbox’s export value
Open your template PDF in Adobe Acrobat:
- Go to Prepare Form mode.
- Right-click a checkbox field and select Properties.
- Go to the Options tab—look for the "Export Value" field (default is usually "On").
Step 2: Update your code to use the correct export value
Replace your current checkbox-setting code with this (adjusting the export value if yours isn’t "On"):
// Assuming you have your Entries instance populated with checkbox states var checkboxProperties = typeof(Entries).GetProperties() .Where(p => p.PropertyType == typeof(bool) && p.Name.StartsWith("CheckBox")); foreach (var prop in checkboxProperties) { bool isChecked = (bool)prop.GetValue(formEntries); string pdfFieldName = prop.Name; // Match this to your PDF's checkbox field name if (isChecked) { // Use your PDF's actual export value here (e.g., "On" or "Yes") pdfFormFields.SetField(pdfFieldName, "On"); } else { // Clear the field to uncheck it pdfFormFields.SetField(pdfFieldName, string.Empty); } }
If you’re using the Entries.CheckBoxes dictionary approach, modify it like this:
foreach (var box in Entries.CheckBoxes) { pdfFormFields.SetField(box.Key, box.Value ? "On" : string.Empty); }
Final checks to ensure it works:
- Field name matching: Confirm
box.Key(orprop.Name) exactly matches the checkbox field name in the PDF (case-sensitive!). - Proper cleanup: Make sure you close the
PdfStamperandPdfReadercorrectly after setting fields:pdfStamper.Close(); pdfReader.Close(); - Avoid flattening prematurely: If you set
pdfStamper.FormFlattening = true;before setting fields, it can break interactive form elements like checkboxes.
内容的提问来源于stack exchange,提问作者Rhys Clarke

