none
Using the print method of the printer class in VB.NET RRS feed

  • Question

  • I am using the print class (from the VB power pack) to print a report to the printer. When the detail line prints, I have blank spaces between the data values being printed and can't seem to get rid of them. The code that executes is a follows:

            Dim printer As New Printer

            Dim intPageNum As Integer = 1
            Dim intDtlLinePerPage As Integer = 46
            Dim intTotPageCnt As Integer
            Dim intRemainder As Integer

            Dim strCurrPageNum As String
            Dim strTotPageNum As String

            If PrintDialog1.ShowDialog = DialogResult.OK Then
            Else
                Exit Sub
            End If

            intTotPageCnt = dgvBscvrgGrpCd.RowCount / intDtlLinePerPage
            intRemainder = dgvBscvrgGrpCd.RowCount Mod intDtlLinePerPage
            If intRemainder > 0 Then
                intTotPageCnt = intTotPageCnt + 1
            End If

            printer.Orientation = vbPRORLandscape
            printer.FontName = "Courier"
            printer.FontSize = 10
            printer.PrintQuality = vbPRPQMedium
            printer.PaperSize = vbPRPSLetter

            strCurrPageNum = intPageNum.ToString("##0")
            strTotPageNum = intTotPageCnt.ToString("##0")

            printer.Print("PAGE ", Trim(strCurrPageNum), " OF ", Trim(strTotPageNum))

            printer.EndDoc()

    The line that prints is a follows:

    PAGE         1            OF         1

    Any help is greatly appreciated

    Friday, March 2, 2018 7:29 PM

All replies

  • I am using the print class (from the VB power pack) to print a report to the printer. When the detail line prints, I have blank spaces between the data values being printed and can't seem to get rid of them. The code that executes is a follows:

            Dim printer As New Printer

            Dim intPageNum As Integer = 1
            Dim intDtlLinePerPage As Integer = 46
            Dim intTotPageCnt As Integer
            Dim intRemainder As Integer

            Dim strCurrPageNum As String
            Dim strTotPageNum As String

            If PrintDialog1.ShowDialog = DialogResult.OK Then
            Else
                Exit Sub
            End If

            intTotPageCnt = dgvBscvrgGrpCd.RowCount / intDtlLinePerPage
            intRemainder = dgvBscvrgGrpCd.RowCount Mod intDtlLinePerPage
            If intRemainder > 0 Then
                intTotPageCnt = intTotPageCnt + 1
            End If

            printer.Orientation = vbPRORLandscape
            printer.FontName = "Courier"
            printer.FontSize = 10
            printer.PrintQuality = vbPRPQMedium
            printer.PaperSize = vbPRPSLetter

            strCurrPageNum = intPageNum.ToString("##0")
            strTotPageNum = intTotPageCnt.ToString("##0")

            printer.Print("PAGE ", Trim(strCurrPageNum), " OF ", Trim(strTotPageNum))

            printer.EndDoc()

    The line that prints is a follows:

    PAGE         1            OF         1

    Any help is greatly appreciated

    I'm not sure what that overload of the Print() method does, but perhaps it inserts spaces automatically and you don't need spaces in your strings "PAGE " and " OF"?

    At any rate, if there is an overload that takes one parameter, just send a single concatenated string:

    printer.Print("PAGE " & strCurrPageNum.ToString & " OF " & strTotPageNum.ToString)

    or in VS2017:

    printer.Print($"PAGE {strCurrPageNum} OF {strTotPageNum}")


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, March 2, 2018 9:21 PM
    Moderator
  • With regard to Visual Basic PowerPacks they are no longer supported (Visual Basic Power Packs Controls) therefore you may want to move to the System.Drawing.Printing Namespace of which others can probably assist you much easier.

    Also see Windows Forms Print Support.


    La vida loca

    Wednesday, March 7, 2018 5:00 AM