Normally
rootGrid.Draw(page, new PointF(0, 0), layoutFormat);
document.Save(stream);
will create Pdf document with 2 pages.
However if we add a Bitmap image (5000x5000px) inside a grid, application hangs on rootGrid.Draw and keeps creating pages without any bounds.
Breaking into during debugging periodically reveals following callstack, number of pages created after several minutes is > 8000!
mscorlib.dll!System.Collections.Generic.List
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfSection.IndexOf(Syncfusion.Pdf.PdfPage page) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfDocumentPageCollection.IndexOf(Syncfusion.Pdf.PdfPage page) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfDocumentTemplate.IsEven(Syncfusion.Pdf.PdfPage page) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfDocumentTemplate.GetBottom(Syncfusion.Pdf.PdfPage page) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfSection.GetBottomIndentHeight(Syncfusion.Pdf.PdfDocument document, Syncfusion.Pdf.PdfPage page, bool includeMargins) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfSection.GetActualBounds(Syncfusion.Pdf.PdfDocument document, Syncfusion.Pdf.PdfPage page, bool includeMargins) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfSection.GetActualBounds(Syncfusion.Pdf.PdfPage page, bool includeMargins) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfPageLayer.InitializeGraphics(Syncfusion.Pdf.PdfPageBase page) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.PdfPageLayer.Graphics.get() Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.GetNextPage(Syncfusion.Pdf.Graphics.PdfLayoutFormat format) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(Syncfusion.Pdf.Graphics.PdfLayoutParams param) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(Syncfusion.Pdf.Graphics.PdfLayoutParams param) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridCell.Draw(Syncfusion.Pdf.Graphics.PdfGraphics graphics, System.Drawing.RectangleF bounds, bool cancelSubsequentSpans) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRowWithBreak(ref Syncfusion.Pdf.Grid.PdfGridLayouter.RowLayoutResult result, Syncfusion.Pdf.Grid.PdfGridRow row, float height) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(Syncfusion.Pdf.Grid.PdfGridRow row) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(Syncfusion.Pdf.Graphics.PdfLayoutParams param) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(Syncfusion.Pdf.Graphics.PdfLayoutParams param) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridCell.Draw(Syncfusion.Pdf.Graphics.PdfGraphics graphics, System.Drawing.RectangleF bounds, bool cancelSubsequentSpans) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRowWithBreak(ref Syncfusion.Pdf.Grid.PdfGridLayouter.RowLayoutResult result, Syncfusion.Pdf.Grid.PdfGridRow row, float height) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.DrawRow(Syncfusion.Pdf.Grid.PdfGridRow row) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutOnPage(Syncfusion.Pdf.Graphics.PdfLayoutParams param) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGridLayouter.LayoutInternal(Syncfusion.Pdf.Graphics.PdfLayoutParams param) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Grid.PdfGrid.Layout(Syncfusion.Pdf.Graphics.PdfLayoutParams param) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Graphics.PdfLayoutElement.Draw(Syncfusion.Pdf.PdfPage page, System.Drawing.RectangleF layoutRectangle, Syncfusion.Pdf.Graphics.PdfLayoutFormat format) Unknown
Syncfusion.Pdf.Base.dll!Syncfusion.Pdf.Graphics.PdfLayoutElement.Draw(Syncfusion.Pdf.PdfPage page, System.Drawing.PointF location, Syncfusion.Pdf.Graphics.PdfLayoutFormat format) Unknown
at reportProvider.Save()
I can't pinpoint what causes this hang.
We add image to a cell with
public void AddImage(Stream stream, float? width, float? height)
{
var bitmap = new PdfBitmap(stream);
float actualHeight = height ?? bitmap.Height;
actualHeight += this.cell.Style.CellPadding.Top + this.cell.Style.CellPadding.Bottom;
this.row.Height = Math.Max(this.row.Height, actualHeight);
float actualWidth = width ?? bitmap.Width;
actualWidth += this.cell.Style.CellPadding.Left + this.cell.Style.CellPadding.Right;
this.grid.BeginCellLayout += BeginCellLayout;
this.cell.Style.BackgroundImage = bitmap;
// Event handler to reposition and fit image into a cell
void BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
if (args.CellIndex == this.parentColumn.Index && args.Style.BackgroundImage != null)
{
PdfImage backgroundOriginalImage = args.Style.BackgroundImage;
var bWidth = actualWidth - this.cell.Style.CellPadding.Left - this.cell.Style.CellPadding.Right;
var bHeight = actualHeight - this.cell.Style.CellPadding.Top - this.cell.Style.CellPadding.Bottom;
var x = args.Bounds.X + GetPosition(this.cell.Style.CellPadding.Left, this.cell.Style.CellPadding.Right, args.Bounds.Width, bWidth, (int)this.hAlign.GetValueOrDefault(VHorizontalAlignment.Left));
var y = args.Bounds.Y + GetPosition(this.cell.Style.CellPadding.Top, this.cell.Style.CellPadding.Bottom, args.Bounds.Height, bHeight, (int)this.vAlign);
args.Graphics.DrawImage(backgroundOriginalImage, new RectangleF(x, y, bWidth, bHeight));
args.Style.BackgroundImage = null;
}
}
}
Hi umlprog,
We have ensured to draw a grid with test images on the PDF document. It is working properly without any hanging issues. We request to try with the following KB documentation code snippet based on your requirement,
To draw the background picture in PDF table cell: https://www.syncfusion.com/kb/9807/how-to-draw-the-background-picture-in-pdf-table-cell-using-c-and-vb-net
To add image and text in the PDF table cell: https://www.syncfusion.com/kb/9675/how-to-add-image-and-text-in-the-pdf-table-cell-using-c-and-vb-net
To add multiple images in the PDF table cell: https://www.syncfusion.com/kb/9746/how-to-add-two-images-in-one-pdf-table-cell-using-c-and-vb-net
If the above KB documentation code snippet did not resolve your issues, Kindly revert us with the modified sample or complete code snippet with the input image to replicate the reported issue to analyze further and provide a precise solution on this.
Regards,
Surya V
It happened with 5000x5000 test image.