c#, itextsharp, aspx

I have 100 pdf files with a field name called 'firstname'
and I want to load all of them and using itextsharp to change the fieldname to 'myfirstname'

how can I do that in itextsharp with c#?

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.

ITsolutionWizardAuthor Commented:
Any helps?
Shaun VermaakTechnical SpecialistCommented:
Please post sample PDF
ITsolutionWizardAuthor Commented:
It is just blank PDF with fields any sample will be fine
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Shaun VermaakTechnical SpecialistCommented:
Is the PDF scanned or generated?
ITsolutionWizardAuthor Commented:
It is PDF with textbox fields. It is not scanned doc
ITsolutionWizardAuthor Commented:
Any helps
Shaun VermaakTechnical SpecialistCommented:
Going offline, please have a look at this from http://stackoverflow.com/questions/9635982/altering-pdf-through-itextsharp-to-replace-some-existing-text-with-a-new-text
    String formFile = Server.MapPath("~/") + "source.pdf";
    String newFile = Server.MapPath("~/") + "sink.pdf";
    PdfReader reader = new PdfReader(formFile);
    PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create));
    AcroFields fields = stamper.AcroFields;
    // set form fields
    fields.SetField("{TO}", "John Doe");
    fields.SetField("{FROM}", "2 Milky Way, London");
    stamper.FormFlattening = true;

Open in new window

Daniel Van Der WerkenIndependent ConsultantCommented:
The methodology is to use the RenameField() method. I am doing this in the following method, but what my method does is rename *all* the fields in a page from "name" to "1_name" for a page usage I have.

You will want to modify my code. Note that you can't modify the field while it's in use, so that's why I had to rename after the fact as I do in this method:

       /// <summary>
        ///     Renames the AcroFields of the selected page of a PDF file and returns the
        ///     renamed fields as a byte array.
        /// </summary>
        /// <param name="pdfFile">Any PDF file that has valid AcroFields in it.</param>
        /// <param name="pageNumber">The page from that PDF file to rename the fields of</param>
        /// <param name="instanceNumber">The renamed instance number to use.</param>
        /// <returns>byte[] representing the page with renamed fields. Use in PdfReader(byte[]) constructor.</returns>
        public static byte[] RenameFieldsIn(string pdfFile, int pageNumber, int instanceNumber)
            byte[] returnByteArray = new byte[] { };
            const string RENAME_FIELD = "{0}_{1}";
            Dictionary<string, string> renamedDictionary = new Dictionary<string, string>();
                using (MemoryStream ms = new MemoryStream())
                    using (PdfReader reader = new PdfReader(pdfFile))
                        reader.SelectPages(new List<int>() { pageNumber });
                        using (PdfStamper stamper = new PdfStamper(reader, ms))
                            AcroFields fields = stamper.AcroFields;
                            if (fields != null && fields.Fields != null && fields.Fields.Count > 0)
                                foreach (string key in fields.Fields.Keys)
                                    // Need to save these here because you can't modify a collection that's being enumerated in-situ
                                    renamedDictionary.Add(key, string.Format(RENAME_FIELD, key, instanceNumber));

                            // Modify the collection now
                            foreach (string key in renamedDictionary.Keys)
                                fields.RenameField(key, renamedDictionary[key]);
                    returnByteArray = ms.ToArray();
            catch (Exception ex)
            return returnByteArray;

Open in new window

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
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Daniel Van Der Werken (https:#a42081718)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer
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.