Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
my problem is when my press the print button the data which is present in textbox will be printed .But when the receipt is printed few words are getting more space in between those the words while comparing other with other words.....
I mean the order of getting items from textbox to print page is changed like some words are talking more spaces in between them...

What I have tried:

this is my code for for printing
VB
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)


this is the code to call print
VB
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  PrintPreviewDialog1.ShowDialog()


please help me .....its urgent. please
Posted
Updated 13-Mar-18 9:25am
v7
Comments
Maciej Los 13-Mar-18 13:37pm    
Urgent - for who?
Richard MacCutchan 13-Mar-18 13:46pm    
You need to check which font is being used for the printing. And also do not use fixed values for the location, calculate where to print the text based on font and string sizes.

We can't see what is happening - so we can't even begin to five you a solution. So, it's going to be up to you.

Start by using the debugger to look at exactly what is in the TextBox.Text - is it single line, is it multiline, what text does it contain?
Then look at where the "extra spaces" are: are they in specific places in the sentence? Or specific places in the output?
Does it only happen with print preview? What does the actual printout look like? What happens when you zoom in the printout? Does the spacing change?

My guess is that it's a receipt print so it has a narrow paper width - and when you figure in any margins the text gets "wrapped" to new lines when you aren't expecting it; which shows up as "extra spacing" you weren't looking for. If so, there isn't anything you can do about that, other than fix the size of your text box to the space available on the paper.

But TBH, just filling a TextBox with text and printing it probably isn't the best way to do a receipt - you should consider doing "proper formatting" of the actual print - it's very rare to see a receipt that just has same sized text throughout, without any bold, logo, small address detail, or even a "tracking" barcode - none of which can be included in a standard TextBox.
 
Share this answer
 
I have seen the same issue while printing using the default font (Microsoft Sans Serif). If you can use TTF fonts like Arial this issue may not be there.

You can try some thing like

Private textToPrint As String
Private printFont As Font

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    textToPrint = TextBox1.Text
    printFont = New Font("Arial", 10)

    PrintPreviewDialog1.ShowDialog()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim charsOnPage As Integer = 0
    Dim linesPerPage As Integer = 0

    e.Graphics.MeasureString(textToPrint, printFont, e.MarginBounds.Size, StringFormat.GenericTypographic, charsOnPage, linesPerPage)

    e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic)

    textToPrint = textToPrint.Substring(charsOnPage)

    e.HasMorePages = (textToPrint.Length > 0)
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900