Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

I am stuck here. actually I am trying to fill a PDF form using asp.net. I get some help and write the following code:

C#
private void fillForm()
    {
        try
        {
            string formFile = Server.MapPath("") + @"\Forms\fw4.pdf";
            string savepath = Server.MapPath("") + @"\Forms\new_fw4.pdf";
            PdfReader pdfReader = new PdfReader(formFile);
            using (FileStream stream = new FileStream(savepath, FileMode.Create))
            {
                PdfStamper pdfStamper = new PdfStamper(pdfReader, stream);
                AcroFields formFields = pdfStamper.AcroFields;

                foreach (DictionaryEntry de in formFields.Fields)
                 {
                     formFields.SetField("field name", "field value");
                 }            
                pdfStamper.FormFlattening = true;
                pdfStamper.Close();
            }
        }
        catch
        {
        }
    }


I want the program to show all fields in a List.
I am unable to iterate all available fields using the foreach loop. Its giving me this error:
Cannot convert type 'System.Collections.Generic.KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item>' to 'System.Collections.DictionaryEntry'


any help would be greatly appreciated.
Posted

1 solution

The error implies that the AcroFields class does not implement IDictionary - it is a generic List of KeyValuePairs instead.

Replace DictionaryEntry with KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item> or KeyValuePair<string, object> or even a var and you should be fine.

You may have a problem with the content of the foreach loop though - you cannot modify an IEnumerable from within a foreach loop on it!
 
Share this answer
 
Comments
tanweer 6-Apr-12 7:37am    
thanks for the post, can I listdown all fields when pdfReader reads the file in an array or some other way here??
tanweer 6-Apr-12 8:30am    
I got the names by using item.Key & item.Value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900