Fixing TypeInitializationException After Upgrading the Adobe PDF Library NuGet Package
Estimated Reading Time: 3 MinutesOverview
After upgrading the Adobe PDF Library (APDFL) NuGet package in a .NET application, you may see this error:
The type initializer for 'Datalogics.PDFL.PDFLPINVOKE' threw an exception.
This message is the outer exception. The more useful information is usually found in the InnerException.
In this situation, the problem generally means that the application could not load one of the native APDFL libraries or one of its dependencies. This can happen when older DLLs remain in the build output, the managed and native libraries come from different APDFL releases, or the application's target architecture does not match the native libraries.
What's Going On?
This problem is related to the application's NuGet packages and runtime environment, not to a particular PDF input file.
After a NuGet upgrade, your application may have:
- Older APDFL DLLs remaining in the
binorobjfolders. - Managed and native APDFL libraries from different package releases.
- Native package assets that were not copied to the output folder.
- An x86/x64 mismatch between the application and the native libraries.
- A missing Microsoft Visual C++ runtime dependency.
The native APDFL DLL may appear in the output folder and still fail to load if one of its own dependencies is missing or incompatible.
Symptoms
You may see one or more of the following:
- The application worked before the APDFL NuGet package was upgraded.
- The application fails during APDFL initialization.
- A
System.TypeInitializationExceptionreferencesDatalogics.PDFL.PDFLPINVOKE. - The inner exception reports that a DLL or one of its dependencies could not be found.
- The required DLL appears to be present but still cannot be loaded.
- The inner exception reports an incorrect format or architecture mismatch.
Things to Check
1. Read the Inner Exception
Start by reviewing the complete exception, including every InnerException.
The outer TypeInitializationException only tells you that APDFL could not initialize. The innermost exception usually provides the real clue, such as a missing DLL, an unavailable dependency, or an architecture mismatch.
2. Clean and Restore the NuGet Packages
Old files can remain in the build output after a package upgrade.
Try the following:
- Close the application.
- Delete the project's
binandobjfolders. - Restore the NuGet packages.
- Rebuild the application.
This helps make sure the output folder contains files from the currently selected APDFL package.
3. Check for Old or Manually Referenced DLLs
Make sure the project references only the intended APDFL NuGet package version.
Remove any older APDFL DLLs that were added as direct file references or copied into the project manually. The managed Datalogics.PDFL.dll and the native APDFL libraries must come from the same package release.
4. Check the Application Architecture
Confirm that the application's platform target matches the native APDFL libraries:
- Use x86 native libraries with a 32-bit application.
- Use x64 native libraries with a 64-bit application.
An architecture mismatch may appear as a BadImageFormatException or another DLL-loading error inside the exception chain.
5. Check the Build Output
Verify that the required APDFL native libraries were copied to the application's output or publish directory.
The application must be able to locate these files at runtime. Also remember that a native DLL can be present but fail to load because one of its dependencies is missing.
6. Check the Microsoft Visual C++ Runtime
If the inner exception indicates that a Microsoft runtime dependency is missing, install the latest supported Microsoft Visual C++ Redistributable for the application's architecture.
Microsoft provides separate installers for x86 and x64 applications. Use the package that matches the application and its native APDFL libraries.
For the current supported downloads, see Microsoft Visual C++ Redistributable downloads.
Avoid downloading individual runtime DLLs from third-party websites.
Still Having Trouble?
If the error continues, send the following information to tech_support@datalogics.com:
- The complete exception and all inner exceptions.
- The APDFL NuGet package version.
- The application's .NET target framework.
- The project's x86 or x64 platform target.
- A list of the APDFL DLLs in the application's output directory.
- Whether older APDFL DLLs were previously referenced or deployed manually.
For additional APDFL documentation, visit docs.datalogics.com.