Skip to main content
Skip table of contents

How to format a field as a currency?

I use iTextSharp to populate a form with data. I just learned [/question/how-format-field-percentage](how to format a field as a percentage). Now I need to learn how to format numbers.

This is what I've tried:

Stream os = new FileStream(PDFPath, FileMode.CreateNew);
PdfReader reader = new PdfReader(memIO);
PdfStamper stamper = new PdfStamper(reader, os, '9', true);
AcroFields fields = stamper.AcroFields;
fields.SetField("Pgo", "1.0",  "100%");    // Works fine
fields.SetField("value", "1217000.000000",  "$1,217,000");    // Drops Dollar sign and comma

It doesn't work. What am I doing wrong?

Posted on StackOverflow on Dec 30, 2014 by Arne

Please take a look at the FormatFields example. In that example, I took an ordinary form with a couple of ordinary fields, and I filled these fields in the exact same way as you did.

The result looks as expected:

Currency in field

Currency in field

iText has created two appearances (the /AP entry of the widget annotation) based on the display parameter we passed to the setField() method. One field shows "100$", the other field shows "$1,217,000". As you can see (and check for yourself here), the dollar sign and the commas are there.

However, the moment you click one of those fields, the appearance that was created by iText disappears and it's replaced by an appearance created by Adobe Reader (or any other PDF viewer) based on the value of the field (the /V entry of the field dictionary): "1.0" or "1217000.000000". This is expected behavior.

I am not claiming that your allegation is wrong, but maybe there is some JavaScript in your form that sets the focus to your value field. Maybe there's an open action that performs some JavaScript that formats the value field. Maybe something else is at play, but in any case: the reason for the phenomenon you describe is buried somewhere in your form. It is not a problem caused by iText: my example proves that the answer to the question How to format a field as a percentage? also works for currency values.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.