Get the export value of a checkbox using iTextSharp

后端 未结 4 741
南笙
南笙 2020-12-10 15:57

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 cod

4条回答
  •  难免孤独
    2020-12-10 16:23

    This was the final method I used to get it working based on the others:

        public string GetCheckBoxExportValue(AcroFields fields, string cbFieldName)
        {
            AcroFields.Item item = fields.GetFieldItem(cbFieldName);
            if (item.values.Count > 0)
            {
                PdfDictionary valueDict = item.values[0] as PdfDictionary;
                PdfDictionary appDict = valueDict.GetAsDict(PdfName.AP);
    
                if (appDict != null)
                {
                    PdfDictionary normalApp = appDict.GetAsDict(PdfName.N);
    
                    foreach (PdfName curKey in normalApp.Keys)
                    {
                        if (!PdfName.OFF.Equals(curKey))
                        {
                            // string will have a leading '/' character
                            return curKey.ToString(); 
                        }
                    }
                }
    
                PdfName curVal = valueDict.GetAsName(PdfName.AS);
                if (curVal != null)
                {
                    return curVal.ToString();
                }
    
            }
    
            return null;
        }
    

提交回复
热议问题