rename fieldname - pdf

Posted on 2013-09-13
Medium Priority
Last Modified: 2013-10-04
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  <<<<<<<<<<<

Question by:VBdotnet2005
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 39492659

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

Author Comment

ID: 39496624
I got this error  on Next - "Collection was modified after the enumerator was instantiated."
LVL 96

Accepted Solution

Bob Learned earned 2000 total points
ID: 39498101
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question