Didstopia / PDFSharp

A .NET Standard 2.0 library for reading, writing and editing PDF files.
MIT License
52 stars 18 forks source link

Null reference exception even with "NeedAppearances" boolean set #14

Closed FuyukiSakura closed 6 years ago

FuyukiSakura commented 6 years ago

I remember there's a problem with PDFSharp in AcroForms so have examine the same piece of code carefully with the original distribution of PDFSharp to make sure that the code should work.

image

Code used

using (var pdf = PdfReader.Open(filename, PdfDocumentOpenMode.Modify))
{
    //Enable AcroForm
    var acroForm = pdf.AcroForm;
    if (!pdf.AcroForm.Elements.ContainsKey("/NeedAppearances"))
    {
        pdf.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
    }
    else
   {
       pdf.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
   }

   //Fill the form
   ((PdfTextField)acroForm.Fields["foobar"]).Value = new PdfString("Hello World");
   pdf.Save(filename);
}

This worked on .NET Framework 4.6.2 with official PDFSharp. I am sorry if I am still wrong. Please correct me if this is not related to the library iteself.