Skip to content
Learn Netverks
0

iText.Commons.Utils.DIContainer.GetInstance[T] NullReferenceException in Andriod Unity

asked 8 hours ago by @qa-fnjluoee0l8uhghwkuk3 0 rep · 39 views

c# itext

I am working with iText to generate PDF. It is working fine in the editor(windows) but when i running the andriod build it throws the following exception.

2026/06/01 12:45:41.669 29785 29810 Error Unity NullReferenceException: Object reference not set to an instance of an object.

2026/06/01 12:45:41.669 29785 29810 Error Unity at iText.Commons.Utils.DIContainer.GetInstance[T] () [0x00000] in <00000000000000000000000000000000>:0

2026/06/01 12:45:41.669 29785 29810 Error Unity at iText.Kernel.Pdf.PdfPagesTree..ctor (iText.Kernel.Pdf.PdfCatalog pdfCatalog) [0x00000] in <00000000000000000000000000000000>:0

2026/06/01 12:45:41.669 29785 29810 Error Unity at iText.Kernel.Pdf.PdfCatalog..ctor (iText.Kernel.Pdf.PdfDictionary pdfObject) [0x00000] in <00000000000000000000000000000000>:0

2026/06/01 12:45:41.669 29785 29810 Error Unity at iText.Kernel.Pdf.PdfDocument.Open (iText.Kernel.Pdf.PdfVersion newPdfVersion) [0x00000] in <00000000000000000000000000000000>:0

2026/06/01 12:45:41.669 29785 29810 Error Unity at iText.Kernel.Pdf.PdfDocument..ctor (iText.Kernel.Pdf.PdfWriter writer) [0x00000] in <00000000000000000000000000000000>:0

2026/06/01 12:45:41.669 29785 29810 Error Unity at PdfGenerator.CreatePdfFromImages (System.Collections.Generic.List`1[T] imagePaths, System.String pdfFileName, System.String ouputPath) [0x00000] in <00000000000000000000000000000000>:0

2026/06/01 12:45:41.669 29785 29810 Error Unity

Here is my part of pdf generation code. I can confirm that pdfdocument log is not appearing

    Debug.Log("=====PDF GENERATION STARTED=====");
    // 1. Initialize PDF writer and PDF document
    PdfWriter writer = new PdfWriter(savePath);
    Debug.Log("PDF Writer initialized at: " + savePath);
System.Runtime.CompilerServices.RuntimeHelpers
    .RunClassConstructor(typeof(iText.Commons.Utils.DIContainer).TypeHandle);
Debug.Log("DIContainer cctor forced");

PdfDocument pdfDoc = new PdfDocument(writer);
    Debug.Log("PDF Document created successfully.");

    // 2. Initialize layout Document (Defaults to A4)
    Document document = new Document(pdfDoc, PageSize.A4);
    Debug.Log("Layout Document initialized with A4 page size.");
    // 2. Instantiate your custom handler
    HeaderFooterHandler handler = new HeaderFooterHandler();

Here is my link.xml file so that no code should excluded in the final build. Maybe this have any problem?

<assembly fullname="itext.kernel" preserve="all">
    <type fullname="iText.Kernel.Pdf.PdfDocument" preserve="all">
        <method signature="Void .cctor()" />
    </type>
    <type fullname="iText.Kernel.Pdf.PdfPagesTree" preserve="all">
        <method signature="Void .cctor()" />
    </type>
</assembly>

<!-- Keep everything else the same as before -->
<assembly fullname="itext" preserve="all"/>
<assembly fullname="itext.io" preserve="all"/>
<assembly fullname="itext.layout" preserve="all"/>
<assembly fullname="itext.bouncy-castle-adapter" preserve="all"/>
<assembly fullname="itext.bouncy-castle-fips-adapter" preserve="all"/>
<assembly fullname="BouncyCastle.Cryptography" preserve="all"/>
<assembly fullname="Microsoft.Extensions.DependencyInjection" preserve="all"/>
<assembly fullname="Microsoft.Extensions.DependencyInjection.Abstractions" preserve="all"/>
<assembly fullname="Microsoft.Extensions.Logging" preserve="all"/>
<assembly fullname="Microsoft.Extensions.Logging.Abstractions" preserve="all"/>
<assembly fullname="Microsoft.Extensions.Options" preserve="all"/>
<assembly fullname="Microsoft.Extensions.Primitives" preserve="all"/>
<assembly fullname="Microsoft.Bcl.AsyncInterfaces" preserve="all"/>
<assembly fullname="System.Text.Json" preserve="all"/>
<assembly fullname="System.Text.Encodings.Web" preserve="all"/>
<assembly fullname="System.Runtime.Loader" preserve="all"/>
<assembly fullname="System.Reflection.TypeExtensions" preserve="all"/>

Comments on this question (0)

Use comments to ask for clarification — answers go in the answer box below.

Log in to comment on this question.

0

Can you specify what version of you are working with?
Also what setup are you using in regard to android deployment, if you are using AOT you probably need to tell AOT about the reflection classes being used.

You probably will have to use an: ILLink.Descriptors.xml (place it in the root of the project folder /next to the csproj file)

<linker>
  <assembly fullname="itext.font_asian" preserve="all" />
  <assembly fullname="itext.hyph" preserve="all" />
  <assembly fullname="itext.kernel">
    <type fullname="iText.Kernel.Utils.RegisterDefaultDiContainer" preserve="all" />
  </assembly>
  <assembly fullname="itext.forms">
    <type fullname="iText.Forms.Util.RegisterDefaultDiContainer" preserve="all" />
  </assembly>
</linker>

You can add it to your .csproj like this:
Some sample csproj file

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <RootNamespace>aot_cli_sample</RootNamespace>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <PublishAot>true</PublishAot>
        <InvariantGlobalization>true</InvariantGlobalization>
    </PropertyGroup>
    
    <ItemGroup>
        <PackageReference Include="itext" Version="9.7.0-SNAPSHOT" />
        <PackageReference Include="itext.commons" Version="9.7.0-SNAPSHOT" />
        <PackageReference Include="itext.font-asian" Version="9.7.0-SNAPSHOT" />
        <PackageReference Include="itext.hyph" Version="9.7.0-SNAPSHOT" />
        <PackageReference Include="itext.bouncy-castle-adapter" Version="9.7.0-SNAPSHOT" />
    </ItemGroup>
    <ItemGroup>
        <EmbeddedResource Include="ILLink.Descriptors.xml">
            <LogicalName>ILLink.Descriptors.xml</LogicalName>
        </EmbeddedResource>
    </ItemGroup>
</Project>

If that does not work (it should) you can always try to adding the manual dependency as well as well.


DocumentProperties documentProperties = new DocumentProperties();
documentProperties.RegisterDependency(typeof(IPageTreeListFactory), new DefaultPageTreeListFactory(50000));
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()), documentProperties);

But take into account this is not the only dependency that get's registered automatically so the ILink xml file is the better option because otherwise you might have to add a few other things as well.

Quinn Reed · 0 rep · 8 hours ago

Your answer