I'm working on dynamically filling in the fields on a pdf document using ITextSharp. I'd like to be able to determine the "export value" of the checkbox is from the codebehind in order to determine what value to send to this checkbox if it should be checked. Most of the documents I've worked with in the past had the same export value for every check box but the one I'm currently working with varies from checkbox to checkbox. I could go through all of the text boxes and make them consistent but it would save a lot of time in the future if I could just determine what the export value of these checkboxes are at runtime and set them accordingly.
Thanks in advance!
I tried to implement the solution below in C# and ended up with the following code:
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;
}
This just returns "/D" every single time. I'm not sure if the approach needs to be different in C# or if I'm just missing something.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…