visualbasicuser
Newcomer
- Joined
- Jun 17, 2003
- Messages
- 7
I am struggling a bit with printing reports in .NET. VB6 was easy in comparison. I have a database search application that should print out the results of a search typed in by the user. The results are in the form of a text file I generated. Here is the code I use to create the text file. The printheader routine is called to create the header for the page and the printdetail is call repeatedly, once for each match that is made in the database:
Public Sub printheader()
xr_temp = "CROSS REFERENCE FACILITY "
xr_temp = xr_temp & "Printed " & DateString & " at " & TimeString
PrintLine(3, xr_temp)
xr_temp = "-----------------------------------------------------------"
xr_temp = xr_temp & "------------------------------"
PrintLine(3, xr_temp)
PrintLine(3, " ")
End Sub
Public Sub printdetail(ByVal xr_printstring As String, ByVal xr_compstring As String)
xr_temp = "Search Field : " & xr_compstring
PrintLine(3, xr_temp)
xr_temp = "AGCS Part Number : " & Mid(xr_printstring, 18, 12)
PrintLine(3, xr_temp)
xr_temp = "Vendor : " & Mid(xr_printstring, 33, 15)
PrintLine(3, xr_temp)
xr_temp = "Vendor Part Number : " & Mid(xr_printstring, 63, 30)
PrintLine(3, xr_temp)
xr_temp = "Description : " & Mid(xr_printstring, 147, 39)
PrintLine(3, xr_temp)
xr_temp = "Blacklist Code : " & Mid(xr_printstring, 109, 6)
PrintLine(3, xr_temp)
xr_temp = "Comments : " & Mid(xr_printstring, 191, 58)
PrintLine(3, xr_temp)
xr_temp = "Package : " & Mid(xr_printstring, 97, 12)
PrintLine(3, xr_temp)
xr_temp = "Mask : " & Mid(xr_printstring, 121, 10)
PrintLine(3, xr_temp)
xr_temp = "Engineer : " & Mid(xr_printstring, 249, 3)
PrintLine(3, xr_temp)
xr_temp = "Part Marking : " & Mid(xr_printstring, 257, 30)
PrintLine(3, xr_temp)
xr_temp = "Packaging Method : " & Mid(xr_printstring, 287, 13)
PrintLine(3, xr_temp)
xr_temp = "Moisture Sensitivity : " & Mid(xr_printstring, 300, 13)
PrintLine(3, xr_temp)
xr_temp = "Tray Number : " & Mid(xr_printstring, 313, 30)
PrintLine(3, xr_temp)
PrintLine(3, " ")
PrintLine(3, vbFormFeed)
End Sub
After I have created and closed the text file, I call the printing() routine to print the contents of the file. The problem is that I would like to print the header on the top of each page, followed by the data. Is there any way to force a Form Feed, in the middle of a print job, to do something like this ? I already thought of creating multiple files, each file representing a page, but that is not acceptable. On my network you get a Print Completed window that pops up each time a print job finishes. If I were to print out 50 pages (1 file per page) that would mean I would have to close out 50 of these windows. Not something I feel like doing every time a report is printed. I have already tried adding both chr(12) and VbFormFeed and they just end up printing as funny-looking characters . They dont even function as form feeds. Here are my current print routines:
' The PrintPage event is raised for each page to be printed.
Public Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics)
' Iterate over the file, printing each line.
While count < linesPerPage
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * PrintFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, PrintFont, Brushes.Black, leftMargin, _
yPos, New StringFormat())
count += 1
End While
' If more lines exist, print another page.
If Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
' Print the file.
Public Sub Printing()
Try
streamToPrint = New StreamReader(filePath)
Try
printfont = New Font("New Courier", 8, FontStyle.Regular)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
' Print the document.
PrintDialog1.Document = pd
pd.Print()
Finally
streamtoprint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'Printing
Is there any way to force a form feed in the middle of a print? Anyone have any ideas?
Thanks,
Visualbasicuser
Public Sub printheader()
xr_temp = "CROSS REFERENCE FACILITY "
xr_temp = xr_temp & "Printed " & DateString & " at " & TimeString
PrintLine(3, xr_temp)
xr_temp = "-----------------------------------------------------------"
xr_temp = xr_temp & "------------------------------"
PrintLine(3, xr_temp)
PrintLine(3, " ")
End Sub
Public Sub printdetail(ByVal xr_printstring As String, ByVal xr_compstring As String)
xr_temp = "Search Field : " & xr_compstring
PrintLine(3, xr_temp)
xr_temp = "AGCS Part Number : " & Mid(xr_printstring, 18, 12)
PrintLine(3, xr_temp)
xr_temp = "Vendor : " & Mid(xr_printstring, 33, 15)
PrintLine(3, xr_temp)
xr_temp = "Vendor Part Number : " & Mid(xr_printstring, 63, 30)
PrintLine(3, xr_temp)
xr_temp = "Description : " & Mid(xr_printstring, 147, 39)
PrintLine(3, xr_temp)
xr_temp = "Blacklist Code : " & Mid(xr_printstring, 109, 6)
PrintLine(3, xr_temp)
xr_temp = "Comments : " & Mid(xr_printstring, 191, 58)
PrintLine(3, xr_temp)
xr_temp = "Package : " & Mid(xr_printstring, 97, 12)
PrintLine(3, xr_temp)
xr_temp = "Mask : " & Mid(xr_printstring, 121, 10)
PrintLine(3, xr_temp)
xr_temp = "Engineer : " & Mid(xr_printstring, 249, 3)
PrintLine(3, xr_temp)
xr_temp = "Part Marking : " & Mid(xr_printstring, 257, 30)
PrintLine(3, xr_temp)
xr_temp = "Packaging Method : " & Mid(xr_printstring, 287, 13)
PrintLine(3, xr_temp)
xr_temp = "Moisture Sensitivity : " & Mid(xr_printstring, 300, 13)
PrintLine(3, xr_temp)
xr_temp = "Tray Number : " & Mid(xr_printstring, 313, 30)
PrintLine(3, xr_temp)
PrintLine(3, " ")
PrintLine(3, vbFormFeed)
End Sub
After I have created and closed the text file, I call the printing() routine to print the contents of the file. The problem is that I would like to print the header on the top of each page, followed by the data. Is there any way to force a Form Feed, in the middle of a print job, to do something like this ? I already thought of creating multiple files, each file representing a page, but that is not acceptable. On my network you get a Print Completed window that pops up each time a print job finishes. If I were to print out 50 pages (1 file per page) that would mean I would have to close out 50 of these windows. Not something I feel like doing every time a report is printed. I have already tried adding both chr(12) and VbFormFeed and they just end up printing as funny-looking characters . They dont even function as form feeds. Here are my current print routines:
' The PrintPage event is raised for each page to be printed.
Public Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics)
' Iterate over the file, printing each line.
While count < linesPerPage
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * PrintFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, PrintFont, Brushes.Black, leftMargin, _
yPos, New StringFormat())
count += 1
End While
' If more lines exist, print another page.
If Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
' Print the file.
Public Sub Printing()
Try
streamToPrint = New StreamReader(filePath)
Try
printfont = New Font("New Courier", 8, FontStyle.Regular)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
' Print the document.
PrintDialog1.Document = pd
pd.Print()
Finally
streamtoprint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'Printing
Is there any way to force a form feed in the middle of a print? Anyone have any ideas?
Thanks,
Visualbasicuser