Results 1 to 6 of 6

Thread: [RESOLVED] [2008] Print In Center Of Page

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Resolved [RESOLVED] [2008] Print In Center Of Page

    I'm attempting to print in the center of the page - but I think VB6 is stuck in my brain. Can someone tell me what's wrong with the following code? It's printing off the left side of the page, and only showing the last few words of the title.

    Code:
        Private Sub doc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles doc.PrintPage
            Dim pnt As PointF
            Dim f As Font
    
            f = New Font("Times New Roman", 14)
            doc.DocumentName = "Open-Shelf/Container Rescan Form"
            e.Graphics.PageUnit = GraphicsUnit.Pixel
            e.Graphics.DrawImage(My.Resources.Retrievex, 500, 500, 1387, 330)
    
            'What's wrong with these next 3 lines?
            pnt.X = (e.PageBounds.Width / 2) - (e.Graphics.MeasureString(doc.DocumentName, f).Width / 2)
            pnt.Y = 1000
            e.Graphics.DrawString(doc.DocumentName, f, Brushes.Black, pnt)
    
        End Sub

  2. #2
    Junior Member Crane101's Avatar
    Join Date
    Feb 2005
    Location
    Neath, Wales, UK
    Posts
    22

    Re: [2008] Print In Center Of Page

    Ok, I ran a few tests and found the following:

    The problem lies with changing the Graphics.PageUnit. The PageBounds.Width remains constant whatever you set the PageUnit to.

    To convert to pixels, you need to multiply the width by 6 (or 0.72 for points). So you get:

    e.PageBounds.Width * 6 / 2

    or

    e.PageBounds.Width * 3

    Admittedly I have never needed to change the PageUnit before, this is only what I've found by trial and error. It's quite possible that someone wiser than me can advise of a way to change the unit that PageBounds.Width is returned in, so you don't have to bother with this adjustment.

    Shane

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: [2008] Print In Center Of Page

    Thank you, Shane. I had actually already realized my error (hence the thread marked 'resolved'). One question tho:

    To convert to pixels, you need to multiply the width by 6
    What are you converting to pixels there?

  4. #4
    Junior Member Crane101's Avatar
    Join Date
    Feb 2005
    Location
    Neath, Wales, UK
    Posts
    22

    Re: [2008] Print In Center Of Page

    Doh! I really should learn to read the thread properly first...

    Anyway, I'm converting the value of PageBounds.Width into pixels in the line

    pnt.X = (e.PageBounds.Width * 6 / 2) - (e.Graphics.MeasureString(doc.DocumentName, f).Width / 2)
    The value for MeasureString does change depending on the PageUnit setting, so it doesn't need a similar conversion.

    Did you just get rid of the line that altered the PageUnit setting, or does it need to be there?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: [2008] Print In Center Of Page

    Did you just get rid of the line that altered the PageUnit setting, or does it need to be there?
    I made quite a mess of it and changed it to:

    Code:
        Private Sub doc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles doc.PrintPage
            Dim pnt As PointF
            Dim f As Font
    
            f = New Font("Comic Sans MS", 16, FontStyle.Bold)
            doc.DocumentName = "Open-Shelf/Container Rescan Form"
            e.Graphics.PageUnit = GraphicsUnit.Pixel
            e.Graphics.DrawImage(My.Resources.Retrievex, 250, 250, 1387, 330)
    
            e.Graphics.PageUnit = GraphicsUnit.Inch
            pnt.X = ((e.PageSettings.PaperSize.Width / 200) - (e.Graphics.MeasureString(doc.DocumentName, f).Width / 2))
            pnt.Y = 1
            e.Graphics.DrawString(doc.DocumentName, f, Brushes.Black, pnt)
    
        End Sub
    I changed to PaperSize.Width (is there a difference between that and PageBounds.Width?), and changed that from hundredths of an inch to inches.

    What is the default measurement of PageBounds.Width?

  6. #6
    Junior Member Crane101's Avatar
    Join Date
    Feb 2005
    Location
    Neath, Wales, UK
    Posts
    22

    Re: [2008] Print In Center Of Page

    hey, whatever works

    There doesn't appear to be any difference between the widths of PaperSize and PageBounds.

    The default measurement for PageBounds.Width depends on the display medium; hence the default unit of measurement is display.

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