我目前在使用iText时遇到了一个问题。
我使用C#创建了一个WindowsForms应用程序,它使用iText来生成订单的pdf文件。
我在工作中使用它,只是为了节省一些时间并简化我的工作,但这只适用于我个人。
大约一周前,我开始创建另一个应用程序,这次是使用.net core技术的Web应用程序。
public IActionResult OnPostPrintToPDF()
{
byte[] pdfBytes;
MemoryStream stream = new MemoryStream();
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
stream.Position = 0;
Paragraph par;
par = new Paragraph("Hello world");
document.Add(par);
document.Close();
pdfBytes = stream.ToArray();
return new FileContentResult(pdfBytes, "application/pdf");
}
当我尝试使用iText时,出现了异常,尽管代码看起来一样。我试过所有的示例源代码,无论是在.net core还是framework控制台应用程序中都不起作用,我仍然会收到相同的异常(System.NullReferenceException),即使在我的第一个应用程序中一切都正常。
异常的堆栈跟踪:
System.NullReferenceException HResult=0x80004003 Message=对象引用未设置为对象的实例。 Source=itext.io StackTrace: at iText.IO.Font.FontCache..cctor()
有什么想法是怎么发生的?
截图如下:https://istack.dev59.com/hLMOw.webp
stream.Position = 0
很可能会破坏结果 PDF。 - mkl