当使用iTextSharp更改现有AcroForm文档中textfont字段的字体时,我在设置'textfont‘属性的行上遇到空指针异常。不过,我可以使用相同的代码来设置字段的值。我按照以下示例创建了我的代码https://stackoverflow.com/questions/14748901/custom-font-not-getting-applied-on-existing-pdf-template-itextsharppdfstamper
我的堆栈跟踪如下所示:
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我的代码如下所示:
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版本中工作得很好。
发布于 2013-02-08 19:52:00
因为你没有分享PDF,所以我用我创建的一些PDF做了一些测试。我发现在/NeedAppearances设置为true的表单中,预填充域没有被展平(它们只是作为空域出现)。我猜我一直在处理/NeedAppearances为false (这是默认设置)的表单,因此从未遇到过这个问题。我修复了这个问题:http://sourceforge.net/p/itext/code/5682/
修复将在下一个版本中提供。
https://stackoverflow.com/questions/14754373
复制相似问题