Simple PrintDocument Example In VB.NET

This code snippet shows you how to use the PrintDocument class to print documents. It uses the PrintPreviewDialog to show the preview of the document and a button to print the document.



  1. ' Place a button on the form (PrintPreview)
  2. ' PLace a textbox control on the form (TextData)
  3. Imports System.Drawing
  4. Imports System.Drawing.Printing
  5. Public Class Form1
  6.     Private Doc As New PrintDocument()
  7.     Private Sub PrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreview.Click
  8.         Dim ppDlg As New PrintPreviewDialog()
  9.         ppDlg.Document = Doc
  10.         ppDlg.Show()
  11.         AddHandler Doc.PrintPage, AddressOf Print_PrintPage
  12.     End Sub
  13.     Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
  14.         Dim g As Graphics = e.Graphics
  15.         Dim Brush As New SolidBrush(Color.Black)
  16.         g.DrawString(Me.TextData.Text, New Font("arial", 9), Brush, 10, 10)
  17.     End Sub
  18. End Class

No comments:

Post a Comment