@Niel Michael Bantilan , If you want to fill the details to the correspond fields into the pdf file, it will be better for you to create a pdf file that contains the text fields.
The following link shows that how to use itextsharp to create a TextField on an existing PDF.
Using iTextSharp to create a TextField on an existing PDF
After creating the pdf, you could try the following code to get fields and set the field's value.
Code:
private void button1_Click(object sender, EventArgs e)
{
ListFieldNames();
FillForm();
}
private void ListFieldNames()
{
string pdfTemplate = @"D:\ExampleWithFileds.pdf";
this.Text += " - " + pdfTemplate;
PdfReader pdfReader = new PdfReader(pdfTemplate);
StringBuilder sb = new StringBuilder();
foreach (var de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString() + Environment.NewLine);
}
richTextBox1.Text = sb.ToString();
}
private void FillForm()
{
string pdfTemplate = @"D:\ExampleWithFileds.pdf";
string newFile = @"D:\ExampleWithFileds111.pdf"; ;
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("Given Name Text Box", "jack");
pdfFormFields.SetField("Family Name Text Box", "father");
pdfFormFields.SetField("Address 1 Text Box", "home");
pdfFormFields.SetField("House nr Text Box", "house");
pdfStamper.FormFlattening = false;
pdfStamper.Close();
}
Note: Please install nuget-package iTextSharp
to use the above code.
Result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.