天天看点

vb.net下打印清单示例(连续纸和固定纸张打印)

清单按类别打印

Private Sub print_doc(ByVal sender As Object, ByVal e As PrintPageEventArgs)


        Dim m_arg As New drawArg(m_printArg.m_title_font, Brushes.Black, New Pen(Color.Blue, 2))
        Dim m_arg1 As New drawArg(m_printArg.m_title1_font, Brushes.Black, New Pen(Color.Blue, 1))
        Dim m_arg2 As New drawArg(m_printArg.m_text_font, Brushes.Black, New Pen(Color.Blue, 1))
        Dim m_arg3 As New drawArg(m_printArg.m_footer_font, Brushes.Black, New Pen(Color.Blue, 1))

        'Dim h As Single = Format(m_arg2.m_font.GetHeight(e.Graphics) + m_printArg.row_height.row3 + 0.005, "0.000")  '设置行高

        Dim t_height As Single = m_arg.m_font.GetHeight(e.Graphics) + m_printArg.row_height.row1
        Dim t_height1 As Single = m_arg1.m_font.GetHeight(e.Graphics) + m_printArg.row_height.row2
        Dim m_height As Single = m_arg2.m_font.GetHeight(e.Graphics) + m_printArg.row_height.row3
        Dim f_height As Single = m_arg3.m_font.GetHeight(e.Graphics) + m_printArg.row_height.row4

        Dim strformat As New StringFormat(StringFormat.GenericDefault)'居中
        strformat.LineAlignment = StringAlignment.Center 
        strformat.Alignment = StringAlignment.Center
        strformat.FormatFlags = StringFormatFlags.NoWrap
        Dim strformatleft As New StringFormat(StringFormat.GenericDefault)
        strformatleft.Alignment = StringAlignment.Near
        strformatleft.LineAlignment = StringAlignment.Center
        strformatleft.FormatFlags = StringFormatFlags.NoWrap
        Dim i As Single = printText(e, "*****医院", m_arg, e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, _
       t_height, strformat, New m_Line_Style(False, False, False, False)) <span style="font-family: Arial, Helvetica, sans-serif;">'</span>i返回行高

        i = printText(e, "病人住院费用一日清单", m_arg, e.MarginBounds.Left, i, e.MarginBounds.Width, _
     t_height, strformat, New m_Line_Style(False, False, False, False))


        Dim t_item(3) As m_item
        t_item(0) = New m_item("病人ID:" & dt(0)(2).ToString, 260 * 0.3937008, _
                               strformatleft, New m_Line_Style(False, False, False, False))
        t_item(1) = New m_item("病人姓名:" & dt(0)(12).ToString, 320 * 0.3937008, _
                              strformat, New m_Line_Style(False, False, False, False))
        t_item(2) = New m_item("性别:" & dt(0)(19).ToString, 200 * 0.3937008, _
                                   strformat, New m_Line_Style(False, False, False, False))
        t_item(3) = New m_item("年龄:" & dt(0)(20).ToString, 200 * 0.3937008, _
                                   strformat, New m_Line_Style(False, False, False, False))
        i = printLine(e, m_arg1,
                      t_item, e.MarginBounds.Left, i, t_height1)

        t_item(0) = New m_item("科室:" & dt(0)(4).ToString, 260 * 0.3937008, _
                       strformatleft, New m_Line_Style(False, True, False, False))
        t_item(1) = New m_item("床位:" & dt(0)(5).ToString & "床", 130 * 0.3937008, _
                              strformat, New m_Line_Style(False, True, False, False))
        t_item(2) = New m_item("费别:" & dt(0)(16).ToString, 210 * 0.3937008, _
                                   strformat, New m_Line_Style(False, True, False, False))
        t_item(3) = New m_item("入院日期:" & dt(0)(6).ToString, 405 * 0.3937008, _
                                   strformat, New m_Line_Style(False, True, False, False))
        i = printLine(e, m_arg1,
                      t_item, e.MarginBounds.Left, i, t_height1)





        Dim m_item(5) As m_item
        m_item(0) = New m_item("代码", m_printArg.col_width.col1 * 100 * 0.3937008, _
                              strformatleft, New m_Line_Style(CBool(m_printArg.col_line1.col1), CBool(m_printArg.col_line2.col1),
                                                              CBool(m_printArg.col_line3.col1), CBool(m_printArg.col_line4.col1)))
        m_item(1) = New m_item("项目名称", m_printArg.col_width.col2 * 100 * 0.3937008, _
                                    strformatleft, New m_Line_Style(CBool(m_printArg.col_line1.col2), CBool(m_printArg.col_line2.col2),
                                                              CBool(m_printArg.col_line3.col2), CBool(m_printArg.col_line4.col2)))
        m_item(2) = New m_item("单位", m_printArg.col_width.col3 * 100 * 0.3937008, _
                                    strformat, New m_Line_Style(CBool(m_printArg.col_line1.col3), CBool(m_printArg.col_line2.col3),
                                                              CBool(m_printArg.col_line3.col3), CBool(m_printArg.col_line4.col3)))
        m_item(3) = New m_item("数量", m_printArg.col_width.col4 * 100 * 0.3937008, _
                                    strformat, New m_Line_Style(CBool(m_printArg.col_line1.col4), CBool(m_printArg.col_line2.col4),
                                                              CBool(m_printArg.col_line3.col4), CBool(m_printArg.col_line4.col4)))
        m_item(4) = New m_item("单价", m_printArg.col_width.col5 * 100 * 0.3937008, _
                                    strformat, New m_Line_Style(CBool(m_printArg.col_line1.col5), CBool(m_printArg.col_line2.col5),
                                                              CBool(m_printArg.col_line3.col5), CBool(m_printArg.col_line4.col5)))
        m_item(5) = New m_item("金额", m_printArg.col_width.col6 * 100 * 0.3937008, _
                                           strformat, New m_Line_Style(CBool(m_printArg.col_line1.col6), CBool(m_printArg.col_line2.col6),
                                                              CBool(m_printArg.col_line3.col6), CBool(m_printArg.col_line4.col6)))




        i = printLine(e, m_arg2, m_item, e.MarginBounds.Left, i, m_height)


        If dt.Rows.Count > 0 Then
            Static page As Integer = 0
            Static j As Integer = 0 '项目
            Static k As Integer = 0 '类别
            Static p As Integer = 0 ' 已打印条目数

            Dim col_name(0) As String
            col_name(0) = "class_on_reckoning"

            Dim dt_t As DataTable = New DataView(dt).ToTable(True, col_name)
            Dim dv As DataView = dt.DefaultView

            Dim t_rows_num As Integer = dt.Rows.Count '项目总条数
            Dim c_rows_num As Integer = dt_t.Rows.Count '类别总条数


            Do While k <= dt_t.Rows.Count - 1 '类别循环开始
                dv.RowFilter = "class_on_reckoning='" & dt_t.Rows(k)(0).ToString & "'"
                Dim dt_t1 As DataTable = dv.ToTable()
                If e.MarginBounds.Height + e.MarginBounds.Top - i > m_height Then '类别行高小于可打印行高,打印类别
                    i = printText(e, dt_t.Rows(k)(0).ToString, m_arg2, e.MarginBounds.Left, i, e.MarginBounds.Width,
                                  m_height,
                                  strformatleft,
                                  New m_Line_Style(True, True, False, False))
                    'Debug.Print(i)
                    p = p + 1
                Else
                    page = page + 1
                    printText(e, "第" & page & "页", m_arg3, _
                                 e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height, _
                                  e.MarginBounds.Width, f_height, _
                                 strformat)


                    e.Graphics.DrawLine(m_arg2.m_pen, e.MarginBounds.Left, i, e.MarginBounds.Width + e.MarginBounds.Left, i)

                    '//重设纸张
                    If CBool(m_printArg.m_PaperKind) Then '如果设为链式纸

                        Dim pageHeight As Single
                        'Dim t_height As Single = Format(m_arg.m_font.GetHeight(e.Graphics) + 0.05, "0.00")
                        'Dim t_height1 As Single = Format(m_arg1.m_font.GetHeight(e.Graphics) + 0.05, "0.00")
                        'Dim m_height As Single = Format(h + 0.05, "0.00")

                        pageHeight = t_height * 2 + 2 * t_height1 + (1 + t_rows_num + c_rows_num - p) * m_height + e.PageSettings.Margins.Top + e.PageSettings.Margins.Bottom

                        If pageHeight <= m_printArg.m_PaperHeight * 100 * 0.3937008 Then '当所有打印内容大于预设页高,以预设页高为纸张页高
                            e.PageSettings.PaperSize = New PaperSize("aaa", m_printArg.m_PaperWidth * 100 * 0.3937008, Format(pageHeight + 0.5, "0"))

                        End If
                    End If

                    '/

                    e.HasMorePages = True


                    Exit Sub
                    End If

                    Do
                        If e.MarginBounds.Height + e.MarginBounds.Top - i >= m_height Then
                            m_item(0).item = dt_t1(j)(14).ToString
                            m_item(1).item = dt_t1(j)(13).ToString
                            m_item(2).item = dt_t1(j)(15).ToString
                            m_item(3).item = dt_t1(j)(22).ToString
                            m_item(4).item = String.Format("{0:f2}", dt_t1(j)(21))
                            m_item(5).item = String.Format("{0:f2}", dt_t1(j)(23))
                            i = printLine(e, m_arg2, m_item, e.MarginBounds.Left, i, m_height)
                            'Debug.Print(i)
                            p = p + 1
                            j = j + 1
                            If j = dt_t1.Rows.Count Then '某类别打印完后,跳出循环进入下一类别

                                j = 0
                                'If k = dt_t.Rows.Count - 1 Then '最后一个类别,打印横线封闭表格
                                '    e.Graphics.DrawLine(Pens.Coral, e.MarginBounds.Left, i, e.MarginBounds.Width + e.MarginBounds.Left, i)
                                'End If

                                Exit Do
                            End If

                        Else '当前页不够打印一行,打印页码和最后一个横线
                            page += 1
                            printText(e, "第" & page & "页", m_arg3, _
                                      e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height, _
                                       e.MarginBounds.Width, f_height, _
                                      strformat)
                            If j < dt_t1.Rows.Count Then '如果不是类别的最后一个项目,因为下次还要打类别,要减掉已打印类别占用的个数1
                                p = p - 1
                            End If
                            '不足一页,最后一个项目画上横线
                            e.Graphics.DrawLine(m_arg2.m_pen, e.MarginBounds.Left, i, e.MarginBounds.Width + e.MarginBounds.Left, i)
                            '//重设纸张
                        If CBool(m_printArg.m_PaperKind) Then '如果设为链式纸
                            Dim pageHeight As Single
                      
                            pageHeight = t_height * 2 + 2 * t_height1 + (1 + t_rows_num + c_rows_num - p) * m_height + e.PageSettings.Margins.Top + e.PageSettings.Margins.Bottom

                            If pageHeight <= m_printArg.m_PaperHeight * 100 * 0.3937008 Then '当所有打印内容大于预设页高,以预设页高为纸张页高
                                e.PageSettings.PaperSize = New PaperSize("aaa", m_printArg.m_PaperWidth * 100 * 0.3937008, Format(pageHeight + 0.5, "0"))

                            End If
                        End If

                        '/
                        e.HasMorePages = True

                        Exit Sub
                        End If


                    Loop
                    k = k + 1
            Loop
            page += 1

            printText(e, "第" & page & "页" & "(共" & page & "页)", m_arg3, _
                                e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height, _
                                 e.MarginBounds.Width, f_height, _
                               strformat)
            e.Graphics.DrawLine(Pens.Black, e.MarginBounds.Left, i, e.MarginBounds.Width + e.MarginBounds.Left, i)'最后一个项目封线
            k = 0
            p = 0
            page = 0

        End If


    End Sub