Search code examples
c#pdftextstylingitext7

iText7 text value is hidden


I have a secured PDF Template with editable fields. When I set a field's value it doesn't show up until I click on it and modify it.

Code for inserting a value into a field:

static void Main(string[] args)
    {
        using (PdfReader reader = new PdfReader(desktopPath + "PdfTemplate.pdf"))
        {
            reader.SetUnethicalReading(true);
            using (PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(desktopPath + "ModifiedPdfTemplate.pdf")))
            {
                PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);
                IDictionary<string, PdfFormField> fields = form.GetFormFields();
                fields["Date"].SetValue("DATE");
            }
        }
    }

This is what an unedited field looks like:

enter image description here

After I run the code, the field still looks like before, but there is a value if I click on it:

enter image description here

After I modified the field (added a space at the end), now it shows the text:

enter image description here

I think it's because there is some styling on the field. How can I achieve that is shown on the last picture?

Software Versions:

Itext -> 7.1.16

Adobe Acrobat -> 2020.009.20063


Solution

  • After the attempt to reproduce the issue here failed, the next step was comparing versions of software involved.

    Synchronizing the iText version to the current 7.1.16 still resulted in different observations.

    But then updating the PDF viewer, Adobe Acrobat (Reader), finally resolved the issue.

    Apparently Acrobat 2020.009.20063 fails to display the field properly while Acrobat 2021.001.20155 and 2021.007.20091 succeed.

    (One might think that basic functionality like field value showing should have been stable in Acrobat for a number of years. But apparently changes do still occur here. This may be related to hardening of signed forms against forgery in the recent months and years.)