使用 vb.net 打印包含图片框、标签和文本框的超过两页或更多页的可滚动面板

Print scrollable panel which more than two or more pages containing pictureboxes , lables and textboxes using vb.net

您好,尝试打印包含标签、文本框和图片框以及可能超过两页的列表视图的可滚动面板的朋友们。我已经尝试并在线搜索帮助,我可以打印标签和文本框,如何打印列表视图。

Static page As Integer = 1
Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
Static maxPages As Integer = 0

If page = 1 Then
    For Each ctrl As Control In Me.Panel1.Controls
        If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Then
            ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
            If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
        End If
    Next
End If

For Each ctrl As Control In Me.Panel1.Controls
    If CInt(ctrl.Tag) = page Then
        If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
            Dim sf As New System.Drawing.StringFormat
            If TypeOf ctrl Is TextBox Then
                If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                    sf.Alignment = StringAlignment.Far
                Else
                    sf.Alignment = StringAlignment.Near
                End If
            ElseIf TypeOf ctrl Is Label Then
                If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
                    sf.Alignment = StringAlignment.Near
                ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
                    sf.Alignment = StringAlignment.Far
                End If
            End If
            sf.FormatFlags = StringFormatFlags.NoClip
            e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)
        ElseIf TypeOf ctrl Is PictureBox Then
            If Not DirectCast(ctrl, PictureBox).image  is nothing Then
                    e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition))
                End If
        End If
    End If
Next

page += 1
If page > maxPages Then
    e.HasMorePages = False
    page = 1
    maxPages = 0
Else
    e.HasMorePages = True
End If

由于面板上的列表视图不可滚动,我建议通过向现有列表添加一些代码将其捕获为图像。

 Static page As Integer = 1
    Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
    Static maxPages As Integer = 0

    If page = 1 Then
        For Each ctrl As Control In Me.Panel1.Controls
            If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Or TypeOf ctrl is ListView Then
                ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
                If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
            End If
        Next
    End If

    For Each ctrl As Control In Me.Panel1.Controls
        If CInt(ctrl.Tag) = page Then
            If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
                Dim sf As New System.Drawing.StringFormat
                If TypeOf ctrl Is TextBox Then
                    If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                        sf.Alignment = StringAlignment.Far
                    Else
                        sf.Alignment = StringAlignment.Near
                    End If
                ElseIf TypeOf ctrl Is Label Then
                    If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
                        sf.Alignment = StringAlignment.Near
                    ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
                        sf.Alignment = StringAlignment.Far
                    End If
                End If
                sf.FormatFlags = StringFormatFlags.NoClip
                e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)

            ElseIf TypeOf ctrl Is ListView Then
                Dim lv As ListView = DirectCast(ctrl, ListView)
                Dim img As New Bitmap(lv.Width, lv.Height)
                lv.DrawToBitmap(img, New Rectangle(Point.Empty, img.Size))
                e.Graphics.DrawImage(img, New PointF(ctrl.Left, ctrl.Top - startPosition))
            End If
        End If
    Next

    page += 1
    If page > maxPages Then
        e.HasMorePages = False
        page = 1
        maxPages = 0
    Else
        e.HasMorePages = True
    End If