VB.net Printing

ROB MCCONNELL
ROB MCCONNELL used Ask the Experts™
on
I know this will be simple but I cant seem to get there without help.


I have a VB.net project and I want to print onto multiple pages.  Could someone please take a look at the attached code a tell where I'm going wrong.

Private Function PreparePrintDocumentVATDetail() As PrintDocument
        ' Make the PrintDocument object.
        Dim print_document As New PrintDocument

        ' Install the PrintPage event handler.
        AddHandler print_document.PrintPage, AddressOf Print_PrintPageVATDetail

        ' Return the object.
        Return print_document
    End Function

    Private Sub Print_PrintPageVATDetail(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        Dim bottomMargin As Integer = Me.PrintDocument1.DefaultPageSettings.Margins.Bottom / 2
        Dim topMargin As Integer = Me.PrintDocument1.DefaultPageSettings.Margins.Top / 2
        Dim leftMargin As Integer = Me.PrintDocument1.DefaultPageSettings.Margins.Left
        Dim rightMargin As Integer = Me.PrintDocument1.DefaultPageSettings.Margins.Right

        Dim pageHeight As Integer = Me.PrintDocument1.DefaultPageSettings.Bounds.Height - topMargin - bottomMargin
        Dim pagewidth As Integer = Me.PrintDocument1.DefaultPageSettings.Bounds.Width - leftMargin - rightMargin
        Dim fontHeight As Integer = Me.lstCustCode.Font.Height
        Dim itemCount As Integer = pageHeight / fontHeight
        Dim x As Integer = leftMargin + 10
        Dim y As Integer = topMargin


        Dim Llength As Integer = 1123
        Dim lwidth As Integer = 794

        Dim F18 As New Font("Trebuchet", 18)
        Dim F16 As New Font("Trebuchet", 16)
        Dim F14 As New Font("Trebuchet", 14)
        Dim F12 As New Font("Trebuchet", 12)
        Dim F10 As New Font("Trebuchet", 10, FontStyle.Bold)
        Dim F11B As New Font("Trebuchet", 11, FontStyle.Bold)
        Dim Font8 As New Font("Trebuchet", 8)
        Dim F8 As New Font("Trebuchet", 8)
        Dim F7 As New Font("Trebuchet", 7)
        Dim F8B As New Font("Trebuchet", 8, FontStyle.Bold)
        Dim F8U As New Font("Trebuchet", 8, FontStyle.Underline)
        Dim F9U As New Font("Trebuchet", 9, FontStyle.Underline)
        Dim F9B As New Font("Trebuchet", 9, FontStyle.Bold)
        Dim F9 As New Font("Trebuchet", 9)


        Dim RH1 As Integer = 20
        Dim RH2 As Integer = 40
        Dim RH3 As Integer = 60
        Dim RH4 As Integer = 80
        Dim RH5 As Integer = 100
        Dim Row1 As Integer = 140
        Dim Row2 As Integer = 190
        Dim Row3 As Integer = 243
        Dim Row4 As Integer = 295
        Dim Row5 As Integer = 340
        Dim Row6 As Integer = 490
        Dim Row7 As Integer = 510
        Dim Row8 As Integer = 540
        Dim Row9 As Integer = 570
        Dim Row10 As Integer = 600
        Dim Row11 As Integer = 630
        Dim Row12 As Integer = 660
        Dim Row13 As Integer = 690
        Dim Row14 As Integer = 720
        Dim Row15 As Integer = 750
        Dim Row16 As Integer = 780
        Dim Row17 As Integer = 810
        Dim Row18 As Integer = 840
        Dim Row19 As Integer = 870
        Dim Row20 As Integer = 900
        Dim Row21 As Integer = 930
        Dim Row22 As Integer = 965
        Dim Row23 As Integer = 1000
        Dim Row24 As Integer = 1020
        Dim Row25 As Integer = 1040
        'Dim Row26 As Integer = 1060
        'Dim Row27 As Integer = 1080
        Dim Row28 As Integer = 1100
        'Dim Row29 As Integer = 1120
        'Dim Row30 As Integer = 1140

        Dim Buffer As Integer = 10

        'B1 Row Levels
        Dim BRow1 As Integer = 170
        Dim BRow2 As Integer = BRow1 + Font.Height + Buffer
        Dim BRow3 As Integer = BRow2 + Font.Height + Buffer
        Dim BRow4 As Integer = BRow3 + Font.Height + Buffer
        Dim BRow5 As Integer = BRow4 + Font.Height + Buffer
        Dim BRow6 As Integer = BRow5 + Font.Height + Buffer
        Dim BRow7 As Integer = BRow6 + Font.Height + Buffer
        Dim BRow8 As Integer = BRow7 + Font.Height + Buffer
        Dim BRow9 As Integer = BRow8 + Font.Height + Buffer
        Dim BRow10 As Integer = BRow9 + Font.Height + Buffer
        Dim BRow11 As Integer = BRow10 + Font.Height + Buffer
        Dim BRow12 As Integer = BRow11 + Font.Height + Buffer
        Dim BRow13 As Integer = BRow12 + Font.Height + Buffer
        Dim BRow14 As Integer = BRow13 + Font.Height + Buffer
        Dim BRow15 As Integer = BRow14 + Font.Height + Buffer
        Dim BRow16 As Integer = BRow15 + Font.Height + Buffer
        Dim BRow17 As Integer = BRow16 + Font.Height + Buffer
        Dim BRow18 As Integer = BRow17 + Font.Height + Buffer
        Dim BRow19 As Integer = BRow18 + Font.Height + Buffer
        Dim BRow20 As Integer = BRow19 + Font.Height + Buffer
        Dim BRow21 As Integer = BRow20 + Font.Height + Buffer
        Dim BRow22 As Integer = BRow21 + Font.Height + Buffer
        Dim BRow23 As Integer = BRow22 + Font.Height + Buffer
        Dim BRow24 As Integer = BRow23 + Font.Height + Buffer
        Dim BRow25 As Integer = BRow24 + Font.Height + Buffer
        Dim BRow26 As Integer = BRow25 + Font.Height + Buffer
        Dim BRow27 As Integer = BRow26 + Font.Height + Buffer
        Dim BRow28 As Integer = BRow27 + Font.Height + Buffer
        Dim BRow29 As Integer = BRow28 + Font.Height + Buffer
        Dim BRow30 As Integer = BRow29 + Font.Height + Buffer

        Dim COLUMNX As Integer = 100
        Dim columnW As Integer = 480
        Dim Alter As Integer = 75
        'Columns
        Dim C1 As Integer = 15                            'Rate %
        Dim C2 As Integer = C1 + Alter - 25               'Batch
        Dim C3 As Integer = C2 + Alter - 35               'Trn No
        Dim C4 As Integer = C3 + Alter - 25               'Date
        Dim C5 As Integer = C4 + Alter - 5                'Ref
        Dim C6 As Integer = C5 + Alter - 45               'Type
        Dim C7 As Integer = C6 + Alter - 35               'Nom A/C
        Dim C8 As Integer = C7 + Alter - 20               'Narrative
        Dim C9 As Integer = C8 + Alter + 210              'Gross
        Dim C10 As Integer = C9 + Alter - 20              'VAT
        Dim C11 As Integer = C10 + Alter - 20             'Net

        Dim Col8 As Decimal
        Dim Col9 As Decimal
        Dim Col10 As Decimal


        Dim OleDbConn As OleDbConnection = New OleDbConnection(ConnstringSQL)
        Dim MyOledbCommand As OleDbCommand = New OleDbCommand()
        Dim dr As OleDbDataReader
        Try
            'SINVB1
            e.Graphics.DrawString("Sales Invoice", F9B, Brushes.Black, C1, Row1 - Font.Height)
            e.Graphics.DrawString("Rate %", F9U, Brushes.Black, C1, Row1)
            e.Graphics.DrawString("Batch", F9U, Brushes.Black, C2, Row1)
            e.Graphics.DrawString("Trn No", F9U, Brushes.Black, C3, Row1)
            e.Graphics.DrawString("Date", F9U, Brushes.Black, C4, Row1)
            e.Graphics.DrawString("Ref", F9U, Brushes.Black, C5, Row1)
            e.Graphics.DrawString("Type", F9U, Brushes.Black, C6, Row1)
            e.Graphics.DrawString("Nom A/C", F9U, Brushes.Black, C7, Row1)
            e.Graphics.DrawString("Narrative", F9U, Brushes.Black, C8, Row1)
            e.Graphics.DrawString("Gross", F9U, Brushes.Black, C9, Row1)
            e.Graphics.DrawString("VAT", F9U, Brushes.Black, C10, Row1)
            e.Graphics.DrawString("Net", F9U, Brushes.Black, C11, Row1)

            'opening the sql connection
            OleDbConn.Open()
            MyOledbCommand.Connection = (OleDbConn)
            MyOledbCommand.CommandText = "SELECT VatRate, VatBat, VATTrn, VatDate, VatRef, VatType, VatNom, VatNarr, VatGross, Vat, VatNet FROM dbo.VatDetRpt where VatType = 'SINV' or VatType = 'BIS'"
            MyOledbCommand.Connection = OleDbConn
            MyOledbCommand.ExecuteNonQuery()
            dr = MyOledbCommand.ExecuteReader()
            Dim PRow As Integer = 170
            Do While dr.Read = True
                If PRow >= pageHeight - bottomMargin Then
                    e.HasMorePages = True
                    dlgPrintPreview.Document = PreparePrintDocumentVATDetail()
                    PRow = 170
                Else
                    Col8 = dr.GetValue(8)
                    Col9 = dr.GetValue(9)
                    Col10 = dr.GetValue(10)

                    Col8.ToString("N2")
                    Col9.ToString("N2")
                    Col10.ToString("N2")

                    e.Graphics.DrawString(dr.GetValue(0), F8, Brushes.Black, C1, PRow)
                    e.Graphics.DrawString(dr.GetValue(1), F8, Brushes.Black, C2, PRow)
                    e.Graphics.DrawString(dr.GetValue(2), F8, Brushes.Black, C3, PRow)
                    e.Graphics.DrawString(dr.GetValue(3), F8, Brushes.Black, C4, PRow)
                    e.Graphics.DrawString(dr.GetValue(4), F8, Brushes.Black, C5, PRow)
                    e.Graphics.DrawString(dr.GetValue(5), F8, Brushes.Black, C6, PRow)
                    e.Graphics.DrawString(dr.GetValue(6), F8, Brushes.Black, C7, PRow)
                    e.Graphics.DrawString(dr.GetValue(7), F8, Brushes.Black, C8, PRow)
                    e.Graphics.DrawString(Col8.ToString("N2"), F8, Brushes.Black, C9, PRow)
                    e.Graphics.DrawString(Col9.ToString("N2"), F8, Brushes.Black, C10, PRow)
                    e.Graphics.DrawString(Col10.ToString("N2"), F8, Brushes.Black, C11, PRow)
                    PRow = PRow + Font.Height + Buffer
                End If
            Loop
            Dim SIGross As Decimal = lblSIGrossTotal.Text
            Dim SIVAT As Decimal = lblSIVatTotal.Text
            Dim SINet As Decimal = lblSINetTotal.Text

            e.Graphics.DrawString(SIGross.ToString("N2"), F8B, Brushes.Black, C9, PRow)
            e.Graphics.DrawString(SIVAT.ToString("N2"), F8B, Brushes.Black, C10, PRow)
            e.Graphics.DrawString(SINet.ToString("N2"), F8B, Brushes.Black, C11, PRow)

            OleDbConn.Close()
            PRow = PRow + Font.Height + Buffer
            PRow = PRow + Font.Height + Buffer

            'SCNVB1
            e.Graphics.DrawString("Sales Credit Note", F9B, Brushes.Black, C1, PRow - Font.Height)
            e.Graphics.DrawString("Rate %", F9U, Brushes.Black, C1, PRow)
            e.Graphics.DrawString("Batch", F9U, Brushes.Black, C2, PRow)
            e.Graphics.DrawString("Trn No", F9U, Brushes.Black, C3, PRow)
            e.Graphics.DrawString("Date", F9U, Brushes.Black, C4, PRow)
            e.Graphics.DrawString("Ref", F9U, Brushes.Black, C5, PRow)
            e.Graphics.DrawString("Type", F9U, Brushes.Black, C6, PRow)
            e.Graphics.DrawString("Nom A/C", F9U, Brushes.Black, C7, PRow)
            e.Graphics.DrawString("Narrative", F9U, Brushes.Black, C8, PRow)
            e.Graphics.DrawString("Gross", F9U, Brushes.Black, C9, PRow)
            e.Graphics.DrawString("VAT", F9U, Brushes.Black, C10, PRow)
            e.Graphics.DrawString("Net", F9U, Brushes.Black, C11, PRow)
            PRow = PRow + Font.Height + Buffer

            'opening the sql connection
            OleDbConn.Open()
            MyOledbCommand.Connection = (OleDbConn)
            MyOledbCommand.CommandText = "SELECT VatRate, VatBat, VATTrn, VatDate, VatRef, VatType, VatNom, VatNarr, VatGross, Vat, VatNet FROM dbo.VatDetRpt where VatType = 'SCN' or VatType = 'BCN'"
            MyOledbCommand.Connection = OleDbConn
            MyOledbCommand.ExecuteNonQuery()
            dr = MyOledbCommand.ExecuteReader()

            Do While dr.Read = True
                If PRow >= pageHeight - bottomMargin Then
                    e.HasMorePages = True
                    PRow = 170
                Else
                    Col8 = dr.GetValue(8)
                    Col9 = dr.GetValue(9)
                    Col10 = dr.GetValue(10)

                    Col8.ToString("N2")
                    Col9.ToString("N2")
                    Col10.ToString("N2")

                    e.Graphics.DrawString(dr.GetValue(0), F8, Brushes.Black, C1, PRow)
                    e.Graphics.DrawString(dr.GetValue(1), F8, Brushes.Black, C2, PRow)
                    e.Graphics.DrawString(dr.GetValue(2), F8, Brushes.Black, C3, PRow)
                    e.Graphics.DrawString(dr.GetValue(3), F8, Brushes.Black, C4, PRow)
                    e.Graphics.DrawString(dr.GetValue(4), F8, Brushes.Black, C5, PRow)
                    e.Graphics.DrawString(dr.GetValue(5), F8, Brushes.Black, C6, PRow)
                    e.Graphics.DrawString(dr.GetValue(6), F8, Brushes.Black, C7, PRow)
                    e.Graphics.DrawString(dr.GetValue(7), F8, Brushes.Black, C8, PRow)
                    e.Graphics.DrawString(Col8.ToString("N2"), F8, Brushes.Black, C9, PRow)
                    e.Graphics.DrawString(Col9.ToString("N2"), F8, Brushes.Black, C10, PRow)
                    e.Graphics.DrawString(Col10.ToString("N2"), F8, Brushes.Black, C11, PRow)
                    PRow = PRow + Font.Height + Buffer
                End If
            Loop
            Dim SCNGross As Decimal = lblSCNGrossTotal.Text
            Dim SCNVAT As Decimal = lblSCNVatTotal.Text
            Dim SCNNet As Decimal = lblSCNNetTotal.Text

            e.Graphics.DrawString(SCNGross.ToString("N2"), F8B, Brushes.Black, C9, PRow)
            e.Graphics.DrawString(SCNVAT.ToString("N2"), F8B, Brushes.Black, C10, PRow)
            e.Graphics.DrawString(SCNNet.ToString("N2"), F8B, Brushes.Black, C11, PRow)

            OleDbConn.Close()
            PRow = PRow + Font.Height + Buffer
            PRow = PRow + Font.Height + Buffer
            'SINVB1
            e.Graphics.DrawString("Purchase Invoice", F9B, Brushes.Black, C1, PRow - Font.Height)
            e.Graphics.DrawString("Rate %", F9U, Brushes.Black, C1, PRow)
            e.Graphics.DrawString("Batch", F9U, Brushes.Black, C2, PRow)
            e.Graphics.DrawString("Trn No", F9U, Brushes.Black, C3, PRow)
            e.Graphics.DrawString("Date", F9U, Brushes.Black, C4, PRow)
            e.Graphics.DrawString("Ref", F9U, Brushes.Black, C5, PRow)
            e.Graphics.DrawString("Type", F9U, Brushes.Black, C6, PRow)
            e.Graphics.DrawString("Nom A/C", F9U, Brushes.Black, C7, PRow)
            e.Graphics.DrawString("Narrative", F9U, Brushes.Black, C8, PRow)
            e.Graphics.DrawString("Gross", F9U, Brushes.Black, C9, PRow)
            e.Graphics.DrawString("VAT", F9U, Brushes.Black, C10, PRow)
            e.Graphics.DrawString("Net", F9U, Brushes.Black, C11, PRow)
            PRow = PRow + Font.Height + Buffer

            'opening the sql connection
            OleDbConn.Open()
            MyOledbCommand.Connection = (OleDbConn)
            MyOledbCommand.CommandText = "SELECT VatRate, VatBat, VATTrn, VatDate, VatRef, VatType, VatNom, VatNarr, VatGross, Vat, VatNet FROM dbo.VatDetRpt where VatType = 'PINV' or VatType = 'BPI'"
            MyOledbCommand.Connection = OleDbConn
            MyOledbCommand.ExecuteNonQuery()
            dr = MyOledbCommand.ExecuteReader()

            Do While dr.Read = True
                If PRow >= pageHeight - bottomMargin Then
                    e.HasMorePages = True
                    dlgPrintPreview.Document = PreparePrintDocumentVATDetail()
                    PRow = 170
                Else

                    Col8 = dr.GetValue(8)
                    Col9 = dr.GetValue(9)
                    Col10 = dr.GetValue(10)

                    Col8.ToString("N2")
                    Col9.ToString("N2")
                    Col10.ToString("N2")

                    e.Graphics.DrawString(dr.GetValue(0), F8, Brushes.Black, C1, PRow)
                    e.Graphics.DrawString(dr.GetValue(1), F8, Brushes.Black, C2, PRow)
                    e.Graphics.DrawString(dr.GetValue(2), F8, Brushes.Black, C3, PRow)
                    e.Graphics.DrawString(dr.GetValue(3), F8, Brushes.Black, C4, PRow)
                    e.Graphics.DrawString(dr.GetValue(4), F8, Brushes.Black, C5, PRow)
                    e.Graphics.DrawString(dr.GetValue(5), F8, Brushes.Black, C6, PRow)
                    e.Graphics.DrawString(dr.GetValue(6), F8, Brushes.Black, C7, PRow)
                    e.Graphics.DrawString(dr.GetValue(7), F8, Brushes.Black, C8, PRow)
                    e.Graphics.DrawString(Col8.ToString("N2"), F8, Brushes.Black, C9, PRow)
                    e.Graphics.DrawString(Col9.ToString("N2"), F8, Brushes.Black, C10, PRow)
                    e.Graphics.DrawString(Col10.ToString("N2"), F8, Brushes.Black, C11, PRow)
                    PRow = PRow + Font.Height + Buffer
                End If

            Loop
            Dim PIGross As Decimal = lblPIGrossTotal.Text
            Dim PIVAT As Decimal = lblPIVatTotal.Text
            Dim PINet As Decimal = lblPINetTotal.Text

            e.Graphics.DrawString(PIGross.ToString("N2"), F8B, Brushes.Black, C9, PRow)
            e.Graphics.DrawString(PIVAT.ToString("N2"), F8B, Brushes.Black, C10, PRow)
            e.Graphics.DrawString(PINet.ToString("N2"), F8B, Brushes.Black, C11, PRow)

            PRow = PRow + Font.Height + Buffer
            PRow = PRow + Font.Height + Buffer
            'SINVB1
            e.Graphics.DrawString("Purchase Credit Note", F9B, Brushes.Black, C1, PRow - Font.Height)
            e.Graphics.DrawString("Rate %", F9U, Brushes.Black, C1, PRow)
            e.Graphics.DrawString("Batch", F9U, Brushes.Black, C2, PRow)
            e.Graphics.DrawString("Trn No", F9U, Brushes.Black, C3, PRow)
            e.Graphics.DrawString("Date", F9U, Brushes.Black, C4, PRow)
            e.Graphics.DrawString("Ref", F9U, Brushes.Black, C5, PRow)
            e.Graphics.DrawString("Type", F9U, Brushes.Black, C6, PRow)
            e.Graphics.DrawString("Nom A/C", F9U, Brushes.Black, C7, PRow)
            e.Graphics.DrawString("Narrative", F9U, Brushes.Black, C8, PRow)
            e.Graphics.DrawString("Gross", F9U, Brushes.Black, C9, PRow)
            e.Graphics.DrawString("VAT", F9U, Brushes.Black, C10, PRow)
            e.Graphics.DrawString("Net", F9U, Brushes.Black, C11, PRow)
            PRow = PRow + Font.Height + Buffer
            OleDbConn.Close()
            'opening the sql connection

            OleDbConn.Open()
            MyOledbCommand.Connection = (OleDbConn)
            MyOledbCommand.CommandText = "SELECT VatRate, VatBat, VATTrn, VatDate, VatRef, VatType, VatNom, VatNarr, VatGross, Vat, VatNet FROM dbo.VatDetRpt where VatType = 'PCN' or VatType = 'BPC'"
            MyOledbCommand.Connection = OleDbConn
            MyOledbCommand.ExecuteNonQuery()
            dr = MyOledbCommand.ExecuteReader()

            Do While dr.Read = True
                If PRow >= pageHeight - bottomMargin Then
                    e.HasMorePages = True
                    PRow = 170
                Else

                    Col8 = dr.GetValue(8)
                    Col9 = dr.GetValue(9)
                    Col10 = dr.GetValue(10)

                    Col8.ToString("N2")
                    Col9.ToString("N2")
                    Col10.ToString("N2")

                    e.Graphics.DrawString(dr.GetValue(0), F8, Brushes.Black, C1, PRow)
                    e.Graphics.DrawString(dr.GetValue(1), F8, Brushes.Black, C2, PRow)
                    e.Graphics.DrawString(dr.GetValue(2), F8, Brushes.Black, C3, PRow)
                    e.Graphics.DrawString(dr.GetValue(3), F8, Brushes.Black, C4, PRow)
                    e.Graphics.DrawString(dr.GetValue(4), F8, Brushes.Black, C5, PRow)
                    e.Graphics.DrawString(dr.GetValue(5), F8, Brushes.Black, C6, PRow)
                    e.Graphics.DrawString(dr.GetValue(6), F8, Brushes.Black, C7, PRow)
                    e.Graphics.DrawString(dr.GetValue(7), F8, Brushes.Black, C8, PRow)
                    e.Graphics.DrawString(Col8.ToString("N2"), F8, Brushes.Black, C9, PRow)
                    e.Graphics.DrawString(Col9.ToString("N2"), F8, Brushes.Black, C10, PRow)
                    e.Graphics.DrawString(Col10.ToString("N2"), F8, Brushes.Black, C11, PRow)
                    PRow = PRow + Font.Height + Buffer
                End If
            Loop
            Dim PCNGross As Decimal = lblPCNGrossTotal.Text
            Dim PCNVAT As Decimal = lblPCNVatTotal.Text
            Dim PCNNet As Decimal = lblPCNNetTotal.Text

            e.Graphics.DrawString(PCNGross.ToString("N2"), F9B, Brushes.Black, C9, PRow)
            e.Graphics.DrawString(PCNVAT.ToString("N2"), F9B, Brushes.Black, C10, PRow)
            e.Graphics.DrawString(PCNNet.ToString("N2"), F9B, Brushes.Black, C11, PRow)

            OleDbConn.Close()

        Catch err As System.Exception
            MsgBox(err.Message)
        End Try
        'H1
        e.Graphics.DrawString("VAT Return Report", F18, Brushes.Black, pagewidth / 2, RH1)
        e.Graphics.DrawString("Client:", F9B, Brushes.Black, C1, RH2)
        e.Graphics.DrawString("Year End:", F9B, Brushes.Black, C1, RH3)
        e.Graphics.DrawString("", Font8, Brushes.Black, C1, RH4)
        e.Graphics.DrawString("Transaction Sorted By Group", F9B, Brushes.Black, C1, RH5)


        'F1
        e.Graphics.DrawString("Printed on: " & DateAndTime.Now, F7, Brushes.Black, C9, Row28)
        e.HasMorePages = False

    End Sub
    Private Sub btnPrintVATDetail_Click(sender As Object, e As EventArgs) Handles btnPrintVATDetail.Click
        Try
            dlgPrintPreview.Document = PreparePrintDocumentVATDetail()
            dlgPrintPreview.WindowState = FormWindowState.Maximized
            dlgPrint.PrinterSettings = PrintDocument1.PrinterSettings
            dlgPrintPreview.ShowDialog()

        Catch err As System.Exception
            'MsgBox(err.Message)
        End Try
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dirk StraussSenior Full Stack Developer
Distinguished Expert 2017

Commented:
What is the error you are receiving? Can you be a bit more specific?

Author

Commented:
To be honest Im not getting an error.  Im getting the output of printed twice over the same page.  Initially I had it printing off the bottom of the page hence the reason I put this in the code for each printable section

                If PRow >= pageHeight - bottomMargin Then
                    e.HasMorePages = True
                    dlgPrintPreview.Document = PreparePrintDocumentVATDetail()
                    PRow = 170
                Else

Obviously checking for the bottom of the page and then trying to create a new page.

Author

Commented:
Could any one tell me where Im going wrong?
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
Dirk,

I am still having no joy with this.  Can you point me in the right direction?

Regards
Rob
change this:
If PRow >= pageHeight - bottomMargin Then
                    e.HasMorePages = True
                    dlgPrintPreview.Document = PreparePrintDocumentVATDetail()
                    PRow = 170

Open in new window


TO:
 
Dim font As New Font("Microsoft Sans Serif", 10)
Dim lineHeight As Single = font.GetHeight(e.Graphics)
Dim PRow As Single = e.MarginBounds.Top
Dim bottomMargin As Single = e.MarginBounds.Bottom

PRow += lineHeight    <--------> This line goes after the single line that is printed

Open in new window



sample:
With e.Graphics
            .DrawString("Purchase Credit Note", F9B, Brushes.Black, C1, PRow )
            .DrawString("Rate %", F9U, Brushes.Black, C1, PRow)
            .DrawString("Batch", F9U, Brushes.Black, C2, PRow)
            .DrawString("Trn No", F9U, Brushes.Black, C3, PRow)
            .DrawString("Date", F9U, Brushes.Black, C4, PRow)
            .DrawString("Ref", F9U, Brushes.Black, C5, PRow)
            .DrawString("Type", F9U, Brushes.Black, C6, PRow)
            .DrawString("Nom A/C", F9U, Brushes.Black, C7, PRow)
            .DrawString("Narrative", F9U, Brushes.Black, C8, PRow)
            .DrawString("Gross", F9U, Brushes.Black, C9, PRow)
            .DrawString("VAT", F9U, Brushes.Black, C10, PRow)
            .DrawString("Net", F9U, Brushes.Black, C11, PRow)
End With
PRow += lineHeight

' *********** This goes at the bottom of a finished single record to print ***********
             If   PRow >= e.MarginBounds.Bottom Then
                    e.HasMorePages = True
                    iPageCnt += 1     <------> I used this for a page count to print in header or bottom of page
             Else
                    e.HasMorePages = False
           End If

Open in new window


sample:
 
Private Sub btnPrintVATDetail_Click(sender As Object, e As EventArgs) Handles btnPrintVATDetail.Click
        Try
  ' Perform processing here.
        Me.dlg = New PrintPreviewDialog()
        With dlg
            .Document = mDoc
            '  .MdiParent = frmMain
            .WindowState = FormWindowState.Normal
            .StartPosition = FormStartPosition.CenterScreen
            ' Set the UseAntiAlias property to true, which will allow the 
            ' operating system to smooth fonts.
            '    .UseAntiAlias = True
            '.PrintPreviewControl.Zoom = 0.9
            ._preview.Zoom = 0.75
            .Height = 700
            .Width = 900
            mDoc.DefaultPageSettings.Landscape = True
            mDoc.DefaultPageSettings.Margins.Top = 25.0
            mDoc.DefaultPageSettings.Margins.Bottom = 25.0
            .ShowDialog()
        End With
        Me.btnPrint.Enabled = True
       Catch err As System.Exception
            'MsgBox(err.Message)
        End Try
    End Sub

Open in new window

Author

Commented:
Brilliant thank you will try that today.

Author

Commented:
Apologies in the delay in getting back to you. Personal / physical reasons.

Can you please explain to me what 'dlg' and 'mDoc' variables are
'************ Printing Area *******************
    Private WithEvents dlg As PrintPreviewDialog
    Private WithEvents mDoc As New PrintDocument()

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial