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 213,126 times

Contents

Related Categories

Printing in VB - Printing Text

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.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Posted by bakuretsu on 27 Sep 2005

    Can you tell me how to set the margins and printer orientation also using this method. It should work in VB .NET 2003 I'm assuming.

    I need to be able to control the printer margins and headers and...

  • Cannot print from Local printer connected on Win X

    Posted by vasanthi on 27 Sep 2005

    [Hi,

    Facing problem in printing from local printer connected to WinXp from VB6. I am able to capture Printer.devicename and thru this print to network printer. But when trying to print on Local pri...

  • problem in printing

    Posted by aadreja on 04 Jul 2005

    hi suhaas

    have u added new paper...

    now go to properties of your generic printer..
    in printing preferences select default paper instead of letter select ur created custom paper...

    now open ...

  • Posted by suhaas on 04 Jul 2005

    hi aadreja,

    thanks for your reply.

    I tried to do as per your reply. But this does not work with VB6. I am working on a invoice printing application where I am using Generic Text printer with VB6...

  • custom paper size in xp

    Posted by aadreja on 04 Jul 2005

    for setting custom paper size in xp
    u have to create form...

    go to start->settings->printers
    in file menu go to "server properties"
    create new from with desired paper size... and give name

    n...