PdfContentByte.SetFontAndSize()抛出“;对象引用未设置为对象的实例“;
本文关键字:设置 对象 实例 对象引用 抛出 SetFontAndSize PdfContentByte | 更新日期: 2024-10-22 11:06:29
我正在尝试添加一个文本框注释,其文本具有指定的字体和大小。代码如下:
void addTextBox(string inputPath,string outputPath)
{
PdfReader pdfReader = new PdfReader(inputPath);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outputPath, FileMode.Create));
PdfContentByte pcb = new PdfContentByte(pdfStamper.Writer);
BaseFont baseFont = FontFactory.GetFont(FontFactory.HELVETICA).BaseFont;
float fontsize = 32;
pcb.SetFontAndSize(baseFont, fontsize);
PdfAnnotation textbox = PdfAnnotation.CreateFreeText(pdfStamper.Writer, new iTextSharp.text.Rectangle(200, 200, 3000, 3000), "Here is a Textbox", pcb);
pdfStamper.AddAnnotation(textbox, 1);
pdfStamper.Close();
}
pcb.SetFontAndSize()
调用正在引发异常:
Object reference not set to an instance of an object.
发生此错误时,pcb
已实例化,并且fontsize
已成功分配其数值,那么这里未分配的对象是什么?
用pdfStamper.GetOverContent(1)
替换PdfContentByte(pdfStamper.Writer)
,问题就会消失。