Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve Unknown PdfException when using itext7 in. net maui

Error returned on return: Unknown PdfException. - - > System.NotSupportedException:Either itext7.bouncy-castle-adapter or itext7. bouncy-castle-fips-adap... On line

Var writer = new PdfWriter(stream);

My code is try to get create a pdf file using itext7 extension. In.net maui for mobile apps.

public static MemoryStream CreatePdf()
    {
        try
        {
            using (var stream = new MemoryStream())
            {
                var writer = new PdfWriter(stream);
                var pdf = new PdfDocument(writer);
                var document = new Document(pdf);
                var paragraph = new Paragraph("hello");
                document.Add(paragraph);
                document.Close();
                stream.Position = 0;
                return stream;
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;
        }
    }
like image 954
Conrad Budde Avatar asked Aug 31 '25 15:08

Conrad Budde


1 Answers

The reason it's occurring because from version 8.0, iText7 did several major breaking changes in the way they handle bouncy-castle dependencies. I assumed, your itext7 version is less than 8. So,

For resolving this issue, you need to install another Nuget package:

  1. Name: itext7.bouncy-castle-adapter
  2. Link: https://www.nuget.org/packages/itext7.bouncy-castle-adapter

You can also check-out this answer, if you need more comprehensive solution including images.

  • Link: https://stackoverflow.com/a/75168612/13861641
like image 72
Akter Avatar answered Sep 02 '25 21:09

Akter