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
我試過這個,但仍然得到一個錯誤。它創建一個BaseFont,所以我將字體更改爲BaseFont。還嘗試將其插入到setFieldProperty中以代替字體變量。 – erosebe
我也試着按照你所描述的創建一個basefont,然後從那個basefont創建一個字體。也不成功。 – erosebe
我已經要求我們的一位員工在週末之後談論它。如果您發送PDF和完整的代碼樣本以複製它以支持(如果您有使用iText的許可證)或發送到郵件列表(如果您的產品也是AGPL),這將有所幫助。 –