0

I am trying to Load a Form PDF using iTextsharp. When i tried to set the value for a field element in that PDF using Acrofield, am getting "Invalid Cast Exception". "Error Converting IDictionary Object type to Array".

We are using itextSharp 5.* version

Error Stack Trace :

Unable to cast object of type 'iTextSharp.text.pdf.PdfDictionary' to type 'iTextSharp.text.pdf.PdfArray'.

   at iTextSharp.text.pdf.AcroFields.Fill()
   at iTextSharp.text.pdf.AcroFields..ctor(PdfReader reader, PdfWriter writer)
   at iTextSharp.text.pdf.PdfStamperImp.GetAcroFields()
   at iTextSharp.text.pdf.PdfStamper.get_AcroFields()
   at generate_PDF.WebForm1.generate_PDF() in c:\Users\Documents\Visual Studio 2013\Projects\generate_PDF\generate_PDF\PDF.aspx.cs:line 159
  at generate_PDF.WebForm1.BtnPrint_Click(Object sender, EventArgs e) in c:\Users\465816\Documents\Visual Studio 2013\Projects\generate_PDF\generate_PDF\PDF.aspx.cs:line 32
  at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
  at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
  at System.   

Please find the form in below Link as well.

https://drive.google.com/file/d/0B3W8aJry8ZMERnJubHpMdVk5SmM/view?usp=sharing

But the same code is working for other PDF form. Please Let me know the reason the reason for getting this Error, is this becasue of the PDF ? or any other issue.

Manikandan
  • 21
  • 1
  • 4

1 Answers1

1

The cause for that exception is that the AcroForm PDF form description in your sample PDF is invalid.


In the AcroForm interactive form dictionary of your PDF (object 200) the value of the Fields key is a dictionary object (object 201):

201 0 obj
<<
/T(ŸJ˜OPZ?h&7·Œ]VŸÄþ)
/Kids[ 190 0 R 191 0 R 192 0 R]
>>
endobj
200 0 obj
<<
/DR 194 0 R
/XFA[(uÕêÒãÊ\0224) 195 0 R(fÈáÕçÏ) 196 0 R(qÂâÃâÉ\0124) 197 0 R(aÆûÒýÍ\012") 198 0 R(uÈüÇïÅ\034=O) 199 0 R]
/Fields 201 0 R
/NeedAppearances true
>>
endobj 

But the specification ISO 32000-1 clearly specifies

Fields array (Required) An array of references to the document’s root fields (those with no ancestors in the field hierarchy).

(Table 218 – Entries in the interactive form dictionary )

So that value must be an array.

Please ask the source of the file to supply a valid PDF.

Community
  • 1
  • 1
mkl
  • 90,588
  • 15
  • 125
  • 265