Robert S. Robbins - Williamsport Web Developer
Home Page
Robert S. Robbins Blog
Client List
Asynchronous JavaScript and XML
Robert S. Robbins Technical Skills
aspPrint Server Component
Database Guru
FrontPage VBA
Active Server Pages Programming
ASP.NET Programming
Network Administration
PC Maintenance
Web Development
StoreFront Customization
Robert S. Robbins - Williamsport Web Developer News
click to read more!Blog
click to read more!Visual Studio 2005 - Item Templates

ASP.NET Printing - Williamsport PA

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> 
Copyright © 2007. Robert S. Robbins.  |  Privacy Policy  |  Terms Of Use  |  Contact

web design by Robert S. Robbins