PDFsharp can't find image (image not found)
Asked Answered
S

4

10

I am using PDFsharp in an ASP.NET MVC application. I want to add an image but no matter what directory I put it in, it can't seem to find it. I have code like this as I am trying to copy the sample application

 Section section = document.AddSection();
 Image image13 = section.AddImage("../../images/logo.png");

No matter what directory I put this image in, when the PDF gets generated, I see an error on the PDF saying "Image not found"

Has anyone else seen this issue?

Shadrach answered 6/7, 2010 at 2:48 Comment(0)
T
10

It might be looking for a full path?

Try

Image image13 = section.AddImage(Server.MapPath("~/images/logo.png"));
Touchwood answered 6/7, 2010 at 3:6 Comment(1)
I had the same problem. The problem is that when debugging an asp.net mvc, librairies are loaded in remote directories, like C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\34f2a404\3b92b98b\assembly\dl3\f8107a2b\16f99150_1bfcce01 --> the idea of Server.MapPath is great: it will work on both debug and release environments.Prosthetics
C
5

i am using pdfsharp in a asp.net mvc application.

BTW: You are not using PDFsharp, you are using MigraDoc.

MigraDoc searches the images relative to the current directory. ASPX pages are compiled to and are executed from a temporary directory, not from the project directory. Therefore relative paths will not work as expected.

Assembly.CodeBase might help to locate the images; Assembly.Location indicates the temporary directory.

Assembly.CodeBase can be used in code that is shared between ASP.NET and .NET. Server.MapPath can also be used (as suggested by Marko), but it works in ASP.NET only.

Calendula answered 6/7, 2010 at 7:30 Comment(2)
Team - i still can't get this working . . do you know if anyone has successfully done this ?Shadrach
We only used it without MVC so far. But here's a "confession": forum.pdfsharp.net/viewtopic.php?p=3833#p3833Calendula
C
3

The MigraDoc Document object has an ImagePath property that allows you to specify the directories that will be searched for images (separate multiple directories with semikola).

If images can be found relative to the location of the assembly, then Assembly.CodeBase can be used to locate the assembly (as mentioned in my earlier answer).

Calendula answered 8/7, 2010 at 10:8 Comment(0)
C
2

Old thread but might come in handy for someone

This worked for me:

Document doc = new Document();
doc.ImagePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", "");

I then set my image to copy always and added it with its relative path by bin directory in my Web project.

Cyndicyndia answered 22/9, 2014 at 12:5 Comment(1)
I need to add that this works great for webform projects!Jargonize

© 2022 - 2024 — McMap. All rights reserved.