[RESOLVED] Printing A New Line While Printing
Hello, everyone! I am setting up printing (to a printer or file) in my application, and it doesn't seem to print out any new lines that I specify. Here's some of my printing code:
Code:
' Loop through all of the text in the array
For i = 0 To 9
If i Mod 2 = 0 Then
TheFont = SpecialFont
Else
TheFont = NormalFont
End If
' Find out how many characters will fit in the printing area
e.Graphics.MeasureString(TextArray(i), TheFont, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, NumCharsFit, NumLinesFilled)
' Print the text on the page
e.Graphics.DrawString(TextArray(i), TheFont, Brushes.Black, rectPrintingArea)
' Add how many characters were printed onto the page
CurrentChar += NumCharsFit
Next
Here are the definitions for the TextArray array:
Code:
Dim TextArray(0 To 9) As String
' Find out the text we're going to print
TextArray(0) = "Order Number: "
TextArray(1) = txtOrderNum.Text & vbCrLf
TextArray(2) = "Order Date: "
TextArray(3) = txtOrderDate.Text & vbCrLf
TextArray(4) = "Order Time: "
TextArray(5) = txtOrderTime.Text & vbCrLf & vbCrLf
TextArray(6) = "Employee that processed the order: "
TextArray(7) = txtEmployee.Text & vbCrLf
TextArray(8) = "Customer: "
TextArray(9) = txtCustomer.Text & vbCrLf
For i = 0 To 9
TextLength += TextArray(i).Length
Next
When I print the output to a PDF file using PDFCreator, it stacks all of the letters onto each other rather than creating new lines at the places I've specified. I've also tried using vbNewLine instead of vbCrLf, but I haven't had any luck. I searched the web for this but couldn't find anything about it. Any help would be greatly appreciated!
Re: Printing A New Line While Printing
Try something like:
vb Code:
'The current location should be initialized to (Left margin, Top Margin)
Dim CurrentLocation As PointF = New PointF(0.0F, 0.0F)
Dim StringSize As SizeF
' Loop through all of the text in the array
For i = 0 To 9
If i Mod 2 = 0 Then
TheFont = SpecialFont
Else
TheFont = NormalFont
End If
' Find out how many graphic units the text occupies.
StringSize = e.Graphics.MeasureString(TextArray(i), TheFont, CInt(PrintAreaWidth), fmt)
' Print the text on the page
e.Graphics.DrawString(TextArray(i), TheFont, Brushes.Black, CurrentLocation)
'Move the current location so the next line fits below the previous.
CurrentLocation.Y += StringSize.Height
Next
You didn't record the measured string and didn't move the location which the string was written on.
DrawString and MeasureString.
Regards
Tom
Re: Printing A New Line While Printing
Quote:
Originally Posted by
ThomasJohnsen
Try something like:
vb Code:
'The current location should be initialized to (Left margin, Top Margin)
Dim CurrentLocation As PointF = New PointF(0.0F, 0.0F)
Dim StringSize As SizeF
' Loop through all of the text in the array
For i = 0 To 9
If i Mod 2 = 0 Then
TheFont = SpecialFont
Else
TheFont = NormalFont
End If
' Find out how many graphic units the text occupies.
StringSize = e.Graphics.MeasureString(TextArray(i), TheFont, CInt(PrintAreaWidth), fmt)
' Print the text on the page
e.Graphics.DrawString(TextArray(i), TheFont, Brushes.Black, CurrentLocation)
'Move the current location so the next line fits below the previous.
CurrentLocation.Y += StringSize.Height
Next
You didn't record the measured string and didn't move the location which the string was written on.
DrawString and
MeasureString.
Regards
Tom
Thank you! That worked very well! :)
Re: Printing A New Line While Printing
Quote:
Originally Posted by
hydrakiller4000
Thank you! That worked very well! :)
(PrintAreaWidth), fmt) is not declared
can I see the exact code and declaration you used?
Re: Printing A New Line While Printing
Quote:
Originally Posted by
hydrakiller4000
Thank you! That worked very well! :)
(PrintAreaWidth), fmt) is not declared
can I see the exact code and declaration you used?
Re: Printing A New Line While Printing
This thread is old, over 8 years.
hydrakiller4000 hasn't been active on the forum in over 7 Years.
Chances are, you are not going to get a response from him, and probably not from ThomasJohnsen who didn't need to know what fmt was to correct the code.
Just select MeastureString in the IDE and hit F1 to get information about the various method Overloads.
If you don't know what you might want fmt to be, then just delete it. There is an overload that doesn't require it, so it technically doesn't matter to you what it was.