使用PDFStamper与AcroFields勾选复选框时单PDF报数组索引越界
我搭建了ASP页面,用于收集用户输入信息并填充到自定义PDF模板中。其中一个PDF模板运行正常,但另一个模板抛出如下错误:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Application.Page.Function(Object sender, EventArgs e) in {Application Folder}
两个PDF的处理代码逻辑一致,仅模板不同,我检查了CS文件、ASPX文件及PDF文件属性,未发现差异。相关代码如下:
正常运行的模板代码
ASP页面
<asp:CheckBox runat="server" ID="Checkbox1" /> <asp:CheckBox runat="server" ID="Checkbox2" />
CS页面
public void submitPage1(object sender, EventArgs e) { string template = MapPath("Template File Path"); string destination = MapPath("Complete File Destination"); DataTable input = new DataTable(); input.Columns.Add("ActiveCheck", typeof(bool)); input.Columns.Add("InactiveCheck", typeof(bool)); DataRow newRow = input.Row(); newRow["ActiveCheck"] = Checkbox1.Checked; newRow["InactiveCheck"] = Checkbox2.Checked; FileStream PDFStream = new FileStream(destination, FileMode.Append); PdfReader templateReader = new PdfReader(template); PdfStamper newFile = new PdfStamper(templateReader, PDFStream); AcroFields formFields = newFile.AcroFields; bool active = (bool)newRow["ActiveCheck"]; bool inactive= (bool)newRow["InactiveCheck"]; if (active) { String[] firstField = formFields.GetAppearanceStates("ActiveCheckBox"); formFields.SetField("ActiveCheckBox", firstField[1]); } if (inactive) { String[] secondField = formFields.GetAppearanceStates("InactiveCheckBox"); formFields.SetField("InactiveCheckBox", secondField[1]); }
报错的模板代码
ASP页面
<asp:CheckBox runat="server" ID="Complete" />
CS页面
public void submitPage1(object sender, EventArgs e) { string template = MapPath("Template File Path"); string destination = MapPath("Complete File Destination"); DataTable input = new DataTable(); input.Columns.Add("CompleteCheck", typeof(bool)); DataRow newRow = input.Row(); newRow["CompleteCheck"] = Complete.Checked; FileStream PDFStream = new FileStream(destination, FileMode.Append); PdfReader templateReader = new PdfReader(template); PdfStamper newFile = new PdfStamper(templateReader, PDFStream); AcroFields formFields = newFile.AcroFields; bool finished = (bool)newRow["CompleteCheck"]; if (finished) { String[] completeField= formFields.GetAppearanceStates("CompleteCheckBox"); formFields.SetField("CompleteCheckBox", completeField[1]); }
编辑1:修改if语句内代码后不再报错,但PDF复选框无法勾选,修改后代码:
if (finished) { formFields.SetField("CompleteCheckBox", "Yes"); }
1. 数组越界错误原因
IndexOutOfRangeException 出现在 completeField[1],说明调用 formFields.GetAppearanceStates("CompleteCheckBox") 返回的数组长度不足2。
核心差异在PDF模板的复选框配置:
- 正常模板的复选框定义了至少2种外观状态(比如"Off"和"Yes"),所以数组索引1有效;
- 报错模板的复选框可能只有1种状态,或状态索引逻辑不同,导致数组长度为1,访问索引1触发越界。
2. 复选框无法勾选的解决方法
直接设置"Yes"无效,是因为该复选框的勾选状态对应的外观状态名称不是"Yes"。不同PDF编辑器创建的复选框,状态名称差异较大,常见的有"On"/"Off"、"1"/"0"或自定义名称。
正确处理步骤:
(1)确认复选框的实际状态名称
先打印该复选框的所有外观状态,明确正确的勾选值:
if (finished) { String[] states = formFields.GetAppearanceStates("CompleteCheckBox"); // 将状态输出到日志或页面,查看所有可用值 foreach(string state in states){ Console.WriteLine(state); } // 根据实际输出设置勾选状态 if(states.Length > 1){ formFields.SetField("CompleteCheckBox", states[1]); } else if(states.Length == 1){ formFields.SetField("CompleteCheckBox", states[0]); } }
(2)检查PDF模板配置
用Adobe Acrobat等编辑器打开报错的PDF模板,查看CompleteCheckBox的属性:
- 确认复选框是可编辑的表单字段,而非静态图形;
- 查看其"勾选状态"对应的外观值,确保代码中使用的名称与模板一致。
(3)简化代码逻辑(版本支持的话)
如果使用的iTextSharp版本支持,可直接通过布尔值映射设置,无需手动处理状态数组:
// 根据模板实际状态名称调整,比如用"On"/"Off"或"1"/"0" formFields.SetField("CompleteCheckBox", finished ? "On" : "Off");
内容的提问来源于stack exchange,提问作者SALXAPHONE

