My code sets text into the fields no problem, but when I try to change the font it returns false. I tried some other field properties, like text size with the same result. What am I doing wrong?
public string Build()
{
Font font = FontFactory.GetFont(FontFactory.COURIER, 8f, Font.BOLD);
foreach (var i in this.pdfFields)
{
bool worked = this.acroFields.SetFieldProperty(i.Name, "textfont", font.BaseFont, null);
worked = this.acroFields.SetField(i.Name, i.Value);
}//foreach
this.pdfStamper.FormFlattening = false;
this.pdfStamper.Close();
return this.newFile;
}//Build
Addendum
private string templateFile;
private string newFile;
private PdfReader pdfReader;
private PdfStamper pdfStamper;
private AcroFields acroFields;
private List<PDFField> pdfFields;
public PDFer(string templateFile, string newFile)
{
this.templateFile = templateFile;
this.newFile = newFile;
this.pdfReader = new PdfReader(this.templateFile);
this.pdfStamper = new PdfStamper(pdfReader, new FileStream(this.newFile, FileMode.Create));
this.acroFields = pdfStamper.AcroFields;
this.pdfFields = new List<PDFField>();
}//PDFer
public void AddTextField(string name, string value)
{
this.pdfFields.Add(new PDFTextField(name, value));
}//AddTextField
public void AddCheckBox(string name, bool isChecked)
{
this.pdfFields.Add(new PDFCheckBox(name, isChecked));
}//AddCheckBox
public float getWidth(string s)
{
Chunk c = new Chunk(s);
return c.GetWidthPoint();
}//getWidth
When you set the text size, the value needs to be a Float value. If it's an int, you're using the wrong method. When you set the font, you need a real BaseFont object. I think your font.BaseFont is null. I'd create the BaseFont like this:
BaseFont.CreateFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED);
Note that EMBEDDED will be ignored as COURIER is one of the Standard Type 1 fonts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With