|
-
May 23rd, 2013, 08:38 PM
#1
Thread Starter
Fanatic Member
string printing, should be easy but cant figure it out!
Hi All,
I have had code that prints text all lined up line by line with a equal width font, this is fine, until we upgraded and allowed more space for a description.
now the description can be 30 chars long and when it is it prints fine. when it is less than 30 chars it fills the rest with spaces (im using padright for that)
again normally fine, but once it goes over to the next line they stop lining up.
for eg

the first item is padded to equal 30 spaces then we have qty of 1 then its price
the second item has 30 chars of actual letters. the font is equal width so 30 chars and 30 padded chars should be the same no?
please help.
My CodeBank Submissions
- Listbox with transparency and picture support - Click Here
- Check for a true internet connection - Click Here
- Open Cash drawer connected to receipt printer - Click Here
- Custom color and size border around form - Click Here
- Upload file to website without user logins, includes PHP - Click Here
- List All Removable USB Storage Devices - Click Here
- Custom On/Off Slide Control - Click Here
- Insert multiple rows of data into one database table using parameters - Click Here
- Trigger USB/Serial Cash Drawer - Click Here
-
May 23rd, 2013, 09:28 PM
#2
Re: string printing, should be easy but cant figure it out!
Are you saying that the "karly wrap" line has been padded to 30 characters and, unlike the "maxchars..." line is not being wrapped? Can you show us the code that does the printing? This is just spit-balling but I wonder whether text doesn't get wrapped if it's only spaces at the end. Maybe try padding the first line to 29 and adding one extra non-space character and see what you get.
-
May 23rd, 2013, 11:04 PM
#3
Thread Starter
Fanatic Member
Re: string printing, should be easy but cant figure it out!
Hi,
yep thats it, code below is the printing class. i found it online years ago and never really changed much nor know the intricate details (just need something to print text)
the remove zeros function is commented out as it didnt work, it just removed the first and last chars so someone where i found it suggested to comment it out and that worked
Code:
Imports System.Drawing
Imports System.Drawing.Printing
Public Class PCPrint : Inherits Printing.PrintDocument
#Region " Property Variables "
''' <summary>
''' Property variable for the Font the user wishes to use
''' </summary>
''' <remarks></remarks>
Private _font As Font
''' <summary>
''' Property variable for the text to be printed
''' </summary>
''' <remarks></remarks>
Private _text As String
#End Region
#Region " Class Properties "
''' <summary>
''' Property to hold the text that is to be printed
''' </summary>
''' <value></value>
''' <returns>A string</returns>
''' <remarks></remarks>
Public Property TextToPrint() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
''' <summary>
''' Property to hold the font the users wishes to use
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property PrinterFont() As Font
' Allows the user to override the default font
Get
Return _font
End Get
Set(ByVal Value As Font)
_font = Value
End Set
End Property
#End Region
#Region " Class Constructors "
''' <summary>
''' Empty constructor
''' </summary>
''' <remarks></remarks>
Public Sub New()
'Set the file stream
MyBase.New()
'Instantiate out Text property to an empty string
_text = String.Empty
End Sub
''' <summary>
''' Constructor to initialize our printing object
''' and the text it's supposed to be printing
''' </summary>
''' <param name="str">Text that will be printed</param>
''' <remarks></remarks>
Public Sub New(ByVal str As String)
'Set the file stream
MyBase.New()
'Set our Text property value
_text = str
End Sub
#End Region
#Region " OnBeginPrint "
''' <summary>
''' Override the default OnBeginPrint method of the PrintDocument Object
''' </summary>
''' <param name="e"></param>
''' <remarks></remarks>
Protected Overrides Sub OnBeginPrint(ByVal e As Printing.PrintEventArgs)
' Run base code
MyBase.OnBeginPrint(e)
'Check to see if the user provided a font
'if they didnt then we default to Times New Roman
If _font Is Nothing Then
'Create the font we need
_font = New Font("Times New Roman", 12)
End If
End Sub
#End Region
#Region " OnPrintPage "
''' <summary>
''' Override the default OnPrintPage method of the PrintDocument
''' </summary>
''' <param name="e"></param>
''' <remarks>This provides the print logic for our document</remarks>
Protected Overrides Sub OnPrintPage(ByVal e As Printing.PrintPageEventArgs)
' Run base code
MyBase.OnPrintPage(e)
'Declare local variables needed
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
'Set print area size and margins
With MyBase.DefaultPageSettings
printHeight = .PaperSize.Height - 2 - .Margins.Bottom
printWidth = 300 '.PaperSize.Width - .Margins.Left - .Margins.Right
leftMargin = 4 '.Margins.Left 'X
rightMargin = 4 '.Margins.Top 'Y
End With
'Check if the user selected to print in Landscape mode
'if they did then we need to swap height/width parameters
If MyBase.DefaultPageSettings.Landscape Then
Dim tmp As Integer
tmp = printHeight
printHeight = printWidth
printWidth = tmp
End If
'Now we need to determine the total number of lines
'we're going to be printing
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)
'Use the StringFormat class for the text layout of our document
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 "
''' <summary>
''' Function to replace any zeros in the size to a 1
''' Zero's will mess up the printing area
''' </summary>
''' <param name="value">Value to check</param>
''' <returns></returns>
''' <remarks></remarks>
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
My CodeBank Submissions
- Listbox with transparency and picture support - Click Here
- Check for a true internet connection - Click Here
- Open Cash drawer connected to receipt printer - Click Here
- Custom color and size border around form - Click Here
- Upload file to website without user logins, includes PHP - Click Here
- List All Removable USB Storage Devices - Click Here
- Custom On/Off Slide Control - Click Here
- Insert multiple rows of data into one database table using parameters - Click Here
- Trigger USB/Serial Cash Drawer - Click Here
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|