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..