0

I have one editable pdf form. I have created a table with same columns as in the PDF. My requirement is to bind those column data(Single row) to the existed PDF's text boxes.

Here with my piece of code, I am able to get the id's of PDF form fields and I am binding those fields with data.

{
MemoryStream ms = new MemoryStream();
PdfReader reader = new PdfReader(Request.MapPath("~/test.pdf"));
PdfStamper formFiller = new PdfStamper(reader, ms);
AcroFields formFields = formFiller.AcroFields;

        ArrayList aa = new ArrayList();



        foreach (DictionaryEntry de in reader.AcroFields.Fields)
        {


            aa.Add(de.Key.ToString());

        }



        SqlCommand cmd = new SqlCommand("select Gname ,addr1 ,addr2,hno,gender ,postcode ,
        Fname,color,driving ,country ,city,height from example where Gname='pramod'", conn);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        conn.Open();
        int counter = 0;
        int colcount = ds.Tables[0].Columns.Count;
        if (!object.Equals(ds.Tables[0], null))
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int j = 0; j < colcount; j++)
                {

                        foreach (var item in aa)
                        {
                            if (item.ToString() == ds.Tables[0].Columns[j].ToString())                                
                            {
                                formFields.SetField(item.ToString(), ds.Tables[0].Rows[0][j].ToString());
                                var it = item.ToString();
                                counter++;
                                if (counter > 0)
                                {
                                    aa.Remove(it);
                                    break;
                                }

                            }
                }
            }
        }

    formFiller.Close();
    reader.Close();


    Response.ContentType = "application/pdf";
    Response.WriteFile("Default4.aspx");
    Response.BinaryWrite(ms.ToArray());

But the problem is I am getting those ids randomly i.e as not in a same order present in the pdf. So I can't go forward with index based loop binding, as the ids are not coming in sequence. And also I can't figure out which Id goes with which text/check box?

For ex in the below image the first came AADHAR NUMBER id is from the 10th page of the pdf. And also, you can see some irregular names like checkbox1,2 for which I can't figure out which text box id it is..

enter image description here

Shakeer Mirza
  • 5,054
  • 2
  • 18
  • 41

1 Answers1

2

You should never rely on the order of the fields in the form. That is bad practice, because anyone can at any time change the order of the fields in the template, and then you will have to change your code because of this changed order. That's asking for problems.

You should give the fields in the PDF the same name as the fields in your database table (based on what you write, I'm assuming that you already do that). If the names are identical, then you can do this:

ds.Tables[0].Rows[0][item.ToString()].ToString()

Note that your loop is awkward. You are wasting CPU! Why don't you just do this:

if (ds.Tables[0].Rows.Count > 0)
{
    foreach (var item in aa)
    {
        var it = item.ToString();
        formFields.SetField(it, ds.Tables[0].Rows[0][it].ToString());
    }
}

DISCLAIMER: I'm not a C# developer, I just Googled until I found the answer. I didn't test the answer, but I hope you get the idea.

I had a hard time reading and understanding your code because you're making it too complex. I guess that's why no one is answering your question. There is a saying "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." I suggest that you do that in the future ;-)

You also say that you see some irregular names like checkbox1,2 for which I can't figure out which text box id it is. We can't help you on that question if you don't share the PDF, but given the names checkbox1, checkbox2,... I would assume that those are not text boxes, but that they are check boxes. Please don't confuse text boxes (can have any string value) with check boxes (can have one out of two possible values, one of which is off).

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165