Printing in VB

Printing Text

Using the Printer object further, you can also apply formatting to the text you output by setting the Font, and its associated FontSize, FontBold, FontItalic, FontStrikeThru and FontUnderline properties. To change the font, simply change the FontName property. For example,

Printer.FontName = "Tahoma"

changes the font to Tahoma. To change the size, just call Printer.FontSize. Most of the other formatting options are just boolean (ie True or False), which makes it even easier to apply formatting. The code below gives a simple demonstration.

With Printer
    Printer.Print "Normal Line"
    .FontBold = True
    Printer.Print "Bold Line"
    .FontItalic = True
    Printer.Print "Bold and Italic Line"
    .FontBold = False
    .FontItalic = False
    Printer.Print "Second Normal Line"
    .EndDoc
End With

Note
Although I am using a With statement, I still have to write Printer.Print for outputting the text. This is another 'quirk' of VB - the Print statement is not actually belong to the Printer object, or in fact any other object in VB. If you search for Print in the Object Browser (press F2), you won't find it anywhere. Print is in fact a statement that VB interprets on its own, and outputs it according to the object before the Print statement. If you call PictureBox1.Print, it will 'print' the text into the PictureBox control. As such, the Print function behaves just as if it were an object of PictureBox or Printer. However, the designers of VB obviously didn't extend this behavior to With statements....

For a full list of font options, go to the Object Browser (F2), and select Printer from the Classes list.

One other thing worth noting regarding formatting... If you are using the RichTextBox control, you don't need to worry about applying all the formatting - there is some code using API calls which does it all for you, including page margins! Click here to see that code.

Setting the alignment of text isn't quite so simple, VB doesn't provide an align property. Instead, we need to work out where the middle of the page is, and then take into account the width of the text. Take a look at this code, which prints the page number in the middle of the page.

sMsg = "Page " & Printer.Page ' Print Page number
HWidth = Printer.TextWidth(sMsg) / 2 ' Get one-half width.
HHeight = Printer.TextHeight(sMsg) /2 ' Get one-half height.
Printer.CurrentX = Printer.ScaleWidth / 2 - HWidth
Printer.CurrentY = Printer.ScaleHeight / 2 - HHeight
Printer.Print sMsg

This code uses the CurrentX and CurrentY to change the position of where the text is outputted, centers the position, and prints the page number using the Printer's Page property. If you want the text in the middle at the top of the page, simply comment out the Printer.CurrentY line.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

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.

“The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but 'That's funny...'” - Isaac Asimov