简体   繁体   中英

Could not load file or assembly PDFsharp

It's a long error, but I will give you the key details. So let's say I released a C# application that includes PDFsharp library and I have got few functions using them and that's where I get the error.

I try to create multiple PDFs (through Crystal Report and export them as PDF) and later on merge those multiples to one. No error yet. While merging I am using PDFsharp functions, I will include the code below to clarify, here's the tricky part, the code works fab, merges PDFs and does everything but only on my PC, I tried to give the application (.exe) to someone else and made them run on their PC, he came back with an error on the merge function.

Also, just an FYI: the PDFs are getting created only merging is an issue, meaning there's no way the program isn't able to find the files it needs to merge, it's looking for some PDFsharp libraries which it can't find. Now, the error holds no meaning because according to my knowledge, when an application is released, it binds all the libraries in itself so that other users don't have to include them. I am also including other libraries, which do not give any error so we may conclude that something's up with PDFsharp.

Error: System.IO.FileNotFoundException: Could not load file or assembly 'PdfSharp, Version=1.32.3057.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb' or one of its dependencies.

Another FYI: I have logged all errors and tried Assembly bindings, IIS application pools, release on different CPUs and nearly everything, so please come up with something working!

Here's my merge code (it's taken from some other source):

using (PdfDocument targetDoc = new PdfDocument())
{
    foreach (string pdf in pdfs)
    {
        using (PdfDocument pdfDoc = PdfSharp.Pdf.IO.PdfReader.Open(pdf, 
               PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import))
        {
            for (int i = 0; i < pdfDoc.PageCount; i++)
            {
                targetDoc.AddPage(pdfDoc.Pages[i]);
            }
        }
    }
    targetDoc.Save(targetPath);
}

You have to ship the file PdfSharp.dll along with your .EXE file (and all assemblies references by PdfSharp.dll). Your .EXE does not include code from other assemblies you reference at compile-time, so these other assemblies must be present at run-time.

BTW: You are using version 1.32 from around 2013 and so you are missing all the bug fixes from the last five years.

Now, the error holds no meaning because according to my knowledge, when an application is released, it binds all the libraries in itself so that other users don't have to include them.

That's new to me. Any references for that information? I assume the error message is correct and your assumption is wrong ...
What is true for LIB libraries for C/C++ is not true for Windows DLL files. DLLs are loaded at run-time and therefore they must be present on the computer running the application.

I had the same issue found an answer on github and it helped

in short:

HtmlRenderer.PdfSharp v1.5.0.6 is loaded as HtmlRenderer.Core v1.5.0.5. Update HtmlRenderer.Core version to v1.5.0.6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM