Skip to main content
Skip table of contents

How to change the text color of an AcroForm field?

When I change the text color for a field it does not change. Why is that?

I have a PDF with named fields I created with Adobe LifeCycle. I am able to fill the fields using iTextSharp but when I change the text color for a field it does not change. I really don't know why this is so. Here is my code:

C#
form.SetField("name", "Michael Okpara");
form.SetField("session", "2014/2015");
form.SetField("term", "1st Term");
form.SetFieldProperty("name", "textcolor", BaseColor.RED, null);
form.RegenerateField("name");

Posted on StackOverflow on Nov 21, 2014 by Okolie Solomon

If your form is created using Adobe LifeCycle, then there are two options:

  • You have a pure XFA form. XFA stands for the XML Forms Architecture and your PDF is nothing more than a container of an XML stream. There is hardly any PDF syntax in the document and there are no AcroForm fields. I don't think this is the case, because you are still able to fill out the fields (which wouldn't work if you had a pure XFA form).

  • You have a hybrid form. In this case, the form is described twice inside the PDF file: once using an XML stream (XFA) and once using PDF syntax (AcroForm). iText will fill out the fields in both descriptions, but the XFA description gets preference when rendering the document. Changing the color of a field (or other properties) would require changing the XML and iText(Sharp) can not do that.

If I may make an educated guess, I would say that you have a hybrid form and that you are only changing the text color of the AcroForm field without changing the text color in the XFA field (which is really hard to achieve).

Please try adding this line in iText 7:

C#
form.removeXfaForm();

This will remove the XFA stream, resulting in a form that only keeps the AcroForm description.

I have written a small example named RemoveXFA (Java/.NET) using the form you shared to demonstrate this:

JAVA
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);

        // Method removes the XFA stream from the document.
        form.removeXfaForm();

        Map<String, PdfFormField> fields = form.getAllFormFields();
        for (Map.Entry<String, PdfFormField> name : fields.entrySet()) {
            if (name.getKey().indexOf("Total") > 0) {
                name.getValue().setColor(ColorConstants.RED);
            }

            name.getValue().setValue("X");
        }

        pdfDoc.close();
C#
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);

            // Method removes the XFA stream from the document.
            form.RemoveXfaForm();

            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
            foreach (KeyValuePair<String, PdfFormField> name in fields)
            {
                if (name.Key.IndexOf("Total") > 0)
                {
                    name.Value.SetColor(ColorConstants.RED);
                }

                name.Value.SetValue("X");
            }

            pdfDoc.Close();

As you can see, I remove the XFA stream and I look over all the remaining PdfFormFields. I change the text color of all the fields with the word "Total" in their name, and I fill out every field with an "X".

Resulting PDF

Resulting PDF

All the fields show the letter "X", but the fields in the TOTAL column are written in red.

Click How to change the text color of an AcroForm field? if you want to see how to answer this question in iText 5.

Creating XFA forms required the use of Adobe LiveCycle Designer, however, Adobe LiveCycle was discontinued in March 2018. In addition, XFA forms were deprecated with the publication of the ISO PDF 2.0 specification in 2017.

We developed the pdfXFA add-on for iText Core to assist with existing XFA workflows. However, for new workflows we recommend alternative solutions such as Fluent, which offers dynamic data-driven document generation in a wide range of document formats, including PDF, PDF/A and PDF/UA.

If you need to process XFA documents, we have a related blog post that may be interesting to you:

JavaScript errors detected

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

If this problem persists, please contact our support.