c# – 生成PDF时未找到图像

我正在尝试使用XSLT将图像放入PDF中.我有已经显示的图像,但在这种情况下,我最初有字节码,我只是生成base64字符串.但是在下面的例子中,我正在从资源文件中读取图像,然后尝试将其更改为base64,但在这种情况下,它会给我以下错误:

路径异常:this.imageFilePath(前100个字符):base64:iVBORw0KGgoAAAANSUhEUgAAAFoAAABNCAYAAAAme3MhAAAABGdBTUEAALGPC / xhBQAAHrFJREFUeF7V3AuwrWVdx / Etq

使用的模板如下:

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);

用于更改图像的代码:

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;
}

我已经检查了base64字符串,它正在生成在线检查的正确图像.

堆栈跟踪:

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.

解决方法:

BASE64图像编码是一个MigraDoc功能,它只适用于MigraDoc 1.50 beta 3及更高版本.

以这种方式编码的文件不能与XSLT或MigraDoc 1.32或更早版本一起使用.

上一篇:c# – MigraDoc页脚位置


下一篇:c# – MigraDoc – imbricated / nested表?