C# 为什么setFieldProperty返回false?
我的代码将文本设置到字段中没有问题,但是当我尝试更改字体时,它返回false。我尝试了其他一些字段属性,比如文本大小,但结果相同。我做错了什么C# 为什么setFieldProperty返回false?,c#,fonts,itextsharp,C#,Fonts,Itextsharp,我的代码将文本设置到字段中没有问题,但是当我尝试更改字体时,它返回false。我尝试了其他一些字段属性,比如文本大小,但结果相同。我做错了什么 public string Build() { Font font = FontFactory.GetFont(FontFactory.COURIER, 8f, Font.BOLD); foreach (var i in this.pdfFields) { bool work
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
私有字符串模板文件;
私有字符串文件;
私人PdfReader PdfReader;
私人PdfStamper PdfStamper;
私人领域;
私人名单;
公共PDFer(字符串模板文件、字符串新文件)
{
this.templateFile=templateFile;
this.newFile=newFile;
this.pdfReader=新的pdfReader(this.templateFile);
this.pdfStamper=newpdfstamper(pdfReader,newfilestream(this.newFile,FileMode.Create));
this.acroFields=pdfStamper.acroFields;
this.pdfFields=新列表();
}//PDFer
public void AddTextField(字符串名称、字符串值)
{
this.pdfFields.Add(新的PDFTextField(名称、值));
}//AddTextField
public void AddCheckBox(字符串名,bool已选中)
{
this.pdfFields.Add(新的PDFCheckBox(名称,isChecked));
}//添加复选框
公共浮点getWidth(字符串s)
{
块c=新块;
返回c.GetWidthPoint();
}//getWidth
设置文本大小时,该值需要为浮点值。如果是int
,则使用了错误的方法。设置字体时,需要一个真正的BaseFont
对象。我想你的font.BaseFont
是null
。我将创建如下基本字体:
BaseFont.CreateFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED);
请注意,EMBEDDED将被忽略,因为COURIER是标准的Type1字体之一。我尝试了此方法,但仍然得到一个错误。它创建了BaseFont,所以我将字体更改为BaseFont。我还尝试将其插入setFieldProperty以代替font变量。我还尝试创建一个如您所述的basefont,然后从该basefont创建一个字体。也不成功。我已经让我们的一名员工在周末后谈一谈。如果你发送一份PDF和一份完整的代码样本,并将其复制到support(如果你有使用iText的许可证)或邮件列表(如果你的产品也是AGPL),这会有所帮助。是的,我的老板不会允许这样做。不过,我很感谢你的帮助,我会尽力回答出现的问题。我将在原始问题后面附加与iTextSharp有关的所有代码。没有多少。