Printers

Using the PrintDocument component in VB.NET applications


If you want to allow users to print form components in run-time, the PrintDocument component lets you offer users this functionality. The PrintDocument component is an object that allows users to send an output to a printer from Windows Forms applications. In this tip, I show you how to use the component in your VB.NET applications.

Using PrintDocument

In order to use the PrintDocument component, you need to double-click on the PrintDocument component in the Toolbox. Once the component is added to the form, you can set its properties and print.

In my example, I will add the PrintDocument component, a TextBox, and a command button to the form. Then, I will add the following code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        PrintDocument1.PrinterSettings.Copies = 2

        PrintDocument1.Print()

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Blue, 100, 100)

End Sub

How this works

When debugging the application, click on the command button. The text found in the TextBox (usually TextBox1, assuming that you didn't change it), will print to the default printer. When the button is clicked, two copies of the document will be sent to the printer since we requested two copies by setting the Copies property value to 2.

On the Print_Page event of the PrintDocument, I use the Graphics class' DrawString method to select the object contents of a TextBox, specifying the font, brush color, and the sizes.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.

----------------------------------------------------------------------------------------

Get Visual Basic tips in your inbox

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!

6 comments
Sameerchand
Sameerchand

PrintDocument PrintPreviewDialog and PrintDialog Control in VB.Net related link

hemanth.tce
hemanth.tce

Which is the source file for the printDocument object? Which file/document does the printDocuemnt object prints?

Justin James
Justin James

Thanks for this article! Printing used to be a lot more work than it is now, but a lot of people do not know that it got so much easier. J.Ja

elitecobra
elitecobra

If I remember correctly, printing the the form contents in Classic VB was very easy. Unfortunately that didn't carry over to .NET. They did recently release a new control that will help make this easier to do. But I have not used it yet. Microsoft says: PrintForm Component 1.0 - "The PrintForm component is designed to bring back the ability to easily print a Windows Form. With this the new PrintForm component you can once again layout the Windows Form exactly as you want it and allow your users to print the form as a quick report" So anyone wanting to print the form, this little control should make it much easier :) Edit: Forgot to post the link - Click here to get the PrintForm control. Jason H VB Code For Free VB Code Source

Editor's Picks