0

I want to list the names of all my PDF fields with iTextSharp. That's what i got so far :

protected void btnPDF_click(object sender, EventArgs e)
{
    MemoryStream ms = new MemoryStream();

    PdfReader lecteur = new PdfReader(Server.MapPath("~/Img/f16.pdf"));
    PdfStamper etampeur = new PdfStamper(lecteur, ms);

    AcroFields af = lecteur.AcroFields;


    foreach (KeyValuePair<string, AcroFields.Item> fil in af.Fields)
    {
        lblErreur.Text += fil.Key.ToString() + "<br />";
    }


    lecteur.Close();
    etampeur.Close();
}

found everything on :

How do I enumerate all the fields in a PDF file in ITextSharp

But strangely, it does not work, that's the error i get :

InvalidCastException :

The specified cast is not valid.

I tried every workaround i knew... but i ran out of ideas...

Community
  • 1
  • 1
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62

1 Answers1

1

Ok, i felt on this :

https://web.archive.org/web/20211020001747/https://www.4guysfromrolla.com/articles/030211-1.aspx

and seems like this works :

using System.Collections;

foreach (DictionaryEntry fil  in af.Fields)
{
    lblErreur.Text += fil.Key.ToString() + "<br />";
}

Sorry... i found that rigth after posting my question...

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62