我正在使用 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# 中的方法是否需要不同,或者我是否只是遗漏了一些东西。
最佳答案
好的,您需要检查低级 PDF 对象的适当值。您可以在 PDF Reference 中查找所述值(第 12 章:交互功能,第 7 节:交互表单)。
特别是(在 Java 中):
AcroFields.Item item = acroFields.getFieldItem(fldName);
PdfDictionary valueDict = item.getValue(0);
PdfDictionary appearanceDict = valueDict .getAsDict(PdfName.AP);
if (appearanceDict != null) {
PdfDictionary normalAppearances = appearanceDict.getAsDict(PdfName.N);
// /D is for the "down" appearances.
// if there are normal appearances, one key will be "Off", and the other
// will be the export value... there should only be two.
if (normalAppearances != null) {
Set<PdfName> keys = normalAppearances .getKeys();
for (PdfName curKey : 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();
}
类似的东西。通常的“我刚刚在这里的编辑框中写了这个”的规定适用,但这应该很好。我编写了大量令人苦恼的低级 iText 代码。
关于c# - 使用 iTextSharp 获取复选框的导出值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4491156/