Insert "http" image in MigraDoc table

3.3k views Asked by At

MigraDoc provides insertion of image when it's in application directory. Example:

Section section = document.AddSection();    
section.AddImage("../../SomeImage.png");

When I add http image path in .AddImage parameter it displays "Image not found." in PDF.

Is there any way to insert http image in PDF generated by MigraDoc?

1

There are 1 answers

0
I liked the old Stack Overflow On BEST ANSWER

No, you cannot insert images via a HTTP reference.

Download the image to a local folder and add this local copy to the MigraDoc document.

When using PDFsharp, you don't need a local file; an Image object can be used instead.

Code for XImage.FromURI can be found here (for PDFsharp):
http://forum.pdfsharp.de/viewtopic.php?p=4851&sid=4898a4ff0b0437cfdbb80bc48bdfed36#p4851

Code that allows MigraDoc do use dynamic images (no temporary local file needed) can be found here:
http://forum.pdfsharp.de/viewtopic.php?p=4123&sid=4898a4ff0b0437cfdbb80bc48bdfed36#p4123

Update:
With PDFsharp 1.50 beta 2 and later it is easy to add images you have in memory (byte[] or such). If you only have an HTTP reference, you still have to download the image yourself. But you no longer need a temporary file or a patched MigraDoc version.
More information on the MigraDoc site

The trick is simple: convert your image to a string and pass that as a filename with the "base64:" prefix.

static string MigraDocFilenameFromByteArray(byte[] image)
{
    return "base64:" +
           Convert.ToBase64String(image);
}