We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 17,823 times

Related Categories

Justify text when printing

chrgibson

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!

Comments

  • Justifying Text

    Posted by MaverickRat on 05 Mar 2002

    Havent tested this but the code looks good
    Just the sort of thing vb should do anyway i think.