Search code examples
c#.net-corepdfsharp

Fields on pdf form are invisible


I'm using PDFSharp on .net core 2.2 C#. I'm reading PDF form in my application, than I'm extract form fields and fill them with values. When I save document fields are invisible until I put focus on them (click on them).

Here is code:

                PdfDocument document = PdfReader.Open(@"locationOnPC", PdfDocumentOpenMode.Modify);

                // Get the root object of all interactive form fields
                PdfAcroForm form = document.AcroForm;

                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                var enc1252 = Encoding.GetEncoding(1252);

                PdfTextField field = (PdfTextField)(form.Fields["myField"]);
                field.Value = new PdfString("12345");
                field.ReadOnly = true;
                document.Save(@"myLocation");

Here is picture before focus:

Picture 1

And here is picture after focus:

Picture 2

Anyone know what is problem?


Solution

  • This should be inserted after:

    PdfAcroForm form = document.AcroForm;
    
    if (form.Elements.ContainsKey("/NeedAppearances"))
        form.Elements["/NeedAppearances"] = new PdfBoolean(true);
    else
        form.Elements.Add("/NeedAppearances", new PdfBoolean(true));