Object ref not found; setting pdf form field values & calling the Draw() method on the MergeDocument

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Generator for .NET (v4)  /  Object ref not found; setting pdf form field values & calling the Draw() method on the MergeDocument

DynamicPDF Generator for .NET (v4) Forum


First of all, my license is for DynamicPDF, but I'm upgrading soon to PDF Merger for .Net. My application is actually an ASP.NET MVC 2 application using C# & ASP.Net 3.5.

I'm programmatically setting form field values on the pdf and then calling the Draw() method to get a byte array so I can stream that to the user for download. I've noticed that if you set a form field value for a field that doesn't exist, no error is thrown until you call the Draw() method on the MergeDocument. The error is: Object reference not set to an instance of an object.

I created a helper method to try to protect myself from this, but it still happens on occasion and I can't figure out what field I'm giving a value for that the method doesn't like. See my code below.

/**
         * Helper function to set field values on pdf's. The call to .draw() will fail if you
         * set a value for a field that does not exist. This method recovers gracefully by logging
         * the fact that a field value was not set, but allows the process to proceed without error.
         */
        private void setField( ref MergeDocument document, string fieldName, string fieldValue)
        {
            bool valueWasSet = false;

            foreach (ceTe.DynamicPDF.Forms.TextField t in document.Form.GetTextFields())
            {               
                if (t.Name.Equals(fieldName) &&
                    !document.Form.Fields[t.Name].IsReadOnly )
                {
                    t.Value = fieldValue;
                    valueWasSet = true;
                }
            }

            //foreach (ceTe.DynamicPDF.Forms.FormField f in document.Form.Fields)
            //{
            //    if (f.Name.Equals(fieldName) && !document.Form.Fields[f.Name].IsReadOnly )
            //    {
            //        document.Form.Fields[f.Name].Value = fieldValue;
            //        valueWasSet = true;
            //    }
            //}

            if (!valueWasSet)
            {
                LogUtil.Write("setField", string.Format("Value was not set for field: {0} and value: {1} because the field name could not be found on the pdf document.", fieldName, fieldValue));
            }
        }

The full error stacktrace reads:

Message: Object reference not set to an instance of an object. StackTrace:    at zz93.gu.a(DocumentWriter A_0)
   at ceTe.DynamicPDF.Forms.FormField.Draw(DocumentWriter writer)
   at ceTe.DynamicPDF.IO.DocumentResourceList.a(DocumentWriter A_0)
   at zz93.ab.v()
   at ceTe.DynamicPDF.Document.Draw(Stream stream)
   at ceTe.DynamicPDF.Document.Draw()

I'm grasping at straws. I need a safe way to supply form field values for a given field name, but only when the field exists and it's valid to do so.

Thanks!
Posted by a ceTe Software moderator
Hello,

Can you please send the PDF document to our Support Team so that they can take a look at what is causing this error?

The error is being thrown when trying to set the value for the form field that either does not exist or the field name is not correct.

Thanks,
ceTe Software Support Team
I don't see how that's possible. I'm explicitly iterating over the form fields on the pdf to ensure that the form field exists before supplying a value. See example function in OP.
To answer your question, I actually can't send the pdf. It has some pretty sensitive information for our company internally. I can say it's incredibly complex and uses a ton of javascript as well as the FDF Toolkit from Adobe.


I found the problem.

In my setValue method, I wasn't checking 'fieldValue' for null. It seems when this value is null, the error detailed with the Draw method occurs. I've revised the method like so:

/**
         * Helper function to set field values on pdf's. The call to .draw() will fail if you
         * set a value for a field that does not exist. This method recovers gracefully by logging
         * the fact that a field value was not set, but allows the process to proceed without error.
         */
        private void setField( ref MergeDocument document, string fieldName, string fieldValue)
        {
            bool valueWasSet = false;

            foreach (ceTe.DynamicPDF.Forms.TextField t in document.Form.GetTextFields())
            {               
                if (t.Name.Equals(fieldName) &&
                    !document.Form.Fields[t.Name].IsReadOnly
                     )
                {
                    if (String.IsNullOrEmpty(fieldValue)) fieldValue = string.Empty;

                    t.Value = fieldValue;
                    valueWasSet = true;
                }
            }

            //foreach (ceTe.DynamicPDF.Forms.FormField f in document.Form.Fields)
            //{
            //    if (f.Name.Equals(fieldName) && !document.Form.Fields[f.Name].IsReadOnly )
            //    {
            //        document.Form.Fields[f.Name].Value = fieldValue;
            //        valueWasSet = true;
            //    }
            //}

            if (!valueWasSet)
            {
                LogUtil.Write("setField", string.Format("Value was not set for field: {0} and value: {1} because the field name could not be found on the pdf document.", fieldName, fieldValue));
            }
        }

I added a null check to fieldValue and set fieldValue to an empty string if it's null. After doing this, I am no longer getting issues with the Draw() method on the MergeDocument object.

You may close this ticket / issue. Thanks!

All times are US Eastern Standard time. The time now is 1:01 PM.