You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

1. Fixing the "identifier error" when syncing CheckBox states to your Entries class

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 Name property (e.g., "CheckBox1") exactly matches the property name in Entries (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.
2. Fixing iTextSharp’s invalid PDF checkbox state updates

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:

  1. Go to Prepare Form mode.
  2. Right-click a checkbox field and select Properties.
  3. 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 (or prop.Name) exactly matches the checkbox field name in the PDF (case-sensitive!).
  • Proper cleanup: Make sure you close the PdfStamper and PdfReader correctly 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:52:04