I'd like to print preview and print 3 tab pages from a windows form. Below is my code. I was able to get it to work if it's only one tab. but not 3 tab pages.

Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing

Public Class Results

    Dim bmp As System.Drawing.Bitmap
    Dim bmp1 As System.Drawing.Bitmap
    Dim bmp2 As System.Drawing.Bitmap

 Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click

        PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
        If PrintDialog1.ShowDialog() = DialogResult.OK Then
        End If
    End Sub

    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click


        PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.Document.DefaultPageSettings.Landscape = True

        ' Preview.
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        bmp = New Bitmap(Me.TabControl1.TabPages(0).Width, Me.TabControl1.TabPages(0).Height)
        Me.TabControl1.TabPages(0).DrawToBitmap(bmp, Me.TabControl1.TabPages(0).ClientRectangle)

        bmp1 = New Bitmap(Me.TabControl1.TabPages(1).Width, Me.TabControl1.TabPages(1).Height)
        Me.TabControl1.TabPages(1).DrawToBitmap(bmp1, Me.TabControl1.TabPages(1).ClientRectangle)

        bmp2 = New Bitmap(Me.TabControl1.TabPages(2).Width, Me.TabControl1.TabPages(2).Height)
        Me.TabControl1.TabPages(2).DrawToBitmap(bmp2, Me.TabControl1.TabPages(2).ClientRectangle)

        e.Graphics.DrawImage(bmp, 20, 20)

        e.HasMorePages = True

        e.Graphics.DrawImage(bmp1, 20, 20)

        e.HasMorePages = True

        e.Graphics.DrawImage(bmp2, 20, 20)

        e.HasMorePages = False


    End Sub

End Class

Can anybody tell me what can I do to make it work? also if there's an entirely different way to do this please do share. Thanks

5 Years
Discussion Span
Last Post by guptamanav

Didn't waste the ink testing it but should do what you want.

Problem with your above is you were drawing the images at the same location. 20,20
They were just overlapping.

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        'keep a static index to reference if the process has more pages.
        Static index As Integer

        'Keep track of the Line Y postition to determine if there are more pages
        Dim CurrentYPosition As Integer = e.MarginBounds.Top

        For i = index To TabControl1.TabPages.Count - 1
            'Create a bitmap to the size of the current tab page and draw the
            'current tab page to it.
            Dim TabBitmap As New Bitmap(TabControl1.TabPages(i).Width, TabControl1.TabPages(i).Height)
            TabControl1.TabPages(i).DrawToBitmap(TabBitmap, New Rectangle(Point.Empty, TabBitmap.Size))

            'Draw the tab image at the margin left and the current y position
            e.Graphics.DrawImage(TabBitmap, New Point(e.MarginBounds.X, CurrentYPosition))

            'Add the current tab page height to the line position
            CurrentYPosition += TabBitmap.Height

            'Shit can the tab image.
            TabBitmap = Nothing

            'If the next tab page will not fit on the current page then there
            'are more pages.
            If i < TabControl1.TabPages.Count - 2 AndAlso CurrentYPosition - TabControl1.TabPages(i + 1).Height > e.MarginBounds.Bottom Then
                'the next page won't fit.
                index = i +1
                e.HasMorePages = True
                Exit Sub
            End If


        'All done reset the static index.
        index = 0
    End Sub

Edited by Unhnd_Exception: n/a


Thanks a lot for posting solution.

I though have another issue here. The tab page I have has a scroll bar as the number of controls on the tab page is more than the display area. Now, the requirement is to print all the controls on the tab page even though they are hidden.

Is it possible to achieve this? How will we enhance the code posted in this thread to fix the issue to show hidden controls?

Any help is greatly appriciated.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.