PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Logo Image file not found
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4181
Page 1 of 1

Author:  -Mirage- [ Thu Oct 01, 2020 8:58 pm ]
Post subject:  Logo Image file not found

Hi!
So I have a program that generates a PDF and it's working great!
I'm using c# and MigraDoc to create the document.

However, when I Publish my program and move it to a test computer, instead of my development computer, the PDF loses the logo (image generates with an Image not found error instead).

I've found old (>10 years) questions to similar problems on the internet, but the solutions out there don't seem to work.


I know what the issue is, the Logo file PermitHeader.PNG isn't on my test system, which makes sense.

First of all, my logo is in the Header of the PDF, so the line I'm currently using to create the PDF is (which is working great for testing):

Code:
MigraDoc.DocumentObjectModel.Shapes.Image image = sec.Headers.Primary.AddImage(@"C:\Users\Tech\Desktop\BBB Ready to Upload\BDS\Resources\PermitHeader.PNG");
image.Height = "3.0cm";
image.Width = "16.0cm";
image.LockAspectRatio = true;


One line on Stackoverflow, was this suggestion:

Code:
Image image13 = section.AddImage(Server.MapPath("~/images/logo.png"));


But that didn't play well with the Headercode, and my IDE did not understand Server.MapPath when I tried it without the Header info.



So I went into Visual Studio, and manually included it under 'Resources' in the Publishing info:


Attachment:
publishlogo1.PNG
publishlogo1.PNG [ 15.5 KiB | Viewed 4396 times ]


But that didn't make any difference.



How can I find the logo file and keep it in the header?

So any assistance would be appreciated.

Thank you!

Author:  TH-Soft [ Fri Oct 02, 2020 7:21 am ]
Post subject:  Re: Logo Image file not found

-Mirage- wrote:
One line on Stackoverflow, was this suggestion:

Code:
Image image13 = section.AddImage(Server.MapPath("~/images/logo.png"));
That line works on Web servers where Server.MapPath will be available and will work.

Adding the image as a resource means the image data will be embedded in your assembly, so you still do not have a file.
See also:
http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx

The simple approach is copying the image file along with your assemblies. Then you can simply use the image in your documents.

Author:  -Mirage- [ Fri Oct 02, 2020 2:37 pm ]
Post subject:  Re: Logo Image file not found

Hi, thank you for the reply!

So do you mean creating a directory in like 'AppData' and then copying the logo image into it? And then reference it?

Author:  -Mirage- [ Fri Oct 02, 2020 11:13 pm ]
Post subject:  Re: Logo Image file not found

Ok,
So I've now embedded the pic I need.
I create a dir under appdata/roaming and I save the pic to it. so that is great. HOwever, I still can't load on the last step.

I have to use %userprofile% to find the pic, but it's still not finding it.

The problem is somewhere in the last line:


Code:
string newfileName = "PermitHeader.PNG";

string finalpath = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "AppData");

string combinesfinalpath = finalpath + @"\" + "Roaming" + @"\" + "Permit" + @"\" + newfileName;
           
MessageBox.Show(combinesfinalpath);   // this confirms the Path is right!


bmp.Save(combinesfinalpath);


MigraDoc.DocumentObjectModel.Shapes.Image image = sec.Headers.Primary.AddImage(@"C:\Users\" + @"%USERPROFILE%" + @"\" + "AppData" + @"\" + "Roaming" + @"\" + "Permit" + @"\" + "PermitHeader.PNG");




Any help would be appreciated!

Author:  -Mirage- [ Fri Oct 02, 2020 11:14 pm ]
Post subject:  Re: Logo Image file not found

I tried changing USERPROFILE to USERNAME but still did not work.

Author:  -Mirage- [ Sat Oct 03, 2020 1:03 am ]
Post subject:  Re: Logo Image file not found

Anyways, I now have it going, thanks!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/