joshman1019
joshman1019

Reputation: 159

Null Reference Exception when calling iText7 PdfAcroForm.GetAcroForm() in .Net Core 3.1 class library

I am working on converting an application to .Net Core 3.1, and in my class library I am generating a PDF form from an existing template, and filling that form with data. In ITextSharp, the predecessor to IText7, the PdfAcroForm static method ".GetAcroForm()" worked perfectly, but in the current version of iText7 (7.1.12) a Null Reference Exception is thrown. I have followed the documentation to the best of my ability, but I am unsure how to continue. Any suggestions would be appreciated.

NOTE: The template path exists, the new document shows that it has been filled properly, and it is impossible to "new" a PdfAcroForm, you are required to use the static .GetAcroForm() method.

A null check will not solve this issue, as the object should never be null. The documentation indicates that the .GetAcroForm() method will create a new form if the parameter "createNotExist" is set to true, which I have done here.

I have researched and have located an issue on the iText GitHub that indicates that this issue was "fixed" around a year ago: https://github.com/itext/itext7/pull/44#issue-351612749

The following is the method which prepares the forms:

public string DocumentGenerator(string templatePath, FormFieldSet[] formFieldSet, bool useSpecailOutputPath)
        {
            if(!File.Exists(templatePath))
            {
                throw new Exception("The template file provided does not exist: MC-071(iText)"); 
            }

            string newFile = useSpecailOutputPath ? 
                m_SpecialOutputPath : 
                Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";
            try
            {

                PdfDocument newDocument = new PdfDocument(new PdfReader(templatePath), new PdfWriter(newFile));
                PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(newDocument, true); // <=== Exception Thrown Here

                foreach (FormFieldSet fs in formFieldSet)
                {
                    acroForm.GetField(fs.FieldName).SetValue(fs.FillValue);
                }

                // Sets form flattening
                acroForm.FlattenFields();

                // Closes and writes the form
                newDocument.Close();

                return newFile;
            }
            catch { return string.Empty; }; 
        }

Any suggestions would be greatly appreciated

Upvotes: 2

Views: 3771

Answers (2)

joshman1019
joshman1019

Reputation: 159

Just an update to anyone looking for this issue. This is a known issue and is fixed in the current development branch. You are safe to bypass the exception in visual studio until it is corrected. This has no negative impact on the functionality and is the result of a misplaced return in the original iText7 source.

UPDATE: 8/9/2023 - This bug is still present in the iText library. This issue will not cause a runtime exception in a production application, and is only thrown when a debugger is attached. If Visual Studio throws an exception when testing an application that is producing a PDF, you can bypass it by pressing F5 and your application will proceed to execute without issue.

Upvotes: 7

West Bennett
West Bennett

Reputation: 594

I had the same problem, and after digging down all the way to iText7's internal objects and methods, I finally "solved" my problem.

Apparently iText has some internal errors/exceptions that they are just sort of "skipping" and "pushing past", because I realized by accident that I had "Enable Just My Code" in Visual Studios disabled, and so my system was trying to debug iText7's code as well as mine. The moment that I re-enabled it in my Visual Studio settings (Tools > Options > Debugging > General > Enable Just My Code checkbox), the problem magically went away.

Settings in Visual Studio

So I spent four hours trying to troubleshoot a problem that was in THEIR code, but that they apparently found some way to work around and push through the method anyways even on a null reference failure.

My convert to PDF function is now working just fine.

Upvotes: 12

Related Questions