PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Nov 30, 2020 2:05 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Oct 01, 2020 8:58 pm 
Offline

Joined: Thu Jul 30, 2020 6:08 pm
Posts: 7
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 190 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!


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 02, 2020 7:21 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 593
Location: CCAA
-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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 02, 2020 2:37 pm 
Offline

Joined: Thu Jul 30, 2020 6:08 pm
Posts: 7
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?


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 02, 2020 11:13 pm 
Offline

Joined: Thu Jul 30, 2020 6:08 pm
Posts: 7
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!


Last edited by -Mirage- on Sat Oct 03, 2020 1:04 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 02, 2020 11:14 pm 
Offline

Joined: Thu Jul 30, 2020 6:08 pm
Posts: 7
I tried changing USERPROFILE to USERNAME but still did not work.


Top
 Profile  
Reply with quote  
PostPosted: Sat Oct 03, 2020 1:03 am 
Offline

Joined: Thu Jul 30, 2020 6:08 pm
Posts: 7
Anyways, I now have it going, thanks!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group