Reputation: 114
I've been using MigraDoc to generate PDFs.
I've got the layout of everything working great EXCEPT for adding in images.
For the part of the program we're generating these PDFs for we're using a program that only saves their information as XML and from there am able to convert the XML into System.Drawing.Image objects.
The problem I'm running into is somehow getting the System.Drawing.Image objects now into my PDFs.
Upvotes: 3
Views: 6668
Reputation: 233
I don't know where this solution started working, but I usually solve this problem with simple method to convert stream into virtual file path.
internal static string LoadImageFromBytes(byte[] cData)
{
return $"base64:{Convert.ToBase64String(cData)}";
}
And then use it in AddImage()
method:
row[0].AddImage(LoadImageFromBytes(byteArray))
Upvotes: 2
Reputation: 21689
MigraDoc was designed to work with filenames. So one option is saving the stream to a temporary file and then use the filename.
Or modify the MigraDoc source code to work with image objects - such a patch can be found on the PDFsharp forum.
BTW: The downvote is not from me. Maybe someone thinks that very little effort was used to post the question here.
Update (January 16, 2016): With version 1.50 (currently available as beta version only, but it is pretty stable) you can pass images in the filename: the filename will contain the image bytes using BASE64 encoding.
It's a bit of a hack, but should work for all scenarios.
Sample code here:
http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
So MigraDoc still uses filenames only, but it is no longer necessary to save images in temporary files.
Upvotes: 4