locked
VB.Net 2008 PrintDocument Issue - Preview and printed output different

    Question

  • I'm new to PrintDocument.  I have the following code that displays my output to a PrintPreviewDialog.  The output is perfect on the preview, but when I print it to my printer, it is pushed to the left.  I suspect that the issue has something to do with the physical printer margins, but I'm not sure.  Any suggestions would be appreciated.

     

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Create a PrintDocument and attach it to PrintPreview dialog

    ppdGameForm.Document = PreparePrintDocument()

    'Preview the PrintDocument

    'Anti-aliasing removes jagged lines when fonts are rendered, but slows down rendering

    ppdGameForm.UseAntiAlias = True

    ppdGameForm.WindowState = FormWindowState.Maximized

    ppdGameForm.ShowDialog()

    End Sub

     

    Private Function PreparePrintDocument() As Printing.PrintDocument

    ' Make the PrintDocument object.

    Dim objPrintDocument As New Printing.PrintDocument

    objPrintDocument.DocumentName = "Shot Chart"

    ' Install the PrintPage event handler.

    AddHandler objPrintDocument.PrintPage, AddressOf Print_PrintPage

    ' Return the object.

    Return objPrintDocument

    End Function

     

    Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    Using objGraphics As Graphics = e.Graphics

    Dim iXAdjustment As Integer = 0

    Dim iYAdjustment As Integer = 0

    Dim strHeaderTitle As String = ""

    Dim strPeriodHeader As String = ""

    Dim objRectangle As Rectangle

    Dim objPen As New Pen(Color:=Color.Black, Width:=3)

    Dim objFont As Font

    Dim stringFormat As New StringFormat()

    'Align text Center/Top

    stringFormat.Alignment = StringAlignment.Center

    stringFormat.LineAlignment = StringAlignment.Center

    'Create Document Header

    objFont = New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point)

    objRectangle = New Rectangle(x:=0, y:=30, Width:=850, Height:=30)

    'Draw the Header

    If cboLocation.Text = "Away" Then

    strHeaderTitle = mstrOpponentTeamNickname & " vs " & mstrOurTeamNickname & " Shot Chart"

    Else

    strHeaderTitle = mstrOurTeamNickname & " vs " & mstrOpponentTeamNickname & " Shot Chart"

    End If

    objGraphics.DrawString(mstrOurTeamNickname & " Shot Chart", objFont, Brushes.Black, objRectangle, stringFormat)

    'Second and third header lines

    objFont = New Font("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Point)

    objRectangle = New Rectangle(x:=0, y:=60, Width:=850, Height:=40)

    If cboLocation.Text = "Away" Then

    strHeaderTitle = mstrOpponentTeamNickname & " vs " & mstrOurTeamNickname

    Else

    strHeaderTitle = mstrOurTeamNickname & " vs " & mstrOpponentTeamNickname

    End If

    strHeaderTitle = strHeaderTitle & vbCrLf & dtpGameDate.Value

    objGraphics.DrawString(strHeaderTitle, objFont, Brushes.Black, objRectangle, stringFormat)

    For I As Integer = 1 To 4

    'Draw a box around 1/2 court

    If I = 1 Then

    iYAdjustment = 120

    Else

    iYAdjustment = ((I - 1) * 220) + 120 '100 is placement of first print

    End If

    iXAdjustment = 45

    'Draw court outline

    objRectangle = New Rectangle(x:=iXAdjustment, y:=iYAdjustment, Width:=380, Height:=220)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Draw stat box outline

    objRectangle = New Rectangle(x:=iXAdjustment + 380, y:=iYAdjustment, Width:=380, Height:=220)

    'Draw the Period Title

    strPeriodHeader = "Period " & I

    'stringFormat.LineAlignment = StringAlignment.Near

    objFont = New Font("Tahoma", 12, FontStyle.Underline, GraphicsUnit.Point)

    objGraphics.DrawString(strPeriodHeader, objFont, Brushes.Black, objRectangle, stringFormat)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Draw the top of the key

    objRectangle = New Rectangle(x:=iXAdjustment + 137, y:=iYAdjustment + 52, Width:=100, Height:=100)

    objGraphics.DrawPie(pen:=objPen, rect:=objRectangle, _

    startAngle:=0, sweepAngle:=180)

    'DRAW THE PAINT

    'Draw the 3 second area

    objRectangle = New Rectangle(x:=iXAdjustment + 137, y:=iYAdjustment, Width:=100, Height:=102)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Left Block

    objPen.Width = 3

    objRectangle = New Rectangle(x:=iXAdjustment + 126, y:=iYAdjustment + 30, Width:=8, Height:=3)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Right Block

    objRectangle = New Rectangle(x:=iXAdjustment + 240, y:=iYAdjustment + 30, Width:=8, Height:=3)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    objPen.Width = 1

    'Left 1st thin

    objRectangle = New Rectangle(x:=iXAdjustment + 125, y:=iYAdjustment + 47, Width:=10, Height:=1)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Right 1st thin

    objRectangle = New Rectangle(x:=iXAdjustment + 239, y:=iYAdjustment + 47, Width:=10, Height:=1)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Left 2nd thin

    objRectangle = New Rectangle(x:=iXAdjustment + 125, y:=iYAdjustment + 62, Width:=10, Height:=1)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Right 2nd thin

    objRectangle = New Rectangle(x:=iXAdjustment + 239, y:=iYAdjustment + 62, Width:=10, Height:=1)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Left 3rd thin

    objRectangle = New Rectangle(x:=iXAdjustment + 125, y:=iYAdjustment + 77, Width:=10, Height:=1)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Right 3rd thin

    objRectangle = New Rectangle(x:=iXAdjustment + 239, y:=iYAdjustment + 77, Width:=10, Height:=1)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    objPen.Width = 3

    'Draw the 3 point line, High School

    objRectangle = New Rectangle(x:=iXAdjustment + 32, y:=iYAdjustment - 155, Width:=310, Height:=310)

    objGraphics.DrawPie(pen:=objPen, rect:=objRectangle, _

    startAngle:=0, sweepAngle:=180)

    'DRAW THE BASKETBALL GOAL

    'Draw the backboard

    objRectangle = New Rectangle(x:=iXAdjustment + 167, y:=iYAdjustment + 7, Width:=40, Height:=1)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Draw the back of the rim

    objRectangle = New Rectangle(x:=iXAdjustment + 184, y:=iYAdjustment + 10, Width:=6, Height:=3)

    objGraphics.DrawRectangle(pen:=objPen, rect:=objRectangle)

    'Draw the rim

    objGraphics.DrawEllipse(pen:=New Pen(Color.Black, Width:=2), _

    rect:=New Rectangle(x:=iXAdjustment + 178, y:=iYAdjustment + 13, Width:=18, Height:=18))

    Next

    'Create Document Footer

    objFont = New Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point)

    objRectangle = New Rectangle(x:=0, y:=1055, Width:=e.PageBounds.Right, Height:=20)

    'Draw the Header

    objGraphics.DrawString("Page 1", objFont, Brushes.Black, objRectangle, stringFormat)

    End Using

     

    e.HasMorePages = False

    End Sub

    Thursday, March 20, 2008 5:07 PM

All replies

  • All of the code that I see here looks fine, the only thing that I can think of is that I don't see that you have set the PrintDocument Margins anywhere.  Is the space on the left of the graphics equal to the space on the right?  If so you probably need to widen you margins.

     

    This looks like a very interesting project, some kind of stats display for basketball?

    Thursday, March 20, 2008 5:33 PM
  • In my PreparePrintDocument code, I have set the margins as shown below, but I still have the same problem.  My output is centered perfectly on my preview, but is offset too far to the right in the printed output.  If anyone would care to copy this code into a blank project and try it out I would appreciate it. I (think I) removed all variables so that is should work as is with just a button and printpreviewdocument control.

     

    Yep, this is going to (eventually) print out a shot chart for basketball from stats taken during a game...I couldn't figure out how to do it in Crystal Reports, now this is driving me crazy! lol

     

    Private Function PreparePrintDocument() As PrintDocument

    ' Make the PrintDocument object.

    Dim objPrintDocument As New PrintDocument

    Dim newMargins As System.Drawing.Printing.Margins

    ''Margins(Left, Right, Top, Bottom)

    newMargins = New System.Drawing.Printing.Margins(45, 45, 50, 50)

    objPrintDocument.DocumentName = "Shot Chart"

    objPrintDocument.DefaultPageSettings.Margins = newMargins

    ' Install the PrintPage event handler.

    AddHandler objPrintDocument.PrintPage, AddressOf Print_PrintPage

    ' Return the object.

    Return objPrintDocument

    End Function

    Thursday, March 20, 2008 5:42 PM
  • Interesting note.  If I set

    objPrintDocument.OriginAtMargins = True

    in my PerparePrintDocument code, the print preview looks exactly like the printed output.
    Thursday, March 20, 2008 7:23 PM
  • At the risk of talking to myself here  , I just discovered something else.

     

    If I set the margins to all 0, and then set OriginAtMargins = True, the PrintPreview is incorrect, but the printed output is perfect.  Is this a bug or do I just have something goofy in my code?

     

    Thursday, March 20, 2008 7:35 PM
  •  

    I'll give you a little piece of mind, it dosen't work for me either.  I'll kep looking into it though.

     

    The graphics look great though.

    Thursday, March 20, 2008 9:50 PM
  • "If I set the margins to all 0, and then set OriginAtMargins = True, the PrintPreview is incorrect, but the printed output is perfect.  Is this a bug or do I just have something goofy in my code?"

    Sort of a bug.  The PrintPreview is off by the HardMargins.  Acceptable for most printers. 

     

    Thursday, March 20, 2008 10:58 PM
  • I can't see that as being intentional.  The hard margins on most printers could really throw off a preview.

    Thursday, March 20, 2008 11:19 PM