首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iTextSharp中设置AcroFields的"textfont“属性时出现空异常

在iTextSharp中设置AcroFields的"textfont“属性时出现空异常
EN

Stack Overflow用户
提问于 2013-02-07 23:08:52
回答 1查看 4.1K关注 0票数 0

当使用iTextSharp更改现有AcroForm文档中textfont字段的字体时,我在设置'textfont‘属性的行上遇到空指针异常。不过,我可以使用相同的代码来设置字段的值。我按照以下示例创建了我的代码https://stackoverflow.com/questions/14748901/custom-font-not-getting-applied-on-existing-pdf-template-itextsharppdfstamper

我的堆栈跟踪如下所示:

代码语言:javascript
复制
at iTextSharp.text.pdf.PdfContentByte.SaveColor(BaseColor color, Boolean fill)
at iTextSharp.text.pdf.PdfContentByte.SetRGBColorFill(Int32 red, Int32 green, Int32 blue)
at iTextSharp.text.pdf.PdfContentByte.SetColorFill(BaseColor value)
at iTextSharp.text.pdf.AcroFields.SetFieldProperty(String field, String name, Object value, Int32[] inst)
at iTextSharpUsage.Utilities.UpdateFontUsingEmbededFont(String inputPdf, String resultPdf) in D:\iTextSharpUsage\iTextSharpUsage\iTextSharpUsage\Utilities.cs:line 229

我的代码如下所示:

代码语言:javascript
复制
var pdfReader = new PdfReader(inputPdf);
string fontsfolder = @"D:\Airmole\airmole.ttf";
var pdfStamper = new PdfStamper(pdfReader, new FileStream(resultPdf, FileMode.Create));
BaseFont customfont = BaseFont.CreateFont();
AcroFields af = pdfStamper.AcroFields;
List<BaseFont> list = new List<BaseFont>();
list.Add(customfont);
iTextSharp.text.Font bold = new iTextSharp.text.Font(customfont, 13,0,BaseColor.BLACK);
af.SubstitutionFonts = list;
foreach (var field in af.Fields)
{
    af.SetField(field.Key, "s");
    //this line works fine
    bool isSuccess = pdfStamper.AcroFields.SetFieldProperty(field.Key, "textcolor ", BaseColor.BLACK , null);
    //the line bellow throws a null pointer exception
    bool isSucces1s = pdfStamper.AcroFields.SetFieldProperty(field.Key, "textfont", customfont, null);
}

我需要添加更多的代码才能工作吗??

这里有一个快速更新...多亏了@Bruno发布的评论,我解决了这个问题,这个功能在5.3.3版本中工作得很好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-08 19:52:00

因为你没有分享PDF,所以我用我创建的一些PDF做了一些测试。我发现在/NeedAppearances设置为true的表单中,预填充域没有被展平(它们只是作为空域出现)。我猜我一直在处理/NeedAppearances为false (这是默认设置)的表单,因此从未遇到过这个问题。我修复了这个问题:http://sourceforge.net/p/itext/code/5682/

修复将在下一个版本中提供。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14754373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档