I've been trying with mixed success to put together a decent print routine to print a series of daily sales and inventory summary reports based off SQL query criteria. After a lot of research and trial and error, I have been able to piece together a somewhat workable subroutine, however there are still a few holes in the hull.
The first issue im having is with font settings.
the line -
printFont = New Font("Currier New", 8)
doesn't really seem to affect how the page prints out at this point -
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
a console.writeline printout of the printFont name reveals this --
It seems like the fontcode being sent to the printer is sans serif instead of currier new. When I print the same file through notepad, I get the desired Currier New font settings. The two documents, original printed through this print routine, and the second printed through notepad, are completely different in terms of look. Any ideas of what is causing the difference in appearance?
The other issue im having is with the form feed / page break codes. For several of the reports I generate, particularly the summary reports, I often need to insert the page break command to separate groups of data. I generate these reports using the StreamWriter object and send the vbFormFeed code when necessary. The problem is that I can not get the printer to recognize the page break code, instead it just prints out a block character. Is there a better way for me to insert the page break trigger?
Here is my currernt new page code -
If bolEndOfSeries = True then
swPRINTReport.WriteLine(vbFormFeed)
End If
Any assistance you can give to help clear these small issues up would be much appreciated, I would really like to be able to close this matter and move on to other things.
Private Function PrintDocument(ByVal printer As String, ByVal file As String) As Integer Dim returnValue As Integer = 0 Try streamToPrint = New StreamReader(file) Try printFont = New Font("Currier New", 8) Dim pd As New PrintDocument AddHandler pd.PrintPage, AddressOf pd_PrintPage pd.PrinterSettings.PrinterName = printer pd.DefaultPageSettings.Landscape = True If pd.PrinterSettings.IsValid Then pd.Print() returnValue = 0 Else MessageBox.Show("Printer is invalid") returnValue = 1 End If Finally streamToPrint.Close() End Try Catch ex As Exception MessageBox.Show(ex.Message) 'returnValue = ex. End Try Return returnValueEnd FunctionPrivate 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 = 25 Dim topMargin As Single = 25 Dim line As String = Nothing ' Calculate the number of lines per page. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) 'linesPerPage = 66 ' Print each line of the file. 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 IfEnd Sub
1) thank you for pointing out the spelling mistake, I feel a little bit dumber now for not noticing that before.
Embarrassment aside, I'm not convinced that your solution to part 2 of my problem is what im looking for. If I bypass the Graphics method, how am I able to control the style of print? For example, setting the print format to landscape and the font to "Courier New"?
x77
No, i do´nt expose any solution on point 2. I explain why control char are´nt interpreted when you draw with graphics.
There are some methods on windows to send a raw file to printer. By sample a saved Spool file.
The Copy method where used when we work on MsDos, now it can not apply depending on printer configuration.
On network printers it works ok, but on Rs232 printers can fail depending on bios configuration.
When you draw on PrintDocument, the Printer Driver generates a stream that Spool sends to the printer.
x77
When We work on MsDos, we add scape codes from Printer manual.
There are many printers and each Report where Printer dependant.
Standard Printer Control chars , allow New Line and Form Feed , but do´nt change margins, Fonts ....
Windows Printer Drivers resolves this isue, now I Do´nt use this technique. Ever use PrintDocument.
I ended up putting this catch in my pd_PrintPage subroutine...
' test for page break trigger
If line.Substring(0) = Chr(12) Then
Exit While
End If
This will break when it finds vbFormFeed, acii code 12.
Thanks for all your help, I appreciate the info.
While count < linesPerPage line = streamToPrint.ReadLine() If line Is Nothing Then Exit While End If ' test for page break trigger If line.Substring(0) = Chr(12) Then Exit While End If yPos = topMargin + count * printFont.GetHeight(ev.Graphics) ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat()) count += 1End While
After x77 pointed out the fact that the Graphics library is not designed to interpret what it inputs, I was able to create my own breakpoint to identify and deal with the formfeed code.
1) thank you for pointing out the spelling mistake, I feel a little bit dumber now for not noticing that before.
Embarrassment aside, I'm not convinced that your solution to part 2 of my problem is what im looking for. If I bypass the Graphics method, how am I able to control the style of print? For example, setting the print format to landscape and the font to "Courier New"?