Print to Fit Demo

This sample illustrates how to print a grid on a single page. It derives a GridPrintDocument class to handle the printing of the entire grid on a single page. It does this by drawing the full-size grid to a large bitmap, and then drawing this bitmap, scaled to fit the output page.



Print To Fit screenshot

The following code overrides OnPrintPage in the inherited GridPrintDocument class.

            protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs ev)
            {
                ev.HasMorePages = false;
                int gridHeight = _grid.Model.RowHeights.GetTotal(0, _grid.Model.RowCount);
                int gridWidth = _grid.Model.ColWidths.GetTotal(0, _grid.Model.ColCount);
                Bitmap gridBM = new Bitmap(gridWidth, gridHeight);
                Graphics g = Graphics.FromImage(gridBM);
                Rectangle saveGridRect = this._grid.GridBounds;
                this._grid.GridBounds = new Rectangle(0, 0, gridWidth, gridHeight);
                this._grid.DrawGrid(g);
                this._grid.GridBounds = saveGridRect;
     
                g.Dispose();
                Rectangle destRect = ev.MarginBounds;
         
                g = ev.Graphics;
                System.Drawing.GraphicsUnit gu = System.Drawing.GraphicsUnit.Point;
                RectangleF srcRect = gridBM.GetBounds(ref gu);
                g.DrawImage(gridBM,  destRect, srcRect, gu);
            }  

Features