|
-
Oct 31st, 2008, 11:22 AM
#1
Thread Starter
Addicted Member
[RESOLVED] [2008]
Hi,
How do I print to the printer text that will be centralized to the width of the page ?
Thanks in advance
-
Oct 31st, 2008, 11:32 AM
#2
Re: [2008]
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2008, 03:09 PM
#3
Re: [2008]
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
-
Oct 31st, 2008, 07:02 PM
#4
Re: [2008]
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:
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
5. Run the code and click the Button.
Tada!
-
Nov 1st, 2008, 02:20 AM
#5
Thread Starter
Addicted Member
Re: [2008]
Hi,
To: "jmcilhinney"
Your code working good.
Thanks to all
-
Nov 1st, 2008, 04:01 AM
#6
Thread Starter
Addicted Member
Re: [2008]
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
-
Nov 1st, 2008, 08:50 AM
#7
Re: [2008]
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?
-
Nov 1st, 2008, 10:25 AM
#8
Thread Starter
Addicted Member
Re: [2008]
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)
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
|