为什么setFieldProperty返回false ?

本文关键字:false 返回 setFieldProperty 为什么 | 更新日期: 2023-09-27 18:08:58

我的代码将文本设置到字段中没有问题,但是当我试图更改字体时,它返回false。我尝试了一些其他字段属性,如文本大小,结果相同。我做错了什么?

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

附录

    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

为什么setFieldProperty返回false ?

设置文本大小时,需要设置为Float值。如果是int,你用错了方法。当你设置字体时,你需要一个真正的BaseFont对象。我认为你的font.BaseFontnull。我将像这样创建BaseFont:

BaseFont.CreateFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED);

注意,EMBEDDED将被忽略,因为COURIER是标准Type 1字体之一。