We are having some problems while adding some images in a pdf.
We were thinking the problem was linked to specific extensions but after doing multiple tests, we did not find any pattern. (some images are displayed correctly while others are causing an exception).
I have uploaded one image causing an exception and the generated stack trace.
var iPagesNb = lDocument.Pages.Count;
var iPage = 0;
foreach (Syncfusion.Pdf.PdfPageBase oPdfPage in lDocument.Pages)
{
var oTemplate = template.GetForPage(iPage, iPagesNb);
iPage++;
PdfGraphics g = oPdfPage.Graphics;
float docHeight = g.ClientSize.Height;
float docWidth = g.ClientSize.Width;
PdfPaddings cellPadding = new PdfPaddings();
cellPadding.All = 3;
// Grouper les Rows Par Alignements
var grpsAlignments = (from drRow in oTemplate.Rows
group drRow by drRow.AlignRow
into drAlign
select
new { Alignement = drAlign.Key, cnt = drAlign.Count(), Rows = drAlign.OrderBy(dr => dr.RowNo).ToList(), TableStartingPoint = drAlign.FirstOrDefault().AlignmentValue }
).ToList();
foreach (var oneGroup in grpsAlignments)
{
PdfGrid pdfGrid = new PdfGrid();
float zeroYPosition = GetPositionTopAlignment(docHeight, oneGroup.Alignement, oTemplate);
float zeroXPosition = oTemplate.MarginLeft;
foreach (var oColumn in oTemplate.Columns.Where(x => x.AlignmentValue == oneGroup.Rows[0].AlignmentValue))
{
PdfGridColumn column = new PdfGridColumn(pdfGrid);
column.Width = oColumn.Width;
pdfGrid.Columns.Add(column);
}
foreach (var oRow in oneGroup.Rows)
{
PdfGridRow row = new PdfGridRow(pdfGrid);
row.Height = oRow.RealHeight;
pdfGrid.Rows.Add(row);
foreach (var oCell in oRow.Cells)
{
var currentFont = Helper.BuildFont(oCell.PdfFontName, oCell.PdfFontSize, oCell.PdfFontStyle);
DTO.Param currentParam = null;
PdfGridCell cell = new PdfGridCell();
if (oCell.Propriete != Enums.ePropertyName.Watermark1)
{
cell = pdfGrid.Rows[oCell.ParentSkinRow.RowNo].Cells[oCell.ParentSkinColumn.ColumnNumber];
cell.StringFormat.WordWrap = PdfWordWrapType.Word;
cell.ColumnSpan = oCell.CellColumnSpan;
cell.Style.CellPadding = cellPadding;
if (!oRow.ShowBorders)
{
cell.Style.Borders.All = PdfPens.Transparent;
}
if (oCell.CellRowSpan != null)
cell.RowSpan = oCell.CellRowSpan.Value;
}
if (oCell.Propriete.HasValue)
{
var propName = oCell.Propriete.Value;
currentParam = Params.FirstOrDefault(dr => dr.Property == propName);
}
switch (oCell.StampType)
{
case Enums.eStamptype.Image:
{
string strHeader = oCell.GetHeader(destinationLanguage);
//var initialColSpan = cell.ColumnSpan;
//var initialRowSpan = cell.RowSpan;
if (!string.IsNullOrEmpty(strHeader))
{
var style = new PdfGridCellStyle();
style.CellPadding = cellPadding;
cell.Value = strHeader;
//cell.ColumnSpan = 1;
//cell.RowSpan = 1;
//Back color cannot be applied to cell that have rowspan or colspan. This is a limitation of Syncfusion.
if (oCell.BackcolorInt != null)
{
style.BackgroundBrush = PdfBrushes.LightGray;
}
cell.Style = style;
}
Image img = null;
if (currentParam != null)
{
if (currentParam.PropertyValue is string)
{
img = Image.FromFile(currentParam.PropertyValue.ToString());
}
else if (currentParam.PropertyValue is System.IO.Stream)
{
img = Bitmap.FromStream((System.IO.Stream)currentParam.PropertyValue);
}
else if (currentParam.PropertyValue is byte[])
{
var strm = new System.IO.MemoryStream((byte[])currentParam.PropertyValue);
img = Bitmap.FromStream(strm);
strm.Dispose();
}
}
if (img != null)
{
PdfBitmap pBmp = new PdfBitmap(img);
if (!string.IsNullOrEmpty(strHeader))
{
PdfGridCell strValueOfHeader = pdfGrid.Rows[oCell.ParentSkinRow.RowNo].Cells[oCell.ParentSkinColumn.ColumnNumber + 1];
strValueOfHeader.ColumnSpan = oCell.ColspanOnValue;
strValueOfHeader.RowSpan = oCell.RowspanOnValue;
strValueOfHeader.Style.BackgroundImage = pBmp;
strValueOfHeader.Value = "";
}
else
{
cell.ColumnSpan = oCell.ColspanOnValue;
cell.RowSpan = oCell.RowspanOnValue;
cell.Style.BackgroundImage = pBmp;
cell.Value = "";
}
}
break;
}
}
}
}
if (oneGroup.TableStartingPoint != 32)
pdfGrid.Draw(oPdfPage.Graphics, new PointF(zeroXPosition, zeroYPosition));
}
}