rename fieldname - pdf

I am using itextsharp to find field name in pdf and it works fine. Is there a way to rename field name? Do you know?

Dim reader As New PdfReader("YourPdf.pdf")
    Dim fields As IDictionary(Of String, iTextSharp.text.pdf.AcroFields.Item) = reader.AcroFields.Fields
    For Each key As String In fields.Keys
        Dim fieldItem = reader.AcroFields.GetFieldItem(key)
        Dim pdfDictionary As PdfDictionary = fieldItem.GetWidget(0)

        Dim pdfFieldName As String = key  <<<<<<<<<<<
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:

iTextSharp — few C# examples.


private byte[] a1(string _ToAppend)
    using (var existingFileStream = new FileStream(Server.MapPath(P_InputStream), FileMode.Open))
    using (MemoryStream stream = new MemoryStream())
        // Open existing PDF
        var pdfReader = new PdfReader(existingFileStream);
        var stamper = new PdfStamper(pdfReader, stream);
        var form = stamper.AcroFields;
        var fieldKeys = form.Fields.Keys;

        foreach (string fieldKey in fieldKeys)
            form.RenameField(fieldKey, fieldKey + _ToAppend);
        // "Flatten" the form so it wont be editable/usable anymore
        stamper.FormFlattening = true;
        return stream.ToArray();


Private Function a1(_ToAppend As String) As Byte()
      Using existingFileStream = New FileStream(Server.MapPath(P_InputStream), FileMode.Open)
            Using stream As New MemoryStream()
                  ' Open existing PDF
                  Dim pdfReader = New PdfReader(existingFileStream)
                  Dim stamper = New PdfStamper(pdfReader, stream)
                  Dim form = stamper.AcroFields
                  Dim fieldKeys = form.Fields.Keys

                  For Each fieldKey As String In fieldKeys
                        form.RenameField(fieldKey, fieldKey & _ToAppend)
                  ' "Flatten" the form so it wont be editable/usable anymore
                  stamper.FormFlattening = True
                  Return stream.ToArray()
            End Using
      End Using
End Function
VBdotnet2005Author Commented:
I got this error  on Next - "Collection was modified after the enumerator was instantiated."
Bob LearnedCommented:
Try using a For...Next:

For index As Integer = 0 to fieldKeys.Count - 1
    Dim fieldKey As String = fieldKeys(index)
    form.RenameField(fieldKey, fieldKey & _ToAppend)
Next index

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.