 |
 |
|  |
This example prints a text file but you can use the various
methods in the System.Drawing class for more interesting
tasks. Remember to change the file path for the
StreamReader and the PrinterName string to valid values for
the resources on your system.
Printing
Under ASP.NET
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Printing" %>
<html>
<script language="VB" runat="server">
' The PrintPage event is raised for each page to be printed.
Private Sub pd_PrintPage(sender As Object, 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
Dim printFont = New Font("Arial", 10)
Dim streamToPrint = New StreamReader("G:\winstst.log")
' 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
Public Sub Page_Load()
Dim streamToPrint = New StreamReader("G:\winstst.log")
Dim printFont = New Font("Arial", 10)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
pd.PrinterSettings.PrinterName = "Canon Bubble-Jet BJC-4300"
If pd.PrinterSettings.IsValid then
lblTest.Text = "Printer name was valid."
pd.Print()
Else
lblTest.Text = "Printer is invalid."
End If
streamToPrint.Close()
End Sub
</script>
<body>
<form id="Form" runat="server">
<asp:Label id="lblTest" runat="server"/>
</form>
</body>
</html>
|
 |
 |
|
|
|
|
|
 |
|
|
|
|
| |