2013-04-11 166 views
2

我的代码将文本设置为字段没有问题,但是当我尝试更改字体时,它返回false。我尝试了一些其他的字段属性,例如具有相同结果的文本大小。我究竟做错了什么?为什么setFieldProperty返回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 

回答

2

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

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

注意,被嵌入将被忽略,因为快递是标准的Type 1字体之一。

+0

我试过这个,但仍然得到一个错误。它创建一个BaseFont,所以我将字体更改为BaseFont。还尝试将其插入到setFieldProperty中以代替字体变量。 – erosebe

+0

我也试着按照你所描述的创建一个basefont,然后从那个basefont创建一个字体。也不成功。 – erosebe

+0

我已经要求我们的一位员工在周末之后谈论它。如果您发送PDF和完整的代码样本以复制它以支持(如果您有使用iText的许可证)或发送到邮件列表(如果您的产品也是AGPL),这将有所帮助。 –

相关问题