Accessing the network printer from ASP.NET VB.NET

I managed to put together several pieces of code from the web to allow network printing. Most of the examples I found were written in C#.

This script can be useful for placing orders with warehouses etc that allow access to their network printers.. etc.

<%@ outputcache location="None" %>
<%@ Page Language="VB" Debug="True"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Printing " %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Web" %>

Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim pd as New PrintDocument()

' Set the printer name.
pd.PrinterSettings.PrinterName = "NS5hpoffice"
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage

Catch ex as Exception
Response.Write("Error: " & ex.ToString)

End Try
End Sub

Private Sub pd_PrintPage(ByVal sender as Object, ByVal ev as PrintPageeventArgs)
Dim yPos as Single = 250
Dim leftMargin as Single = ev.MarginBounds.Left
Dim topMargin as Single = ev.MarginBounds.Top
Dim printFont = New Font("Arial", 10)
Dim sb As StringBuilder =  New StringBuilder()

' Page title and date/time.
sb.Append("Warehouse Shipment Request")
sb.Append("DateTime: " + DateTime.Now.ToString()+ Environment.NewLine)

' Iterate submitted form fields and get field names.
Dim fieldValue As String
Dim fieldName As String

' Exclude viewstate and submit button.
For Each fieldName In HttpContext.Current.Request.Form
If fieldName = "__VIEWSTATE" Or fieldName = "Submit" Then
    ' Get the field values.
    fieldValue = HttpContext.Current.Request.Form(fieldName)
    ' Add the field names and values to the page.
    ' Break the field values into 50 character segments so it will fit on the paper.
    ' Currently, this only accounts for fields of l50 characters or less.
     ' ISSUE: breaks in the middle of words instead of spaces
    If fieldValue.Length > 100 Then
              sb.Append(fieldName + ": " + fieldValue.Substring(0,50) + Environment.NewLine)
              sb.Append("            " + fieldValue.Substring(50,50) + Environment.NewLine)
              sb.Append("            " + fieldValue.Substring(100,fieldValue.Length - 100) + Environment.NewLine)
    Else If fieldValue.Length > 50 Then
              sb.Append(fieldName + ": " + fieldValue.Substring(0,50) + Environment.NewLine)
              sb.Append("            " + fieldValue.Substring(50,fieldValue.Length - 50) + Environment.NewLine)
              sb.Append(fieldName + ": " + fieldValue + Environment.NewLine)
    End If
 End If
ev.Graphics.DrawString(sb.ToString(), printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
End Sub


    <title>Network Printing</title>
    <form runat="server">
        <table width="456" border="1" align="center" cellpadding="3" cellspacing="0">
                    <td colspan="2">
                        <p>Network Printing</p>
                    <td width="148">
                    <td width="290">
                        <asp:TextBox id="Quantity" width="25" text="0" runat="server" />

                        <asp:TextBox id="InvNumber" text="12345" runat="server" />
                        <asp:TextBox id="Price" text="12.50" runat="server" />
                        <asp:TextBox id="Description" Text="Were having fun now!" runat="server" />
                    <td colspan="2">
                        <asp:Button id="Submit" onclick="Submit_Click" runat="server" Text="Submit" />

Seems to work great on Intranet. Haven’t had the opportunity to try it external. Let me know how it does if you do.