10007 : Multi page print

This sample uses VB6 code to show how to call the PrintToHdc method and also react to OnPrintToHdcNewPage event that will allow you to divide your print over as pages that are needed. OnPrintToHdcNewPage will also help you to skip pages and only render a page with a certain page number.

 

Private Sub CommandPrint_Click()
    Dim lngWidthReturned As Long
    Dim lngHeightReturned As Long
    Dim dblXscale As Double
    Dim lngTitleHeight As Long
    Dim lngPicWidth As Long
   
    gUsingMultiPagePrint = True
   
    With phGantX1
        dblXscale = 3.5
    
    
        lngPicWidth = picPreview.Width \ Screen.TwipsPerPixelX
       
        Printer.Print “Anything really”  ‘ must init printer somehow, ideal is to call StartDoc, but print will also suffice
        Call .PrintToHdc(Printer.hDC, 1, lngTitleHeight, dblXscale, dblXscale, .TreeWidth, _
                            .TopItemTree, -1, False, .Start, .Stop, .GetScaleLen, .ScalerHeight, _
                            lngWidthReturned, lngHeightReturned)
          
    End With
    Printer.EndDoc
   

End Sub

Private Sub phGantX1_OnPrintToHdcNewPage(ByVal theGant As phGantXControl.IphGantX3, ByVal aPageNo As Long, aPageHeight As Long, aPageWidth As Long, goahead As Boolean, render As Boolean)
    If gUsingMultiPagePrint Then
        If aPageNo > 1 Then
            Printer.NewPage
        End If
        aPageHeight = 1000 ‘pixels, you will probably want to find out the paper size and use it here
        aPageWidth = 1000 ‘pixels
        goahead = True   ‘ true, continue , false will end this print-run
        render = True    ‘ true=this page will be drawn, false=this page will not be drawn (good for print of page x)
        
    Else
        aPageHeight = -1 ‘no limit
        aPageWidth = -1 ‘no limit
        goahead = True
        render = True
    End If
   
End Sub

————————————–

We got a question on how to go about adapting the print scaling in runtime so that you can use as much of the paper as possible

This is the way I did it when having more or less the same problem, my problem was only the grid but tweaking this approach you can use it for the Gantt:

    private void CustDocPrintDoc_PrintPage(object sender, PrintPageEventArgs e)
    {
          
      Rectangle r=new Rectangle(e.MarginBounds.Location,e.MarginBounds.Size);
      
      // here is check if my columns width are larger that the paper (or preview) width
      float
neededscaling=((float)e.MarginBounds.Width)/((float)Math.Max(3000,grid.Columns.SumWidth()));       if (neededscaling<1 && neededscaling>0) { // Then I scale it... e.Graphics.ScaleTransform(neededscaling,neededscaling); // ...and since everything got smaller, I can print a bigger Rectangle...
r= new Rectangle((int)(r.X/neededscaling),(int)(r.Y/neededscaling),(int)(r.Width/neededscaling),(int)(r.Height/neededscaling)); }
grid.PrintPage(e.Graphics,r,ref morep); e.HasMorePages=morep; }

 

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();
    }