How to rename a PDF form field when the field name contains a period using iTextSharp

I am using iTextSharp to rename a PDF form field. The names of the old fields and the names of new notes are entered in the text field (separated by commas). My code then iterates over each line, captures the name of the old field, searches for the field in PDF, and renames it to a new field. Finally, a new PDF is created from the old PDF, and all fields in the new PDF will have new names.

My problem is that I cannot rename the PDF fields containing the period in the field name (e.g. First.Name). Is there a way to rename such PDF fields using iTextSharp? I searched everywhere and could not find clear answers or code examples.

Here is the code that I use 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(); } 
+1
source share
1 answer

This is described in the source code for iText (Sharp):

 /** * Renames a field. Only the last part of the name can be renamed. For example, * if the original field is "ab.cd.ef" only the "ef" part can be renamed. * @param oldName the old field name * @param newName the new field name * @return <CODE>true</CODE> if the renaming was successful, <CODE>false</CODE> * otherwise */ virtual public bool RenameField(String oldName, String newName) 

Thus, using this method, you can rename First.Name to First.NameOrEmpty or something like that, but not to EmptyOrFirst.Name .

The background is that the form fields in PDF files are not just ordered as a list of objects with a full name, but instead as a tree in which the full name of the node is a concatenation of the partial names of its ancestors and the node itself is separated by dots.

Thus, changing anything other than the part after the last point (or changing this part, but introducing a new point) actually means moving the field in the tree to another parent node, which may or may not be created in the process.

The RenameField method, on the other hand, only supports changing the local name of the field tree element itself.

And that is reasonable. Form fields can inherit a number of properties from their parent. So, if you rename a field, do you want the result field to inherit from the new parent? Or will all inherited properties be explicitly added to node? Or only those that are different from the corresponding new legacy information? Or do you have another idea?

+1
source

Source: https://habr.com/ru/post/890382/


All Articles