无法读取C#中的PDF控件

时间:2018-11-21 06:59:15

标签: c# pdf itext

正在尝试读取PDf和PDF控件中的内容。我的pdf是由Adobe pdf库生成的。得到空的Acro字段。但我的表单有4个复选框。 4复选框,我可以用来选中或取消选中。我想要复选框是否被选中。 我使用itextsharp来阅读pdf,但是找不到控件。

private static string GetFormFieldNamesWithValues(PdfReader pdfReader)
    {

        return string.Join("\r\n", pdfReader.AcroFields.Fields
       .Select(x => x.Key + "=" +
                pdfReader.AcroFields.GetField(x.Key) + "=" + pdfReader.AcroFields.GetFieldType(x.Key)).ToArray());

    }


    static void Main(string[] args)
    {
        DataTable filedDetails;
        DataRow dr;
        string cName="";
        string cType = "";
        string cValue = "";
        int txtCount = 0;
        int btnCount = 0;
        int chkBoxCount = 0;
        int rdButtonCount = 0;
        int dropDownCount = 0;

        var fileName = "C:\\PreScreenings\\ViewPDF Cien.pdf";// PDFFileName.Get(context);
        //var fileName = @"C:\Users\465sanv\Downloads\Read-PDF-Controls-master\ReadPDFControl\Input\David1990.pdf";
        var fields = GetFormFieldNamesWithValues(new PdfReader(fileName));
        string[] splitRows = fields.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
        filedDetails = new DataTable("PDF Table");
        filedDetails.Columns.AddRange(new[] { new DataColumn("Control Name"), new DataColumn("Control Type"), new DataColumn("Control Value") });

        foreach (string row in splitRows)
        {

            dr = filedDetails.NewRow();
            string[] str = row.Split("=".ToCharArray(), StringSplitOptions.None);

            cName = str[0].ToString();
            cValue = str[1].ToString();
            if (str[2].ToString() == "1")
            {
                btnCount++;
                cType = "Button" + btnCount.ToString();
            }
            else if (str[2].ToString() == "2")
            {
                chkBoxCount++;
                cType = "Check Box" + chkBoxCount.ToString();

            }
            else if (str[2].ToString() == "3")
            {
                rdButtonCount++;
                cType = "Radio Button" + rdButtonCount.ToString();
            }
            else if (str[2].ToString() == "4")
            {
                txtCount++;
                cType = "Text Field" + txtCount.ToString();
            }
            else if (str[2].ToString() == "6")
            {
                dropDownCount++;
                cType = "Drop Down" + dropDownCount.ToString();
            }
            dr[0] = cName;
            dr[1] = cType;
            dr[2] = cValue;

            filedDetails.Rows.Add(dr);

        }

    }

0 个答案:

没有答案