Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class PCPrint : Inherits Printing.PrintDocument
- #Region " Property Variables "
- Private _font As Font
- Private _text As String
- #End Region
- #Region " Class Properties "
- Public Property TextToPrint() As String
- Get
- Return _text
- End Get
- Set(ByVal value As String)
- _text = value
- End Set
- End Property
- Public Property PrinterFont() As Font
- Get
- Return _font
- End Get
- Set(ByVal value As Font)
- _font = value
- End Set
- End Property
- #End Region
- #Region " Class Constructors "
- Public Sub New()
- MyBase.New()
- _text = String.Empty
- End Sub
- Public Sub New(ByVal str As String)
- MyBase.New()
- _text = str
- End Sub
- #End Region
- #Region " onBeginPrint "
- Protected Overrides Sub OnBeginPrint(ByVal e As System.Drawing.Printing.PrintEventArgs)
- MyBase.OnBeginPrint(e)
- If (_font Is Nothing) Then
- _font = New Font("Times New Roman", 10)
- End If
- End Sub
- #End Region
- #Region " onPrintPage"
- Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
- MyBase.OnPrintPage(e)
- ' Local variables
- Static curChar As Integer
- Dim printHeight As Integer
- Dim printWidth As Integer
- Dim leftMargin As Integer
- Dim rightMargin As Integer
- Dim lines As Int32
- Dim chars As Int32
- ' Print area size and margins
- With MyBase.DefaultPageSettings
- printHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
- printWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
- leftMargin = .Margins.Left
- rightMargin = .Margins.Top
- End With
- If MyBase.DefaultPageSettings.Landscape Then
- Dim tmp As Integer
- tmp = printHeight
- printHeight = printWidth
- printWidth = tmp
- End If
- Dim numLines As Int32 = CInt(printHeight / PrinterFont.Height)
- 'Create a rectangle printing are for our document
- Dim printArea As New RectangleF(leftMargin, rightMargin, printWidth, printHeight)
- Dim format As New StringFormat(StringFormatFlags.LineLimit)
- 'Fit as many characters as we can into the print area
- e.Graphics.MeasureString(_text.Substring(RemoveZeros(curChar)), PrinterFont, New SizeF(printWidth, printHeight), format, chars, lines)
- 'Print the page
- e.Graphics.DrawString(_text.Substring(RemoveZeros(curChar)), PrinterFont, Brushes.Black, printArea, format)
- 'Increase current char count
- curChar += chars
- 'Detemine if there is more text to print, if
- 'there is the tell the printer there is more coming
- If curChar < _text.Length Then
- e.HasMorePages = True
- Else
- e.HasMorePages = False
- curChar = 0
- End If
- End Sub
- #End Region
- #Region " RemoveZeros "
- ''' Function to replace any zeros in the size to a 1
- ''' Zero's will mess up the printing area
- Public Function RemoveZeros(ByVal value As Integer) As Integer
- 'Check the value passed into the function,
- 'if the value is a 0 (zero) then return a 1,
- 'otherwise return the value passed in
- Select Case value
- Case 0
- Return 1
- Case Else
- Return value
- End Select
- End Function
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement