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
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