Page 1 of 1

How to right justify text on a printer (Windows)?

Posted: Tue Feb 14, 2006 1:16 pm
by dpaterso
This is driving me insane!

Does anyone have a routine that will right justify text on a printer.

I am using the 'Courier New' font which does not appear to be a proportional font but still do not seem to get it right.

In the 'old' DOS days we would say:

@ 0, 80 - Len( cVar1 ) Say cVar1
@ 1, 80 - Len( cVar2 ) Say cVar2

but this does not work in windows.

I have been pulling my hair out the whole morning with this (type) of code:

...

Define Font oFont;
Name 'Courier New';
Size 15, 50

Print oPrn Name 'Report' From User

If Empty( oPrn:hDC )
oPrn:End( )
Return ( NIL )EndIf

oPrn:SetFont( oFont )

nRowStep := oPrn:nVertRes( ) / 66 // 66 rows
nColStep := oPrn:nHorzRes( ) / 80 // 80 cols

oPrn:SetPortrait( )

Page

nRow += nRowStep
oPrn:Say( nRow, ( nColStep * 80 ) - (nColStep * ( Len( 'D' ) ) ), 'D' )

nRow += nRowStep
oPrn:Say( nRow, ( nColStep * 80 ) - (nColStep * ( Len( 'DD' ) ) ), 'DD' )

EndPage

EndPrint

oPrn:End( )

etc. etc.

If you keep repeating the lines above the text 'creeps' to the left.

Any ideas?

Regards,

Dale.

Re: How to right justify text on a printer (Windows)?

Posted: Tue Feb 14, 2006 2:17 pm
by Enrico Maria Giordano
Have a look at the eighth parameter of TPrinter:Say() method (nPad).

EMG

Posted: Tue Feb 14, 2006 7:06 pm
by manuramos
RPAD not run efectively in many situations, Try this:

nRightMargin := xxx pixels
oPrn:Say( nTop,nRightMargin-oPrn:GetTextWidth( cText , oFont ),cText,oFont )

You could build a function to only right justify all cTexts. (excuse my english)

Right Justify to Printer

Posted: Tue Feb 14, 2006 11:12 pm
by E. Bartzokas
Try this:

LPAD(myvariable, 12) for character strings

(your variable can be a field name of course, and the number 12 is how many spaces will be added infront of the variable/field.
Same way as it is required to use: Str(nNumber, 12,2) for numbers

I hope I gave you an idea to help you.

Regards
Evans

Posted: Wed Feb 15, 2006 12:23 am
by James Bott

Code: Select all

#include "fivewin.ch"

function main()
   local oPrn,oFont, nRow:=0,nRowStep:=1,nColStep:=1, aText, I

   Print oPrn Name 'Report' preview

   If Empty( oPrn:hDC )
   oPrn:End( )
   Return ( NIL )
   EndIf

   Define Font oFont;
   Name 'Courier New';
   Size 15, 10 of oPrn

   oPrn:SetFont( oFont )

   nRowStep := oPrn:nVertRes( ) / 66 // 66 rows
   nColStep := oPrn:nHorzRes( ) / 80 // 80 cols

   oPrn:SetPortrait( )

   aText:= {"D","very long text","shorter","123456789","cat"}

      Page

      for I:= 1 to 5

         nRow += nRowStep
         //oPrn:Say( nRow, ( nColStep * 80  ) - (nColStep * ( Len( "D" ) ) ), "D" )
         // Last parameter: 0= left justify, 1= right justify, 2= Center justify
         oPrn:Say( nRow, ( nColStep * 20 ),aText[i],,,,,1)

      next

      EndPage

   EndPrint

   oPrn:End( )

   oFont:end()

return nil

Posted: Wed Feb 15, 2006 7:53 am
by Enrico Maria Giordano
James Bott wrote:

Code: Select all

EndPrint

oPrn:End( )
You don't have to issue the latter as it is included in the function called by EndPrint command (PrintEnd()).

EMG