I use iTextSharp to rename PDF form field. The old field names and the new replacement names are entered in a text box (comma separated). My code then loops through each line, grabs the old field name, looks for the field in the PDF and renames it to the new field. Finally, a new PDF is created from the old PDF, and all of the fields in the new PDF will have the new names.
My problem is that I can't seem to rename the PDF fields which contain a period in the field name (e.g. First.Name). Is there a way to rename such PDF fields using iTextSharp? I looked everywhere and couldn't find a clear answers or code examples.
Here is the code I'm using so far:
//source PDF file
string src = PDFFile.Text;
//destination PDF file
string dest = @"Renamed_" + DateTime.Now.ToString("yyyymmddhhss") + ".pdf";
//open source PDF
PdfReader reader = new PdfReader(src);
using (FileStream fs = new FileStream(dest, FileMode.Create))
{
PdfStamper stamper = new PdfStamper(reader, fs);
AcroFields fields = stamper.AcroFields;
//read each line from the txtOldAndNewFieldNames text box
//each line contains <old field name>,<new field name>
//the two values are then put in an array
//finally, the PDF field with <old field name> is renamed to <new field name>
foreach (string line in txtOldAndNewFieldNames.Lines)
{
string[] namePair = new string[2];
namePair = line.Split(',');
try
{
fields.RenameField(namePair[0], namePair[1]);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
stamper.Close();
reader.Close();
}