)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to configure the printing to fit the grid in a single page?

Platform: WinForms |
Control: GridControl |
Published Date: May 18, 2016 |
Last Revised Date: May 28, 2019
Tags: printing

 

 

In order to print the entire grid to fit in a single page, we can override the OnPrintPage (System.Drawing.Printing.PrintPageEventArgs ev) method. This can make any number of rows and columns of the grid to be displayed in a single page.

The reason for overriding the OnPrintPage method is to fit the grid boundaries to the default page as a Bitmap image, so that it could be printed in single page.

C#

protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs ev)
{
   ev.HasMorePages = false;
 
   //draw a fullsize bitmap of the grid...
   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);
   this._grid.GridBounds = new Rectangle(0, 0, gridWidth, gridHeight);
   this._grid.DrawGrid(g);
   this._grid.ResetGridBounds();
   g.Dispose();
   Rectangle destRect = ev.MarginBounds;
 
   //use the printer graphics
   g = ev.Graphics;
 
   //draw grid bitmap into the rect on the print page
   System.Drawing.GraphicsUnit gu = System.Drawing.GraphicsUnit.Point;
   RectangleF srcRect = gridBM.GetBounds(ref gu);
 
   //adjust destRect to make sizing proportional
   // you can comment this out this block if you don't want proportional fit
   float srcRatio = srcRect.Width / srcRect.Height;
   float tarRatio = (float)destRect.Width / destRect.Height;
   if (tarRatio < srcRatio)
   {
      destRect.Height = (int)(destRect.Width / srcRatio);
   }
   else
   {
      destRect.Width = (int)(destRect.Height * srcRatio);
   }
   //////////////endof proportional fit//////////////////////////
 
   g.DrawImage(gridBM, destRect, srcRect, gu);
}

 

VB

Protected Overrides Sub OnPrintPage(ByVal ev As
System.Drawing.Printing.PrintPageEventArgs)
   ev.HasMorePages = False
 
   'draw a fullsize bitmap of the grid...
   Dim gridHeight As Integer = _grid.Model.RowHeights.GetTotal(0, _grid.Model.RowCount)
   Dim gridWidth As Integer = _grid.Model.ColWidths.GetTotal(0, _grid.Model.ColCount)
   Dim gridBM As New Bitmap(gridWidth, gridHeight)
   Dim g As Graphics = Graphics.FromImage(gridBM)
   Me._grid.GridBounds = New Rectangle(0, 0, gridWidth, gridHeight)
   Me._grid.DrawGrid(g)
   Me._grid.ResetGridBounds()
   g.Dispose()
   Dim destRect As Rectangle = ev.MarginBounds
 
   'use the printer graphics
   g = ev.Graphics
 
   'draw grid bitmap into the rect on the print page
   Dim gu As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.Point
   Dim srcRect As RectangleF = gridBM.GetBounds(gu)
 
   'adjust destRect to make sizing proportional
   ' you can comment this out this block if you don't want proportional fit
   Dim srcRatio As Single = srcRect.Width \ srcRect.Height
   Dim tarRatio As Single = CSng(destRect.Width) / destRect.Height
   If tarRatio < srcRatio Then
   destRect.Height = CInt(Fix(destRect.Width / srcRatio))
   Else
   destRect.Width = CInt(Fix(destRect.Height * srcRatio))
   End If
   '////////////endof proportional fit//////////////////////////
 
   g.DrawImage(gridBM, destRect, srcRect, gu)
End Sub
 

 

 

Showing print option for GridControl

 

 

 

 

 

Showing print preview page for GridControl

 

Sample Link:

PrintingFitToPage_C#

PrintingFitToPage_VB

 

 

 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon