37

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;
        }
    }
2
  • 2
    You should consider the complete message "System.NotSupportedException:Either itext7.bouncy-castle-adapter or itext7. bouncy-castle-fips-adap..." including the part cut off at the ellipse. Add the itext7.bouncy-castle-adapter or itext7. bouncy-castle-fips-adapter to your project. Commented May 16, 2023 at 14:28
  • 1
    @mkl This is not helpful without a reference to how to do so. Commented May 29, 2023 at 15:56

3 Answers 3

76

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I use BouncyCastle 1.8.9 in my project for working with certificates and such. When I use this nuget, I get the error that itext7.bouncy-castle-adapter uses BouncyCastle 1.8.10 which is not available as a nuget. How is this error possible?
based on my understanding, you should uninstall your BouncyCastle 1.8.9 and try again.
This is so confusing. There is itext.bouncy-castle-adapter and itext7.bouncy-castle-adapter. They have the same version number, same published date, support the same frameworks and similar download numbers (sort or). On the website, they only mention IText Core. I'm lost... Which one of the adapter libraries do I choose?
In my case, installing itext7.bouncy-castle-adapter of the same version (i.e. 8.0.3) as itext7 resolved this issue.
@kkuilla, try to exploits official one "itext7.bouncy-castle-adapter".
|
18

I'm using itext7 in a simple LinqPad script and encountered the problem after upgrading to Version 8. To get it up and runnnig again, I had to add references to:

  • itext7.bouncy-castle-adapter (8.0.1)
  • Portable.BouncyCastle (1.9.0)

A restart of LinqPad was also required. As mentioned already, this breaking change is documented here.

2 Comments

This helped in my case. The Portable.BouncyCastle reference was needed and solved my issue.
I am using the LINQPad for macOS beta and had this same issue. These instructions resolved it
6

I added itext7.bouncy-castle-adpater (8.0.5) but was still getting the error. What solved it for me was adding this line to my Program.cs file:

Environment.SetEnvironmentVariable("ITEXT_BOUNCY_CASTLE_FACTORY_NAME", "bouncy-castle");

I hadn't seen this fix anywhere else, but it was briefly mentioned here: https://kb.itextpdf.com/itext/breaking-changes-itext-8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.