iTextSharpを使用して既存のPDFドキュメントのAcroFormフィールドのフォントを変更しているときに、「textfont」プロパティを設定している行でnullポインター例外が発生します。ただし、同じコードを使用してフィールドの値を設定することはできます。次の例に従って、コードを作成しました 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で正常に機能します。