【发布时间】:2011-05-28 07:55:03
【问题描述】:
我正在使用 ITextSharp 动态填写 pdf 文档上的字段。我希望能够确定复选框的“导出值”来自代码隐藏,以便确定如果应该选中该复选框,则发送给该复选框的值。我过去使用过的大多数文档对于每个复选框都有相同的导出值,但我目前使用的文档因复选框而异。我可以遍历所有文本框并使它们保持一致,但如果我可以在运行时确定这些复选框的导出值并相应地设置它们,那么将来会节省大量时间。
提前致谢!
我尝试在 C# 中实现下面的解决方案并最终得到以下代码:
public string GetCheckBoxExportValue(AcroFields pdfDocument, string checkBoxFieldName)
{
AcroFields.Item item = pdfDocument.GetFieldItem(checkBoxFieldName);
if (item.values.Count > 0)
{
PdfDictionary valueDict = item.GetValue(0);
PdfDictionary appearanceDict = valueDict.GetAsDict(PdfName.AP);
// if there's an appearance dict at all, one key will be "Off", and the other
// will be the export value... there should only be two.
if (appearanceDict != null)
{
foreach (PdfName curKey in appearanceDict.Keys)
{
if (!PdfName.OFF.Equals(curKey))
{
return curKey.ToString(); // string will have a leading '/' character
}
}
}
// if that doesn't work, there might be an /AS key, whose value is a name with
// the export value, again with a leading '/'
PdfName curVal = valueDict.GetAsName(PdfName.AS);
if (curVal != null)
{
return curVal.ToString();
}
}
//return null if you get this far
return null;
}
这只是每次都返回“/D”。我不确定 C# 中的方法是否需要有所不同,或者我只是遗漏了一些东西。
【问题讨论】:
-
@Lorenzo - 我认为你需要停止关心其他人如何使用该网站
-
@Lorenzo,我不确定我应该在常见问题解答中阅读什么内容?
-
这是常见问题解答的摘录,用于解释如何接受答案:
[...] This lets other people know that you have received a good answer to your question. Doing this is helpful because it shows other people that you're getting value from the community. (If you don't do this, people will often politely ask you to go back and accept answers for more of your questions!)。
标签: c# asp.net pdf itextsharp