Getting an object reference error with iText7 8.0.2 GetAcroForm.GetField.SetValue() within a COMHOST library

93 Views Asked by At

Creating a COMHOST library for a legacy VB6 application and while it compiles correctly and all other functionality works (including some other recent nuget packages), I'm running into an issue with iText8.0.1. The iText8.0.1 code all works fine when hosted in a .NET6 application, so it's not syntax issues. But when hosting via VB6, the same line of code returns a null object and I get a "Object reference not set to an instance of an object" error.

I've managed to attach to the VB6 process and step through my .NET code and these are the lines causing the issue.

var pdfDoc = new PdfDocument(new PdfReader(pdfTemplate), new PdfWriter(newFileName));
var form = PdfFormCreator.GetAcroForm(pdfDoc, true);

The values for pdfTempate and newFileName are all correct, but the first line sets pdfDoc to null and therefore the second line throws the "Object reference not set to an instance of an object" error.

We used to use iText5.5.13 for this process, but I updated the library and recoded for the new iText8.0.1. I can roll those changes back of course and go with the deprecated EOL version, but if there is a way to insure the newer version will work, I'd rather fix that.

UPDATE: So I had to make some changes to debug properly with COMHOST, turning off optimizations. I also broke the code into steps as follows to make it easier to see where the issue is. Turns out the object reference error was not on the second line above as thought (that was optimized code jumping the cursor to incorrect places)

var reader = new PdfReader(pdfTemplate);
var writer = new PdfWriter(newFileName);
var pdfDoc = new PdfDocument(reader, writer);
var form = PdfFormCreator.GetAcroForm(pdfDoc, true);
form.GetField("shipperAccount").SetValue(shipperAccount); // HERE

The error is on the line marked HERE above.

In debug immediate window form.GetField("shipperAccount") works fine. However the call to .SetValue is where the mystery error happens (only on COMHOST, not native C#/.NET 6 use of the library). So to clarify, the form does have a shipperAccount field and the shipperAccount variable has a correct value. But the call to .SetValue either inline above or by itself after retrieving the field generates the object reference error.

UPDATE #2 Attaching to the process, I tried several different changes and oddly when I do the following:

form.GetField("shipperAccount").SetValue(shipperAccount, false);

THAT line works. Doing that with "true" raises the object error inside the iText7 code. The boolean tells iText whether to generate the field appearance or not.

Likewise the call to flatten the form also fails with the same error. Wrote to their tech support, though they try to direct all questions to StackOverflow. Hopefully someone will respond. If not, I may have to download the source code and try to debug myself or switch to a new library.

UPDATE #3 I've reported this to the developer of iText. Nothing back from them, but they are aware of this bug and this thread. Hopefully they come and respond at some point. However, to get moving, I tried several other PDF libraries and IronPDF worked for our needs and was quick and easy to implement.

0

There are 0 best solutions below