我联系了Websupergoo团队,他们非常有帮助。他们解释说,没有办法通过某种索引来选择单选按钮,而且每个选项引用的字符串必须与表单选项中的字符串匹配。
为此,如果不与选项对象的字符串交互,就无法选择单选按钮,而且由于字符串格式不正确,因此正常的选项选择代码不起作用。
为了解决这个问题,如果有人发现自己在我的鞋子里,websupergo团队提供了以下功能:
/// <summary>
/// Checks each field to ensure it has properly formatted options
/// </summary>
/// <param name="field"></param>
/// <returns>An array of options that have been safely formatted</returns>
private static string[] VetField(Field field)
{
List<string> options = new List<string>();
foreach (Field kid in field.Kids)
{
bool different = false;
DictAtom ap1 = kid.Resolve(Atom.GetItem(kid.Atom, "AP")) as DictAtom;
if (ap1 == null) continue;
DictAtom ap2 = new DictAtom();
foreach (var pair1 in ap1)
{
DictAtom apType1 = kid.Resolve(pair1.Value) as DictAtom;
Debug.Assert(apType1 != null); // should never happen
DictAtom apType2 = new DictAtom();
ap2[pair1.Key] = apType2;
foreach (var pair2 in apType1)
{
string name1 = pair2.Key;
StringBuilder sb = new StringBuilder();
foreach (char c in name1)
{
if (c < 128)
sb.Append(c);
}
string name2 = sb.ToString();
if (name1 != name2)
different = true;
apType2[name2] = pair2.Value;
if (pair1.Key == "N")
options.Add(name2);
}
}
if (different)
((DictAtom)kid.Atom)["AP"] = ap2;
}
return options.ToArray();
}
它检查选项字段的字符串是否存在格式问题,并返回经过清理的选项列表。因此,以问题中的代码为例,我可以通过执行以下操作正确选择选项单选按钮:
Doc theDoc = new Doc();
theDoc.Read(Server.MapPath("fileName.pdf"));
Field areYouHappy = theDoc.Form["Q28_happy"];
string[] options = VetField(areYouHappy); //uses above function to check for formatting errors
areYouHappy.Value = options[0];
theDoc.Save(Server.MapPath("newFileName.pdf"));
这个方法很有效,我希望将来能帮助别人!