Justify text when printing

Whose sick of vb not having a fully justify? I was, although you dont get any justification when printing, its easy enough to just do X - printer.textwidth (right justify), X - printer.textwidth/2 (centered).

Here's a bit of code to justify text.
LineSpace is the space that your printing into (the page width if its a letter). Printlines is an array of lines of text.  eHzRight, eHzCenter etc. are just my constants in my app. The SetBkMode(Printer.hdc, 1) is an api to make the text transparent (its a problem in windows).  Xval and Yval are your starting position.


   'Take each line one at a time
   For L = LBound(PrintLines) To UBound(PrintLines)
       
       PrintLine = PrintLines(L)
       
       'Set x ready to print line just "picked out" of memo field
       If Align = eHzCenter Then
           Xval = x - (0.5 * Printer.TextWidth(PrintLine))
       ElseIf Align = eHzRight Then
           Xval = x - Printer.TextWidth(PrintLine)
       Else
           Xval = x
       End If
       
       Printer.CurrentX = Xval
       i = SetBkMode(Printer.hdc, 1)
       
       If Align = eHzFull Then
           
           'FULL JUSTIFICATION
           Dim Words() As String
           Dim LineX As Long
           Dim w As Long
           Dim SpaceLeft As Long
           Dim SpaceLen As Long
           Dim SpaceCount As Long
           '---------------------
           
           Words = Split(PrintLine, " ")
           LineX = Xval
           
           'Get space left after line
           SpaceLeft = LineSpace - Printer.TextWidth(PrintLine)
           SpaceCount = CountString(PrintLine, " ")
           If SpaceCount = 0 Then
               SpaceLen = Printer.TextWidth(" ")
           Else
               SpaceLen = Printer.TextWidth(" ") + (SpaceLeft / SpaceCount)
           End If
           
           Printer.CurrentX = Xval
           
           If L = UBound(PrintLines) Then
               'Just print LAST line as normal
               Printer.Print PrintLine
           ElseIf Trim(PrintLines(L + 1)) = "" Then
               'just print END OF PARA line as normal
               Printer.Print PrintLine
           Else
               'Get each word in line
               For w = LBound(Words) To UBound(Words)
                   Printer.CurrentY = Yval
                   
                   'Print Ahoy!
                   Printer.CurrentX = LineX
                   Printer.Print Words(w)
                   
                   LineX = LineX + Printer.TextWidth(Words(w)) + SpaceLen
               Next w
           
           End If
           
           Yval = Printer.CurrentY
           
       Else
           Printer.Print PrintLine
       End If
       
       PrintLine = ""          'Reinitialze printline so it can be built up again
   Next L

I hope this is useful to a few of you!

You might also like...

Comments

Chris Gibson

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski