Error when adding some images to a pdf

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.

Here's the code :

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

Attachment: 20180528_103823_66102a31.zip

1 Reply

SK Sasi Kumar Sekar Syncfusion Team May 31, 2018 04:17 PM UTC

Hi Sebastien, 
 
Thank you for contacting Syncfusion. 
 
Based on the input image you have provided, we have tried to reproduce the issue from our side. But it works fine in our side, we initially suspect that this issue occured when trying to generate the PDF in multi threading environment. But its worked as expected. We have attached a working sample for your reference, can you please try it in your machine and let us know the result. If the issue still occurs. 
kindly share the below details to check the same form our end. 
 
1.     Essential Studio product version. 
2.     .NetFramework 
3.     OS environment and bit details (32 bit or 64 bit) 
  
Please find the sample tried in our side to reproduce the issue. 
Regards, 
Sasi Kumar S. 


Loader.
Up arrow icon