I've got one issue with iText right now. I've created a WindowsForms application with C#, it uses iText for generating pdf files with orders. I'm using it in my work, just to save some time and simplify my work a bit, but it's only for me. About a week ago I've started creating another application, this time it's web app with .net core technology.
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");
}
When I tried to use iText, there's an exception, even though code looks the same. I've tried any example source codes and nothing works with console application both .net core and framework, I keep getting the same exception (System.NullREferenceException), even in my first app where everything worked so far.
Stack trace of the exception:
System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=itext.io StackTrace: at iText.IO.Font.FontCache..cctor()
Any ideas what could happen?
The problem was related due some sort of cache. I can't really understand what, but as you can see in the code the exceptions was generate when he tried to load fonts from cache. I realized that after having tried the same code to a new project and it worked.
so I cleaned the solution, deleted bin, obj, .vs, killed IIS Express, removed and reinstalled all nuget packages then run again, magically it worked.
then I had to made only a fix to the code: instead of HtmlConverter.ConvertToDocument that generates only 15 bytes document i used HtmlConverter.ConvertToPdf to generate a full pdf.
User contributions licensed under CC BY-SA 3.0