HELP: Any Printing Guru's? (PrintPreview OK...PrintDoc... NOT!)

M

Mr. B

This is driving me NUTZ!!! I've been screwing around on this for a week now.
And I have tried to find examples similar to what I have (nada). Got lots of
streaming a TXT file... bah!

I am really stuck here (probably the ol' Not seeing the tree cause of the
forest thingy). But I just can not get this to work.

The following is my Test example code. Just needs a Button1 on a blank Form.
Add in PrintDocument1, PrintPreviewDialog1, PageSetupDialog1... and run it.

It does everything that I want it to... EXCEPT print! I get the multiple page
preview. My formating is okay. But all it prints is a Single Blank page with
my "Weeks Total Hours: " on the page... Why??? Why can I preview OK... but
not print?


MY Test CODE (watch for word wrap):

Imports System.IO
Imports System.Drawing.Printing
' *********************************************************************
' Print Out Variables
Dim itm, rowTtl As Integer
Dim rwCntr As Integer ' Print Rows Counter
Dim pageNo As Integer ' Page Number
Dim PtrCtr As Integer
Dim tableFont, tableFont1 As Font
Dim X1, X2, X3 As Integer
Dim W1, W2, W3 As Integer
Dim Y As Integer
Dim lbWeekItems As Integer = 30
' *********************************************************************

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

Dim pageWidth As Integer
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
PageSetupDialog1.AllowOrientation = False
PrintDocument1.DefaultPageSettings.Margins.Top = 75
PrintDocument1.DefaultPageSettings.Margins.Bottom = 50
PrintDocument1.DefaultPageSettings.Margins.Left = 50
PrintDocument1.DefaultPageSettings.Margins.Right = 50
PrintDocument1.DefaultPageSettings.Landscape = True

tableFont = New Font("Arial", 10)
tableFont1 = New Font("Arial", 9)

With PrintDocument1.DefaultPageSettings
pageWidth = .PaperSize.Height - _
.Margins.Left - _
.Margins.Right
' Measured in 1/100ths of an Inch
End With

PtrCtr = 0
rwCntr = 0
pageNo = 0

' X's are coordinates of the left edge of the columns
' W's are the widths of the columns
W1 = 75
W2 = 250
W3 = 40
X1 = 50
X2 = 410
X3 = 720

Try
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch exc As Exception
MsgBox("Print Operation Failed... " & vbCrLf & exc.Message)
End Try

End Sub
' *********************************************************************
Protected Sub PrintDocHeader(ByVal e As
System.Drawing.Printing.PrintPageEventArgs)
Dim str As String

Y = PrintDocument1.DefaultPageSettings.Margins.Top
str = "Time Sheet Header"
e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y)
Y = Y + 25
e.Graphics.DrawString("Job Information", tableFont, Brushes.Black, X1, Y)
e.Graphics.DrawString("Cost Code Information", tableFont, Brushes.Black,
X2, Y)
e.Graphics.DrawString("Sat", tableFont, Brushes.Black, X3, Y)
e.Graphics.DrawString("Sun", tableFont, Brushes.Black, X3 + 40, Y)
e.Graphics.DrawString("Mon", tableFont, Brushes.Black, X3 + 80, Y)
e.Graphics.DrawString("Tue", tableFont, Brushes.Black, X3 + 120, Y)
e.Graphics.DrawString("Wed", tableFont, Brushes.Black, X3 + 160, Y)
e.Graphics.DrawString("Thr", tableFont, Brushes.Black, X3 + 205, Y)
e.Graphics.DrawString("Fri", tableFont, Brushes.Black, X3 + 245, Y)
e.Graphics.DrawString("Total", tableFont, Brushes.Black, X3 + 283, Y)
Y = Y + 20
' Draw Break Lines
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawLine(Pens.Black, X1, Y, .PaperSize.Height - 50, Y)
e.Graphics.DrawLine(Pens.Black, X1, Y + 2, .PaperSize.Height - 50, Y +
2)
End With
Y = Y + 4

End Sub
' *********************************************************************
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

While PtrCtr < lbWeekItems
Dim str As String
rwCntr = rwCntr + 1

' Print Page Header
If rwCntr = 1 Then
PrintDocHeader(e)
' Print Page Number
pageNo = pageNo + 1
e.Graphics.DrawString("Page " & pageNo, tableFont, Brushes.Black,
1000, 800)
End If

Dim R1 As New RectangleF(X1, Y, W1, 80) ' Col 1 Job
Dim R2 As New RectangleF(X1 + 80, Y, W2, 80) ' Col 2 Job Desc
Dim R3 As New RectangleF(X2, Y, W1, 80) ' Col 3 cCode
Dim R4 As New RectangleF(X2 + 60, Y, W2, 80) ' Col 4 cCode Desc
Dim R5 As New RectangleF(X3, Y, W3, 80) ' Col 5 Sat
Dim R6 As New RectangleF(X3 + 40, Y, W3, 80) ' Col 6
Dim R7 As New RectangleF(X3 + 80, Y, W3, 80) ' Col 7
Dim R8 As New RectangleF(X3 + 120, Y, W3, 80) ' Col 8 Tue
Dim R9 As New RectangleF(X3 + 160, Y, W3, 80) ' Col 9
Dim R10 As New RectangleF(X3 + 205, Y, W3, 80) ' Col 10
Dim R11 As New RectangleF(X3 + 245, Y, W3, 80) ' Col 11 Fri
Dim R12 As New RectangleF(X3 + 285, Y, W3, 80) ' Col 12 Total
Dim R13 As New RectangleF(X1, Y + 17, 1050, 80) ' Col 13 cCode Desc

str = "Item"
e.Graphics.DrawString(str, tableFont, Brushes.Black, R1)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R2)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R3)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R4)

' Week Day Hours
str = lbWeekItems
e.Graphics.DrawString(str, tableFont, Brushes.Black, R5)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R6)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R7)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R8)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R9)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R10)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R11)
e.Graphics.DrawString("22", tableFont, Brushes.Black, R12)
Y = Y + 15

str = "Line Item: " & lbWeekItems
If Len(str) <= 165 Then
tableFont1 = New Font("Arial", 9)
Else
tableFont1 = New Font("Arial", 8)
End If
e.Graphics.DrawString(str, tableFont1, Brushes.Black, R13)
' Reset Font
tableFont1 = New Font("Arial", 9)

' Draw Break Line
With PrintDocument1.DefaultPageSettings
Y = Y + 20
e.Graphics.DrawLine(Pens.Black, X1, Y, .PaperSize.Height - 50, Y)
End With

' That's it for this page...
If rwCntr = 18 Then
e.HasMorePages = True
PtrCtr = PtrCtr + 1
rwCntr = 0
Exit Sub
End If
PtrCtr = PtrCtr + 1
End While

' Total Weeks Hours (last page)
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawString("Weeks Total Hours: ", tableFont, Brushes.Black,
875, 775)
e.Graphics.DrawString("44", tableFont, Brushes.Black, 1000, 775)
End With

' No More Pages to Print
e.HasMorePages = False

End Sub
' *********************************************************************

Any kind soul can spot the trouble?

Regards,

Bruce
 
J

Justin Weinberg

I'm able to preview your code OK also. I'm not at a printer at the moment,
so I can't test printed output, but I believe I know what your problem is.

When you invoke "print", the document goes through the onPrintPage series
of events all over again. Since you initialized values in the click event
for your loops, these values are already past their points when you click on
print.

You can verify this without printing by adding a second button with a
preview. In that button, do not initialize your loopers. Click on the
normal button first and you'll see your nice print preview (nice job on
working with PrintDocuments by the way). Click on the second button next,
and you'll see the problem you are reporting.

The solution is to move your loop initializers into the printhandling code.

One more thing, you might want to turn option strict on at the top of your
code. You have an implicit cast from integer to string in your printing
handler.

Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com

<Long code section snipped>
 
J

Justin Weinberg

Slight correction. Use the begin print event for your initializers since
your using them to determine continuation in your PrintPage event.

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
PtrCtr = 0
rwCntr = 0
pageNo = 0
End Sub


--
Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com
 
M

Mr. B

Justin Weinberg said:
Slight correction. Use the begin print event for your initializers since
your using them to determine continuation in your PrintPage event.

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
PtrCtr = 0
rwCntr = 0
pageNo = 0
End Sub


OMG!!!! YES!!! That's got it! THANK YOU THANK YOU THANK YOU THANK YOU...

Damn... I knew I was 'close'... but (belive it or not)... this is my first
attempt at printing. And of course, what I do it pick the worst possible
thing to do... then once I got it, everything is downhill!!

Many Thanks!!!

Oh and the Option Strict On... thanks. Just was testing out my stuff...

Regards,

Bruce
 
M

Mr. B

Justin Weinberg said:
Glad I could help!

Me too :)

While I won't pretend to understand 'why' your suggestion worked... I'm just
glad it did (grin). I'll have to digest the whole thing some more to get an
understanding on it.

Thanks again...

Regards,

Bruce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top