Question Printing formatted strings

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
I have VB 2005, and have successfully printed:
Line 1
Line 2
Line 3
etc. using ev. Graphics.etc as per numerous examples.
However, I want to print
Company Name In Bold font size 16 Ltd​
Date: font size 12, Normal not bold​
A line of underscores____________________________________
A blank line
Then columns of things, some items bold, some not.
First Name Last Name
John Little
Robin Hood
Will Scarlett

Sorry, just imagine these names are in columns!
And so on. Easy in VB6, please how can I achieve this in VB2005? Thank you.
 
I should add, I know there's a Left Margin property. My question is really, am I right in that a whole page's worth of strings to be printed has to be preloaded before calling PrintDocument1 and the associated PrintPage?
Is it possible to get one string, print it, then proceed to the next string? How do you handle 328 pages of product listings, formatted into 7 or 8 columns, for example?
 
DrawString method takes a Font object that can be any font you can imagine. The font has a GetHeight method to give you information when only need to know line height of that font. Graphics has a MeasureString method for more advanced string measurements. For example you may choose to have fixed column widths but some fields may then span several lines, MeasureString can then tell you the height of the drawn text given the available width. For a centered header use it to measure text width, then calculate from e.MarginBounds.Width the corresponding X location (=(availwidth-textwidth)/2). You do need to keep track of the location during the page print to know where to put next string etc. A "line of underscores" is probably easier to do with DrawLine. If you want to calculate column widths to fit you have to iterate the data first or do some lookup to find each column longest string, depends on the data source. No data has to be loaded before you call print, there are many examples of stream based data sources like datareaders and streamreaders that can be used.
 
Thanks, John. I am just not sure of the 'something' or 'somethingelse' in the text width string:

mnumLeftMargin = ev.Graphics.MeasureString(mstrThisString, fntPrintFont, (ev.MarginBounds.Width / 2) - (something.TextWidth / 2), somethingelse.StringFormat).

Apart from that, it's great, thank you.
 
I think you have misunderstood something, orelse it's me :confused: Take this MeasureString overload for example:
VB.NET:
Public Function MeasureString ( _
	text As String, _
	font As Font, _
	width As Integer _
) As SizeF
If you were to measure "header" you would call:
VB.NET:
Dim stringSize As SizeF = e.Graphics.MeasureString("header", yourfont, e.MarginBounds.Width)
To calculate centering use these facts: Now stringSize.Width is the actual width of the "header" string given that font. The available width of drawing area of full page is still e.MarginBounds.Width. The X location to start drawing the text centered within the available width is then (availwidth-textwidth)/2. That X location has to be justified by e.MarginBounds.Left because that is the X location where available width starts.

Here are some numbers to see this: for example page width 120, margin 10 around, available width is then 100, "header" width 20. Which gives 100-20=80, 80/2=40. This means "header" will start relatively at 40 and span 20 and end at 60, there will be 40px empty space at either side of available width. Add margin 10 to that and you got the absolute X location in page where to draw that header, that is X=50. The Y location can be no less than e.MarginBounds.Top (which is 10 with this margin). The drawing show these figures (it's a very "sketchy" page):
Untitled.png
 
It was:
VB.NET:
Dim stringSize As SizeF = e.Graphics.MeasureString("header", yourfont, e.MarginBounds.Width)
that was what I was missing.

Centering now works just fine, thank you, although my right-handed, left-justified brain still wants to shift centred text a bit to the left, to make it look OK. Is there a software work-around for that...? :)
 
Back
Top