I've built a WinForms app which uses PDFium to print PDF documents. I installed PDFium from NuGet, and it created two subfolders in my project - x86 and x64 - as expected, each with the relevant version of pdfium.dll inside. My application's target platform is set to Any CPU.
When I run the application in debug on my Windows 10 64-bit machine it works perfectly. However, when I release the application and install it for real on the same computer, PDFium throws an exception:
System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
After doing some research, I changed my application's target platform to x86 and removed the x64 subfolder from my project. The application now works perfectly after release, so the problem is solved.
However, I'd like to know if there's a way that I can get my application to work so that it supports both x86 and x64, using the appropriate version of PDFium for the target computer. It'd be nice if it'd install a 64-bit version on machines which can support it (which is most of them within our organisation, but I feel that I need to go with 32-bit if I have to choose one or the other, to ensure compatibility).