2013-04-11 164 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),這將有所幫助。 –

相關問題