如何使用itextsharp从PDF读取复选框值 [英] How to read checkbox value from PDF using itextsharp

查看:318
本文介绍了如何使用itextsharp从PDF读取复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用itextsharp从PDF文件中读取数据。我从复选框中读取数据时遇到问题。在这里我不能使用任何OCR。要再次使用OCR,我需要将PDF转换为图像然后可以由OCR处理。



除了OCR之外,只需使用itextsharp。是否有可能根据坐标从复选框中读取数据。



我能够使用复数值以外的itextsharp读取PDF中的所有内容。



请帮我解决这个问题。

Hi,

I am using itextsharp to read data from PDF file. I am having problem to read data from checkbox. Here i can't use any OCR. To use OCR again i need to convert PDF to Image and then can be processed by OCR.

Other than OCR, just by using itextsharp. Is there any possibility to read data from checkbox based on coordinates.

I am able to read all content from PDF using itextsharp other than checkbox value.

Please help me on this.

推荐答案

你可以在这里试试这段代码 - 我记得那不是那么直截了当 - 你实际上必须读取该字段的值。如果它没有字段名称 - 您需要获取复选框的引用并读取值 - 它不会自动转换为bool。

You might try this code here - I remembered that it was not so straight forward - you actually had to read a value of the field. If it does not have a field name - you will need to get the reference to the checkbox and read values - it does not automagically translate to bool.
public String getCheckboxValue(String src, String name) 
{
   PdfReader reader = new PdfReader(SRC);
   AcroFields fields = reader.getAcroFields();
   // CP_1 is the name of a check box field
   String[] values = fields.getAppearanceStates("CP_1");
   StringBuilder sb = new StringBuilder();
   for (String value : values) 
   {
       sb.append(value);
       sb.append('\n');
   }
   return sb.ToString();
}



或者你可以使用这个[从这里:

获取复选框的导出值[ ^ ]


Or you can use this [From Here:
Get the Export Value of a Checkbox[^]

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;
}


这篇关于如何使用itextsharp从PDF读取复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆