asp.net 索引在数组的边界之外,使用PDFStamper和AcroFields检查CheckBox,但它只适用于两个PDF中的一个

x759pob2  于 5个月前  发布在  .NET
关注(0)|答案(1)|浏览(66)

我不知道为什么这个问题只发生在一个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中将复选框设置为On。以下是我更改的内容:
if (finished) {
formFields.SetField("CompleteCheckBox", "Yes");
}

sg2wtvxw

sg2wtvxw1#

由于我面临的问题,我最终将复选框更改为“是”和“否”的下拉列表。我仍然很困惑,为什么三个复选框中只有一个给我错误,因为它们都是相同的(从我可以告诉在PDF和C#代码)。我将离开这个开放,如果有人能找到一个原因,为什么它的工作;然而,我现在有一个替代的解决方案。我知道这是一个问题,它只是阅读,最后一个复选框没有第二个元素,但为什么它工作的其他两个复选框。

相关问题