3

我的代码将文本设置到字段中没有问题,但是当我尝试更改字体时它返回 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
4

1 回答 1

3

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

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

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

于 2013-04-12T10:48:44.377 回答