Saving PDF file can cause NaN to show in calculation fields

When saving the attached PDF document using the following code, the saved copy has many calculation fields displaying “NaN”: test.pdf (629.5 KB)

new Document("test.pdf").Save();

Here is an example of the incorrect output: image.png (72.9 KB)

@bvadala,

Can you attach the original one? Because the one attached already had those $NaN values so it is normal that the save result will have them too. I want the input, not the output.

I also modify a field And then save it and the values are preserved.


FieldsWithNaNValue_output.pdf (667.9 KB)

Hi @carlos.molina,

Sorry for the late reply. Please see the attached original copy of the document.
test.pdf (604.5 KB)

@bvadala,

I just run the following code:

private void Logic()
{
    var doc = new Document($"{PartialPath}_input.pdf");

    Random rnd = new Random();           

    foreach (var field in doc.Form.Fields)
    {
        if (field is TextBoxField)
        {
            field.Value = rnd.Next(0,10000).ToString();
        }
    }

    doc.Save($"{PartialPath}_output.pdf");
}

The output file:
FieldsWithNaNValue_1_output.pdf (622.9 KB)

As you see, no field shows NaN. So the document is fine when you save it. Probably you did not add the right values to those TextBoxFields, therefore the validation error.

Hi @carlos.molina,

This is exactly the problem. In reality, there are cases that not all fields require to be filled. The output of the PDF shouldn’t automatically put in NAN for the fields that are not filled.

When the same file is opened in Acrobat DC and saved to a different copy, it doesn’t have this problem.

@bvadala

That is not accurate. I just saved a document without any value, and you can see there is no NaN in any field.

Here is the code:

private void Logic()
{
    var doc = new Document($"{PartialPath}_input.pdf");

    doc.Save($"{PartialPath}_output.pdf");
}

The input and output
test.pdf (604.5 KB)
FieldsWithNaNValue_1_output.pdf (593.1 KB)

Here is the input and output. So I can say for sure you are adding values that are making the NaN appears. Because with proper numbers, they show and number, and without a number, they show 0%.

Hi @carlos.molina,

My apologies for missing an important step in my code sample. You actually need to call doc.Flatten() before saving it to see this problem, and only in Aspose.PDF v23.1.0 and prior.

I realised today that this bug is fixed in v23.2 and later versions.

Thank you for all your responses.

@bvadala,

No problem. Glad it id working with the latest API version.