Exception filling in form field twice

If I fill in a form field, print it, and then try to do that again, I get a null reference exception with the following callstack:
at Aspose.Pdf.Forms.TextBoxField.hulrgtdcn4a36jvghassc3d9p8uvbscd ( , Annotation )
at Aspose.Pdf.Annotations.WidgetAnnotation.hulrgtdcn4a36jvghassc3d9p8uvbscd (Annotation )
at Aspose.Pdf.Forms.Field.hulrgtdcn4a36jvghassc3d9p8uvbscd ()
at Aspose.Pdf.Forms.Field.(String )
at Aspose.Pdf.Forms.Field.set_Value(String value)
at Aspose.Pdf.Forms.TextBoxField.set_Value(String value)
at Aspose.Pdf.Facades.Form.FillField(String fieldName, String fieldValue)
at Homepoint.DatasystemInterface.AppraisalPrinter.PdfForm.SetFieldString(String sourceFieldName, String newFieldValue) in C:\HomePoint\SourceTrees\TFS_Git\hpfc\DataSystemInterfaces\AppraisalPrinter\Program.cs:line 947
at Homepoint.DatasystemInterface.AppraisalPrinter.PdfForm.SetFieldsString(String[] targetFieldNames, String newValue) in C:\HomePoint\SourceTrees\TFS_Git\hpfc\DataSystemInterfaces\AppraisalPrinter\Program.cs:line 968
at Homepoint.DatasystemInterface.AppraisalPrinter.Form_ReceiptAcknowledge.FillInFormAndPrint(ProcessedLoanInfo sourceData, PrinterSettings printerSettings, PageSettings pageSettings) in C:\HomePoint\SourceTrees\TFS_Git\hpfc\DataSystemInterfaces\AppraisalPrinter\Program.cs:line 1054
at Homepoint.DatasystemInterface.AppraisalPrinter.Program.ProcessLoans(Settings config, RunMode runMode, String singleLoanTestPrint_LoanNumber, String[] specifidLoanNumbers, SecureConfiguration_EncompassReadOnly secureConfigEncompassRead, SecureConfiguration_EncompassReadOnly secureConfigEncompassWrite, EmailKeyVaultConfiguration secureConfigEmail) in C:\HomePoint\SourceTrees\TFS_Git\hpfc\DataSystemInterfaces\AppraisalPrinter\Program.cs:line 384

I'm using Aspose.Pdf 17.3.0.0 on .NET 4.6.1 on Windows Server 2012 R2. This happens consistently the second time when filling out the first field, hpfc address2, which does exist. It does not happen on another form created with the same program. In both cases, the docs were created with Acrobat (I think) and are simple. The code is very straightforward, and looks like this:
if (formTemplate == null)
formTemplate = new Aspose.Pdf.Facades.Form(formSourceFilename);
formTemplate.Save(memoryStream);
Program.PrintStream(memoryStream, printerSettings, pageSettings);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
using (var asposeDocPrinter = new Aspose.Pdf.Facades.PdfViewer())
{
asposeDocPrinter.BindPdf(memoryStream);
asposeDocPrinter.AutoResize = true;
asposeDocPrinter.AutoRotate = true;
asposeDocPrinter.PrintPageDialog = false;
asposeDocPrinter.PrintDocumentWithSettings(pageSettings, printerSettings);
}

I've attached a copy of the problem template. Can you help please? Is there a call I'm missing or do we need to change something in the template?

Hello David,

Thanks for contacting support.

DluxAspose:

This happens consistently the second time when filling out the first field, hpfc address2, which does exist.

I have checked the shared document but I could not find the field with name “hpfc address2” but “HPFC Address” and it was second field in the Fields Collection, not the first one. Though, I have tried to fill the form field (HPFC Address ) before and after printing the document using Aspose.Pdf 17.4.0, and was unable to notice the exception which you have mentioned. Please check the following code snippet that I have used to test the scenario. The document was printed fine and I was able to fill the form field after printing had done.

MemoryStream memoryStream = new MemoryStream();

Aspose.Pdf.Facades.Form formTemplate = new Aspose.Pdf.Facades.Form(dataDir + "ReceiptAcknowledgementTemplate.pdf");

formTemplate.FillField("HPFC Address", "foo");

formTemplate.Save(memoryStream);


System.Drawing.Printing.PageSettings pageSettings = new System.Drawing.Printing.PageSettings();

System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();


memoryStream.Seek(0, System.IO.SeekOrigin.Begin);


using (var asposeDocPrinter = new Aspose.Pdf.Facades.PdfViewer())

{

    asposeDocPrinter.BindPdf(memoryStream);

    asposeDocPrinter.AutoResize = true;

    asposeDocPrinter.AutoRotate = true;

    asposeDocPrinter.PrintPageDialog = false;

    asposeDocPrinter.PrintDocumentWithSettings(pageSettings, printerSettings);

}


formTemplate = new Aspose.Pdf.Facades.Form(memoryStream);

formTemplate.FillField("HPFC Address", "foo2");

formTemplate.Save(memoryStream);


using (var asposeDocPrinter = new Aspose.Pdf.Facades.PdfViewer())

{

    asposeDocPrinter.BindPdf(memoryStream);

    asposeDocPrinter.AutoResize = true;

    asposeDocPrinter.AutoRotate = true;

    asposeDocPrinter.PrintPageDialog = false;

    asposeDocPrinter.PrintDocumentWithSettings(pageSettings, printerSettings);

}

Please try using latest version of the API as it is strongly recommended to use latest version and in a case if you still face any issue, please share a complete code snippet which you are using to fill the Form Fields. This would help us in replicating the scenario in our environment.

Best Regards,

Hi, thanks for the reply. Correct, the field "hpfc address2" is not the second in the collection, it is the nineth. I have code that checks for the field's existence before setting it and throwing if it does not exist.

This is not a problem with the 2nd field. The problem happens the second time the code I showed is run. In other words, after filling in the fields, saving the PDF to a stream, and printing that stream, I have to do that again for the next record in the database. As soon as I start processing or record #2 - when I set "hpfc address2" - I get this exception from Aspose. I see no documentation saying not to do this, so I am at a loss.

Hello David,


Thanks for adding more details to the scenario. I have again tested it with “hpfc address2” field by the code shared above and I did not notice any exception. As you can see in the above code that I am filling a TextBoxField twice i.e before/after printing and I observed no issue while printing the document even second time.

Moreover, you may also check in the code that I am saving the document in the stream after filling the form field and using same stream for printing. You can compare this routine with the one which you are trying and in a case if it is different, please let us know by sharing a complete code snippet. We will try to replicate the issue in our environment and address it accordingly.

We are sorry for the inconvenience.


Best Regards,
Thanks! I see the key difference between your code and mine is that before the second field change, you re-recreate the form object:
formTemplate = new Aspose.Pdf.Facades.Form(memoryStream);

I changed my code accordingly and it now works fine. Do the docs say that after doing a render to stream the form object must be disposed of? Can you point me to where so I can read up?

Thanks again.

Hi David,

Thanks for your feedback. It is good to know that suggested code worked for you. I have tested the scenario using Aspose.Pdf for .NET 17.4.0 by commenting the line of code as well and have not noticed any issue. Please use latest version to fix the issue.

We are sorry for this inconvenience.

Best Regards,

DluxAspose:
Thanks! I see the key difference between your code and mine is that before the second field change, you re-recreate the form object:
formTemplate = new Aspose.Pdf.Facades.Form(memoryStream);

I changed my code accordingly and it now works fine. Do the docs say that after doing a render to stream the form object must be disposed of? Can you point me to where so I can read up?
Hello David,

Thanks for the acknowledgement.

When we call document print routine, the document handler is closed. So in order to fill further information, we need to load the same document again, so that data can be filled and then further operation can be performed. I am afraid this information is not documented because its similar to calling save(..) method after PDF manipulation.