4

I am trying to place image in PDF using XSLT. I have images that are already displayed but in that case I have the bytecode initially and I simply generate the base64 string. However in the below case I am reading image from the resource file and then trying to change it to base64 but in this case it gives me the following error:

Path exception: this.imageFilePath (first 100 chars):base64:iVBORw0KGgoAAAANSUhEUgAAAFoAAABNCAYAAAAme3MhAAAABGdBTUEAALGPC/xhBQAAHrFJREFUeF7V3AuwrWVdx/Etq

The template used is as follows:

string brandLogoXslt = @"<Paragraph Style=""P"">Some text here:</Paragraph>";
       brandLogoXslt += @"<Image><xsl:attribute name=""Filename"">" + MigraDocFilenameFromByteArray(ExtractResource(Business.Properties.Resources.logo)) + "</xsl:attribute></Image>";
       genericPlaceholderNodes[i].ParentNode.InnerXml = genericPlaceholderNodes[i].ParentNode.InnerXml.Replace(genericPlaceholderNodes[i].OuterXml, brandLogoXslt);

Code used for changing the image:

private static string MigraDocFilenameFromByteArray(byte[] image)
{
    return "base64:" + Convert.ToBase64String(image);
}
public static byte[] ExtractResource(System.Drawing.Image image)
{
    MemoryStream ms = new MemoryStream();

    image.Save(ms, ImageFormat.Png);
    if (ms == null)
        return null;
    byte[] imageByteArray = ms.ToArray();
    return imageByteArray;
}

I already check the base64 string and it is generating the correct image checked online.

Stack trace:

bei MigraDoc.Rendering.ImageRenderer.CalculateImageDimensions()
bei MigraDoc.Rendering.ImageRenderer.Format(Area area, FormatInfo  previousFormatInfo)
bei MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
bei MigraDoc.Rendering.FormattedCell.Format(XGraphics gfx)
bei MigraDoc.Rendering.TableRenderer.FormatCells()
bei MigraDoc.Rendering.TableRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
bei MigraDoc.Rendering.TableRenderer.Format(Area area, FormatInfo previousFormatInfo)
bei MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
bei MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
bei MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
bei MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
bei MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
bei MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
bei School.DE.WebApplications.SCHDI.Business.NewPDFGenerator.TemplateToPdfTransformerBase.Transform2Pdf(Object data, XslTransform transform, Stream outStrm) in d:\Projects\Business\NewPDFGenerator\TemplateToPdfTransformerBase.cs:Zeile 70.
Maqsood
  • 369
  • 4
  • 17

1 Answers1

0

The BASE64 encoding of images is a MigraDoc feature and it works with MigraDoc 1.50 beta 3 and later only.

A file encoded this way cannot be used with XSLT or with MigraDoc 1.32 or older.

  • yes i know but as mentioned i am already able to display images using the xslt. the xslt is only for templating the pdf document – Maqsood Nov 28 '16 at 15:03
  • If the exception occurs inside MigraDoc code then provide more information (e.g. stack trace for the exception) - or better provide an MCVE. If the exception occurs outside MigraDoc code then the answer is it won't work. – I liked the old Stack Overflow Nov 28 '16 at 15:07
  • You can edit your question to include the strack trace in a more readable form and remove the comments here. BASE64 for images is a new feature of MigraDoc 1.50. Which version of MigraDoc are you using? – I liked the old Stack Overflow Nov 28 '16 at 15:28
  • yes i know and i have been able to use base64 for images but when i am reading the images from resource file and then converting it to base64 it give than mentioned exception. – Maqsood Nov 28 '16 at 15:39