1
votes

I have been working with PDFSharp to fill in PDFs with AcroForm fields. I am able to set the value of the form fields by using some code similar to this:

    PdfTextField txtField = (PdfTextField)oldPDF.AcroForm.Fields["fieldname"];
    txtField.Value = new PdfString("my form value");

The problem I have is when I try to change the font for the text that will go into the field. I am able to access the font property of the field, and it lets me set it like this:

txtField.Font = new XFont("Courier New", 16, XFontStyle.Bold);

However, when I open the saved PDF, it defaults the font back to a different font. I have tried using other fonts as well, like "Arial", or "Helvetica", and varying sizes, but nothing seems to stick.

Interesting too, when I open the PDF before I make changes to it with Foxit PhantomPDF I can inspect the form field and see the font is set to Times New Roman

enter image description here

After I fill the PDF and save it, then look at the fields and it will show set to Helvetica 10, no matter what font I try to set in my code.

enter image description here

If I manually edit the form field with Foxit PhantomPDF and change the font that way, it will stick, but I want to do it in code. Is this possible?

1

1 Answers

1
votes

Not sure if this is the proper way, but I am able to change the font by accessing the "/DA" default apperance element in the AcroForm field dictionary and modifying it.

   if(txtField.Elements.ContainsKey("/DA") == false)
      {
         txtField.Elements.Add("/DA", new PdfString("/CoBo 12 Tf 0 g"));
      }
   else
      {
         txtField.Elements["/DA"] = new PdfString("/CoBo 12 Tf 0 g");
      }

I feel like the "Font" property would be the correct way to do this, but I cannot get that method to work.