0

I am wondering how to instantiate the PdfAcroForm object in iText7.

Here is the code that did previously work for me but seems to have started throwing an exception recently without change (that I can recall):

public static void ExtractValues(string filePath)
{
    //The following classes read the PDF and return the fields on the form
    PdfReader reader = new PdfReader(filePath);
    PdfDocument document = new PdfDocument(reader);
    PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, false);
    IDictionary<string, PdfFormField> fields = acroForm.GetFormFields();
} 

The exception is occurring when trying to instantiate the PdfAcroForm.

Therefore I just presumed I’d be able to instantiate it by:

PdfAcroForm pdfAcroForm = new PdfAcroForm();

However when I try this I am greeted with an error stating that PdfAcroform does not contain a constructor that takes 0 arguments. However I cant find a constructor for the object at all that takes any number of arguments.

This did previously work for me so I’m not sure why it’s not all of a sudden.

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Fildor Apr 28 '20 at 10:01
  • @Fildor I’m not passing a null reference to it though which is normally the case and would jump out to me straight away. The document variable I am using has successfully been assigned to the document I passed in via the file path variable. What’s even stranger is that whilst I am debugging and hit this exception I can step into the code and it’ll pass it like nothings happened and will work as hoped. So although this exception is occurring the actual functionality is still working as hoped – Specialized48 Apr 28 '20 at 10:40
  • Can you post the stacktrace? If this is true, that would hint to some bug in iText. I am not saying, that's impossible, yet highly unlikely. – Fildor Apr 28 '20 at 10:57
  • 1
    Does it throw if you pass `true`? Does the document actually contain an acroform ? – Fildor Apr 28 '20 at 11:04
  • 1
    @Fildor regarding your message about there potentially being a message in iText itself, I believe this may be possible as when I check the stack trace it displays the following: “ at iText.IO.Font.FontCache..cctor()” (that’s the whole trace by the looks of it). This would potentially explain why this has started happening all of a sudden even though I haven’t changed the code. – Specialized48 Apr 28 '20 at 11:24
  • @Fildor regarding passing true instead of false, still get the same issue, unfortunately. – Specialized48 Apr 28 '20 at 11:29
  • Hmmm ... I'd try to create a [mcve] and file an issue with iText. Do you have a professional support contract with iText? This seems like a legit case to use it. – Fildor Apr 28 '20 at 11:31
  • 1
    Please add the full stacktrace to see if the error happens on your side or on iText side and please also attach sample PDF to reproduce – Alexey Subach May 03 '20 at 18:32
  • _"I’m not passing a null reference to it though"_ -- whether you are or not is, frankly, unproven. Without a good [mcve], you may simply _think_ you're not. But beyond that, you still need to debug the exception. The marked duplicate tells you how to do that. If after doing that, you have a _different_ question that can be posed without having to mention the `NullReferenceException`, and which includes a good [mcve] that reliably reproduces the problem, go ahead and post that as a new question. – Peter Duniho May 03 '20 at 20:51

0 Answers0