Hi,
How do I print to the printer text that will be centralized to the width of the page ?
Thanks in advance
Printable View
Hi,
How do I print to the printer text that will be centralized to the width of the page ?
Thanks in advance
you need to use descriptive titles for your threads. it'll increase your chances of getting answered. [2008] doesn't tell us much.
can you post your code?
Hi,
Here's a link how to print the content of a richtextbox;
http://support.microsoft.com/kb/811401
It's written for vb 2005, but must be the same for vb 2008.
Hope it's a start,
sparrow1
You call DrawString in the PrintPage event handler and specify a StringFormat value that centres the text. Here's a very simple example:
1. Create a new WinForms project.
2. Add a Button, a PrintDocument and a PrintPreviewDialog.
3. Assign the PrintDocument to the PrintPreviewDialog's Document property.
4. Add this code:5. Run the code and click the Button.Code:Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
Me.PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
ByVal e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim text As String = "Hello World"
Dim font As Font = Me.Font
Dim brush As Brush = Brushes.Black
Dim marginBounds As Rectangle = e.MarginBounds
Dim format As New StringFormat With {.Alignment = StringAlignment.Center}
Dim textSize As SizeF = e.Graphics.MeasureString(text, font, marginBounds.Size, format)
Dim textBounds As New Rectangle(marginBounds.X, marginBounds.Y, marginBounds.Width, CInt(textSize.Height))
e.Graphics.DrawRectangle(Pens.Red, textBounds)
e.Graphics.DrawString("Hello World", Me.Font, Brushes.Black, textBounds, format)
End Sub
Tada!
Hi,
To: "jmcilhinney"
Your code working good.
Thanks to all
Hi,
I tried the next code and it is not printing in the center.
Can you tell me whats wrong ?
Code:Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
Dim MyText As String = "Hello World"
Dim MyFont As New Font("miriam fixed", 11, FontStyle.Regular)
Dim YLocation As Integer = 100
'Dim TextLength As Integer = Me.CreateGraphics.MeasureString(MyText, MyFont).Width
Dim TextLength As Integer = CreateGraphics.MeasureString(MyText, MyFont).Width
Dim MarginBoundsWidth As Integer = e.MarginBounds.Width - TextLength
Dim XLocation As Integer = MarginBoundsWidth / 2
e.Graphics.DrawString(MyText, MyFont, Brushes.Black, XLocation, YLocation)
End Sub
First up, why are you calling CreateGraphics when you already have a Graphics object available? Not only is it unnecessary, the way you're doing it is very bad because you can't dispose it when you're finished with it.
As for the problem, it's caused by the fact that your XLocation is half the width of the margin bounds less half the width of the text, but you draw the text that distance from the edge of the page instead of from the left margin. Why use the margin in your calculation at all when you're positioning the text relative to the page?
Hi,
This is my new code and thank you all.
Code:Dim MyText As String = "Hello World"
Dim MyFont As New Font("miriam fixed", 11, FontStyle.Regular)
Dim YLocation As Integer = 100
Dim TextLength As Integer = e.Graphics.MeasureString(MyText, MyFont).Width
Dim MarginBoundsWidth As Integer = e.Graphics.VisibleClipBounds.Width - TextLength
Dim XLocation As Integer = MarginBoundsWidth / 2
e.Graphics.DrawString(MyText, MyFont, Brushes.Black, XLocation, YLocation)