c#, itextsharp, aspx

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
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#?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Any helps?
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Please post sample PDF

Author

Commented:
It is just blank PDF with fields any sample will be fine
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Is the PDF scanned or generated?

Author

Commented:
It is PDF with textbox fields. It is not scanned doc

Author

Commented:
Any helps
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
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;
    stamper.Close();

Open in new window

Independent Consultant
Commented:
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>();
            try
            {
                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)
            {
                Console.WriteLine(ex.Message);
            }
            return returnByteArray;
        }

Open in new window

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.

frankhelk
Experts-Exchange Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial