Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iText keeps throwing NullReferenceException

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?

Screenshot

like image 948
szybki Avatar asked May 01 '26 19:05

szybki


1 Answers

I also encountered this exception.

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=itext.io
  StackTrace: 
    iText.IO.Font.FontCache..cctor()

But I tried to fix this exception!

Try to Install package from NuGet.

itext7.font-asian

I don't know why, but it works successfully.

This question helped me: How do I read Japanese characters from a PDF?

like image 150
Michelle Guo Avatar answered May 04 '26 07:05

Michelle Guo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!