10602 : Multi page print with scaling in GTP.NET

Multi page print with scaling in GTP.NET

If you want to scale your print you can do that very easily by applying a scaleTransform to the Graphics object.

 

This sample looks very much like the other samples for multi page print, but this will use the scale 0.5 and will fit twice as much Gantt rows to a single page.

Using a scaletransform ensures that the fonts used get scaled along with all the other graphics.


    private void buttonPrintWithScaling_Click(object sender, EventArgs e)
    {
      try
      {
        // Assumes the default printer.
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPageWithScaling);
        pd.BeginPrint += new PrintEventHandler(this.pd_BeginPrint);
        pd.EndPrint += new PrintEventHandler(this.pd_EndPrint);
        printPreviewDialog1.Document = pd;
        printPreviewDialog1.ShowDialog(this);

      }
      catch (Exception ex)
      {
        MessageBox.Show("An error occurred while printing", ex.ToString());
      }
    }

    private void pd_PrintPageWithScaling(object sender, PrintPageEventArgs e)
    {
       Rectangle r = new Rectangle(e.MarginBounds.Location, e.MarginBounds.Size);
       float scalingApplied = 0.5F; // Make it half size
       e.Graphics.ScaleTransform(scalingApplied, scalingApplied);
       // If it is half as big we get twice the space -> make the rect bigger
       r = new Rectangle((int)(r.X / scalingApplied), (int)(r.Y / scalingApplied), (int)(r.Width / scalingApplied), (int)(r.Height / scalingApplied));
       
       bool aHasMorePages=false;
       gantt1.PrintPage(e.Graphics, r, gantt1.GridWidth /* just as on screen */, gantt1.DateScalerHeight /* just as on screen */, ref aHasMorePages);
       e.HasMorePages=aHasMorePages;
       
     }

    public void pd_BeginPrint(object sender,   PrintEventArgs e)
    {
        gantt1.PrintInit(null);
    }
    
    public void pd_EndPrint(object sender,PrintEventArgs e)
    {
      gantt1.PrintEnd();
    }

Leave a Reply