locked
Printing multiple lines with different font on each line and centered each line RRS feed

  • Question

  • Hi,

    I have problem with printing multiple lines with different font on each line and centered each line vb.net.

    I could only print one line of text for each page because I use the printing part as below:

    Public Sub PrintHeader()
    
      Dim Print As New My.myPrinter
      Dim Printstring As String
      Dim HeaderFont As Font
      Dim StringSize As SizeF
      
         Printstring = BusinessInfo.telephone
         HeaderFont = New Font("FontB11", 12.0F)
         StringSize = TextRenderer.MeasureText(Printstring, HeaderFont)
         Print.prt(Printstring, True, (280 - CInt(StringSize.Width.ToString)) / 2, 40, "FontB11", 12.0F)
    
    end sub
    
    Public Class myPrinter
            Friend TextToBePrinted As String
            Friend ssFontName As String
            Friend bbBold As Boolean
            Friend iiFontSize As Integer
            Friend cCoordX As Single
            Friend cCoordY As Single
            Public Sub prt(ByRef txtPrint As String, ByRef bBold As Boolean, ByRef CoordX As Short, _
                           ByRef CoordY As Short, Optional ByRef sFontName As String = "", Optional ByRef iFontSize As Short = 0)
                TextToBePrinted = txtPrint
                ssFontName = sFontName
                bbBold = bBold
                iiFontSize = iFontSize
                cCoordX = CoordX
                cCoordY = CoordY
                Dim prn As New Printing.PrintDocument
                Using (prn)
                    AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
                    prn.Print()
                    RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
                End Using
            End Sub
            Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
                Dim myFont As New Font(ssFontName, iiFontSize)
                Dim drawBrush As New SolidBrush(Color.Black)
                Dim drawPoint As New PointF(cCoordX, cCoordY)
                Dim drawFormat As New StringFormat
                drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
                args.Graphics.DrawString(TextToBePrinted, myFont, drawBrush, drawPoint)
            End Sub
        End Class

    However, if I print 2 or more lines as below I got 2 or more pages printed out because each line was treated as a single page. 
     Public Sub PrintHeader()
            Dim Print As New My.myPrinter
            Dim Printstring As String
            Dim HeaderFont As Font
            Dim StringSize As SizeF
           
       Printstring = BusinessInfo.telephone
       HeaderFont = New Font("FontB11", 12.0F)
       StringSize = TextRenderer.MeasureText(Printstring, HeaderFont)
       Print.prt(Printstring, True, (280 - CInt(StringSize.Width.ToString)) / 2, 40, "FontB11", 12.0F)
    
       Printstring = BusinessInfo.address + ", " + BusinessInfo.city + ", " + BusinessInfo.zip
       HeaderFont = New Font("FontB11", 12.0F)
       StringSize = TextRenderer.MeasureText(Printstring, HeaderFont)
       Print.prt(Printstring, True, (280 - CInt(StringSize.Width.ToString)) / 2, 40, "FontB11", 12.0F)
     End Sub
    Thank you very much for any suggestion.

    Wednesday, October 1, 2014 12:29 AM

Answers

  • Tony,

    Each time you call your prt() sub it calls the PrintPageHandler when the line prn.Print() executes and each time the print handler is called ejects a page (unless you set it otherwise). So you are calling the print handler with each string in the header sub, ejecting a page each time.

    You need to put everything to print the three strings in the print handler. Then just call the prt sub once. You could eliminate the PrintHeader sub and a lot of the variable swapping you do ie cCoordX = CoordX etc.

    I hope that makes sense.

    If you can give a better description or a picture of the result you want to end up with we might be able to help more.

    • Proposed as answer by Frank L. Smith Wednesday, October 1, 2014 2:14 AM
    • Marked as answer by Carl Cai Friday, October 17, 2014 9:38 AM
    Wednesday, October 1, 2014 1:48 AM