Advertisement
Guest User

Print class VB.Net

a guest
Apr 21st, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.53 KB | None | 0 0
  1. Public Class PCPrint : Inherits Printing.PrintDocument
  2. #Region " Property Variables "
  3.     Private _font As Font
  4.     Private _text As String
  5. #End Region
  6. #Region " Class Properties "
  7.     Public Property TextToPrint() As String
  8.         Get
  9.             Return _text
  10.         End Get
  11.         Set(ByVal value As String)
  12.             _text = value
  13.         End Set
  14.     End Property
  15.     Public Property PrinterFont() As Font
  16.         Get
  17.             Return _font
  18.         End Get
  19.         Set(ByVal value As Font)
  20.             _font = value
  21.         End Set
  22.     End Property
  23. #End Region
  24. #Region " Class Constructors "
  25.     Public Sub New()
  26.         MyBase.New()
  27.         _text = String.Empty
  28.     End Sub
  29.     Public Sub New(ByVal str As String)
  30.         MyBase.New()
  31.         _text = str
  32.     End Sub
  33. #End Region
  34. #Region " onBeginPrint "
  35.     Protected Overrides Sub OnBeginPrint(ByVal e As System.Drawing.Printing.PrintEventArgs)
  36.         MyBase.OnBeginPrint(e)
  37.  
  38.         If (_font Is Nothing) Then
  39.             _font = New Font("Times New Roman", 10)
  40.         End If
  41.     End Sub
  42. #End Region
  43. #Region " onPrintPage"
  44.     Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
  45.         MyBase.OnPrintPage(e)
  46.  
  47.         ' Local variables
  48.         Static curChar As Integer
  49.         Dim printHeight As Integer
  50.         Dim printWidth As Integer
  51.         Dim leftMargin As Integer
  52.         Dim rightMargin As Integer
  53.         Dim lines As Int32
  54.         Dim chars As Int32
  55.  
  56.         ' Print area size and margins
  57.         With MyBase.DefaultPageSettings
  58.             printHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
  59.             printWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
  60.             leftMargin = .Margins.Left
  61.             rightMargin = .Margins.Top
  62.         End With
  63.         If MyBase.DefaultPageSettings.Landscape Then
  64.             Dim tmp As Integer
  65.             tmp = printHeight
  66.             printHeight = printWidth
  67.             printWidth = tmp
  68.         End If
  69.  
  70.         Dim numLines As Int32 = CInt(printHeight / PrinterFont.Height)
  71.         'Create a rectangle printing are for our document
  72.         Dim printArea As New RectangleF(leftMargin, rightMargin, printWidth, printHeight)
  73.         Dim format As New StringFormat(StringFormatFlags.LineLimit)
  74.         'Fit as many characters as we can into the print area    
  75.         e.Graphics.MeasureString(_text.Substring(RemoveZeros(curChar)), PrinterFont, New SizeF(printWidth, printHeight), format, chars, lines)
  76.         'Print the page
  77.         e.Graphics.DrawString(_text.Substring(RemoveZeros(curChar)), PrinterFont, Brushes.Black, printArea, format)
  78.         'Increase current char count
  79.         curChar += chars
  80.         'Detemine if there is more text to print, if
  81.         'there is the tell the printer there is more coming
  82.         If curChar < _text.Length Then
  83.             e.HasMorePages = True
  84.         Else
  85.             e.HasMorePages = False
  86.             curChar = 0
  87.         End If
  88.     End Sub
  89. #End Region
  90. #Region " RemoveZeros "
  91.     ''' Function to replace any zeros in the size to a 1
  92.     ''' Zero's will mess up the printing area
  93.     Public Function RemoveZeros(ByVal value As Integer) As Integer
  94.         'Check the value passed into the function,
  95.         'if the value is a 0 (zero) then return a 1,
  96.         'otherwise return the value passed in
  97.         Select Case value
  98.             Case 0
  99.                 Return 1
  100.             Case Else
  101.                 Return value
  102.         End Select
  103.     End Function
  104. #End Region
  105. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement