PdfContentByte.SetFontAndSize() 抛出 "Object reference not set to instance of object"
PdfContentByte.SetFontAndSize() throws "Object reference not set to instance of object"
我正在尝试添加一个文本框注释,其文本具有指定的字体和大小。代码如下:
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
也已经成功赋值了它的数值,那么这里未赋值的对象是什么?
将PdfContentByte(pdfStamper.Writer)
替换为pdfStamper.GetOverContent(1)
,问题就会消失。
我正在尝试添加一个文本框注释,其文本具有指定的字体和大小。代码如下:
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
也已经成功赋值了它的数值,那么这里未赋值的对象是什么?
将PdfContentByte(pdfStamper.Writer)
替换为pdfStamper.GetOverContent(1)
,问题就会消失。