I have a PDF document with 3 Fields txt_FirstName, txt_MiddleName and txt_LastName that I write into using iTextSharp.
I have a loop that creates the output file, writes to it, and closes the file.
The first time in the loop the file writes the first name and the middle name.
The second time in the loop the file should have the first name, middle name, and write the last name.
Issue: The problem is, when it goes to the loop the 2nd time around and writes the lastname the first name, and middle names disappear.
Goal: The main thing I want to do is write to the same PDF documents multiple times
Download PDF template: https://www.scribd.com/document/412586469/Testing-Doc
public static string templatePath = "C:\\temp\\template.pdf";
public static string OutputPath = "C:\\Output\\";
private static void Fill_PDF()
{
string outputFile = "output.pdf";
int counter = 1;
for (int i = 0; i < 2; i++)
{
PdfStamper pdfStamper;
PdfReader reader;
reader = new PdfReader(File.ReadAllBytes(templatePath));
PdfReader.unethicalreading = true;
if (File.Exists(OutputPath + outputFile))
{
pdfStamper = new PdfStamper(reader, new FileStream(OutputPath + outputFile,
FileMode.Append, FileAccess.Write));
}
else
{
pdfStamper = new PdfStamper(reader, new FileStream(OutputPath + outputFile,
FileMode.Create));
}
AcroFields pdfFormFields = pdfStamper.AcroFields;
if (counter == 1)
{
pdfFormFields.SetField("txt_FirstName", "Scooby");
pdfFormFields.SetField("txt_MiddleName", "Dooby");
counter++;
}
else if (counter == 2)
{
pdfFormFields.SetField("txt_LastName", "Doo");
}
pdfStamper.Close();
}
}