Using code below to drap various bitmaps inside pdf cell.
public void AddImage(Stream stream, float? width, float? height)
{
var bitmap = new PdfBitmap(stream);
float h = height ?? bitmap.Height;
h += this.cell.Style.CellPadding.Top + this.cell.Style.CellPadding.Bottom;
this.row.Height = Math.Max(this.row.Height, h);
float w = width ?? bitmap.Width;
w += this.cell.Style.CellPadding.Left + this.cell.Style.CellPadding.Right;
void BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
if (args.CellIndex == this.parentColumn.Index && args.Style.BackgroundImage != null)
{
PdfImage backgroundOriginalImage = args.Style.BackgroundImage;
// Calculate the new image dimensions
float newWidth, newHeight;
//Calculate Aspect ratio of image.
float radiusX = (float)(args.Bounds.Width / backgroundOriginalImage.Width);
float radiusY = (float)(args.Bounds.Height / backgroundOriginalImage.Height);
float r = Math.Min(radiusX, radiusY);
newWidth = backgroundOriginalImage.Width * r;
newHeight = backgroundOriginalImage.Height * r;
//Draw image in center of the cell.
float centerX = args.Bounds.X + (args.Bounds.Width - newWidth) / 2;
float centerY = args.Bounds.Y + (args.Bounds.Height - newHeight) / 2;
args.Graphics.DrawImage(backgroundOriginalImage, new RectangleF(centerX, centerY, w, h));
args.Style.BackgroundImage = null;
}
this.grid.BeginCellLayout += BeginCellLayout;
this.cell.Style.BackgroundImage = bitmap;
}
Fields row, grid and cell are stored within class:
private readonly PdfGridRow row;
private readonly PdfGrid grid;
private readonly PdfGridCell cell;
When drawing main grid with
var layoutFormat = new PdfLayoutFormat { Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate };
this.rootGrid.Draw(this.page, new PointF(0,0), layoutFormat);
this.document.Save(stream);
this.document.Close(true);
, exception is thrown:
-----<<Exception Message>>-----
Collection was modified; enumeration operation may not execute.
-----<<Exception Source>>-----
mscorlib
-----<<Exception Stack Trace>>-----
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(PdfLayoutParams param)
at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(PdfLayoutParams param)
at Syncfusion.Pdf.Grid.PdfGridCell.Draw(PdfGraphics graphics, RectangleF bounds, Boolean cancelSubsequentSpans)
at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(RowLayoutResult& result, PdfGridRow row, Single height)
at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(PdfGridRow row)
at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(PdfLayoutParams param)
at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(PdfLayoutParams param)
at Syncfusion.Pdf.Grid.PdfGridCell.Draw(PdfGraphics graphics, RectangleF bounds, Boolean cancelSubsequentSpans)
at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(RowLayoutResult& result, PdfGridRow row, Single height)
at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(PdfGridRow row)
at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(PdfLayoutParams param)
at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(PdfLayoutParams param)
at Syncfusion.Pdf.Grid.PdfGridCell.Draw(PdfGraphics graphics, RectangleF bounds, Boolean cancelSubsequentSpans)
at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRowWithBreak(RowLayoutResult& result, PdfGridRow row, Single height)
at Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(PdfGridRow row)
at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(PdfLayoutParams param)
at Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(PdfLayoutParams param)
at Syncfusion.Pdf.Grid.PdfGrid.Layout(PdfLayoutParams param)
at Syncfusion.Pdf.Graphics.PdfLayoutElement.Draw(PdfPage page, RectangleF layoutRectangle, PdfLayoutFormat format)
at Syncfusion.Pdf.Graphics.PdfLayoutElement.Draw(PdfPage page, PointF location, PdfLayoutFormat format)
Thank you for your input.
Actually problem went away after implementing text wrapping, but not sure whether it's in other cells of a PdfGrid or cells that also contain images:
cell.Value = text;
UpdateRowHeight((string)this.cell.Value, this.cell.Style.Font);
private void UpdateRowHeight(string text, PdfFont font)
{
font = font ?? new PdfStandardFont(PdfFontFamily.Helvetica, 8f); // Latter is the default pdf font.
//measure the string height
SizeF size = font.MeasureString(text);
// find width
float columnWidth = 0;
for (int i = this.parentColumn.Index; i < this.parentColumn.Index + this.ColSpan; i++)
{
columnWidth += this.grid.Columns[i].Width;
}
//divide the text width and column width
float lineCount = (int)Math.Ceiling(size.Width / columnWidth + 1);
float heightNeeded = (size.Height + 1) * lineCount;
if (this.row.Height < heightNeeded)
{
this.row.Height = heightNeeded;
}
}