Results 1 to 8 of 8

Thread: [RESOLVED] [2008]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Resolved [RESOLVED] [2008]

    Hi,

    How do I print to the printer text that will be centralized to the width of the page ?

    Thanks in advance

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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?

  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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!
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2008]

    Hi,

    To: "jmcilhinney"
    Your code working good.

    Thanks to all

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    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
  •  



Click Here to Expand Forum to Full Width