Normal Topic Printstring syntax to center text (Read 358 times)
jyellis
Full Member
Members
***
Offline


No personal text

Posts: 114
Location: Dallas-Fort Worth Area
Joined: May 19th, 2003
Printstring syntax to center text
Apr 27th, 2004 at 2:49pm
Print Post Print Post  
I'm using the Printstring commands to generate several reports.  Is there a way to center the string you want to print on the page?
  
Back to top
 
IP Logged
 
TJCajun
Junior Member
**
Offline


"Laissez les bons temps
rouler"

Posts: 72
Location: Louisiana
Joined: Nov 25th, 2002
Re: Printstring syntax to center text
Reply #1 - Apr 28th, 2004 at 1:06am
Print Post Print Post  
I'm assuming that by "string" you probably mean something like a title, or some similar type of static text.  If you are trying to print columns, be sure to see pp184-188 in the Programming Guide.  Also see the February 2004 issue of Inside Sesame for an article on "Precision Check Printing with Print String" - it should be of help.

If you know what the string value is that you want to center, then you can be specific in your PrintString.  If the text of the string needs to vary, then you'll have to use variables, and incorporate them in the PrintString.

If you set NewPage(850,1100) you have designated an 8.5x11 inch sheet of paper as having 100 "units" per inch, in both directions.

In PrintString, there are 7 parameters.  You will have to be concerned with several of these to center this text.

XPos is the horizontal starting position of the string
Width sets the width of string.  (You will set this to an integer value of zero, unless you need the string to wrap to a new line.)
FontSize is the size of the font in the above "units".  This is NOT "point size".  A font size of 16 when there are 100 units/inch means a capital letter is 16/100 units tall, or not quite 3/16 of an inch.

XPos and FontSize may be the only parameters that we need to deal with here.  You must set values for ALL 7 of the parameters when you set up your print string, but I'm only dealing with what is needed to center text.

Lets say you want to center the text:   THIS IS CENTERED

There are 16 characters in this text string, including the 2 spaces.  If you set the FontSize at 20, then that means you have used 320 "units" of the width of the page.  The page is 850 units wide, so 850 minus 320 leaves 530 units of "blank" space to be split equally on both sides of the centered text.  Dividing this by 2 gives a value of about 265 units, which would be the value for XPos

I'm "winging it" on this next part - I haven't tried this.  But if you want to use variables, so that you don't have to calculate this value for a text string that may vary, you would need to designate variables such a vTEXT for the string and vFontSize for the font size.  Then the value for the XPos position might be something similar to:

  (850 - (@Len(vText) x vFontSize)) / 2

This says find the Length of vText, multiply it by the FontSize.  Subtract this value from 850 (the width of the paper in designated units) and then divide by 2 to split the blank area in half.

I bet someone else will come up with something more elegant than this!

Also, in the May issue of Inside Sesame that is about to come out, there is a great article that uses PrintString() to generate a report.  I just finished proofreading the issue for Marble Publications, so I can't really say any more than that.

Hope this helped some.

  
Back to top
IP Logged