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)
Try
Dim pd as New PrintDocument()

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

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(Environment.NewLine)
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
Else
    ' 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)
    Else
              sb.Append(fieldName + ": " + fieldValue + Environment.NewLine)
    End If
 End If
Next
ev.Graphics.DrawString(sb.ToString(), printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
End Sub



<html>

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

                    </td>
                </tr>
                <tr>
                    <td>
                        Inventory&nbsp;Number&nbsp;:</td>
                    <td>
                        <asp:TextBox id="InvNumber" text="12345" runat="server" />
                    </td>
                </tr>
				<tr>
                    <td>
                       Price&nbsp;:</td>
                    <td>
                        <asp:TextBox id="Price" text="12.50" runat="server" />
                    </td>
                </tr>
			<tr>
                    <td>
                        Description&nbsp;:</td>
                    <td>
                        <asp:TextBox id="Description" Text="Were having fun now!" runat="server" />
                    </td>
                </tr>
				   <tr>
                    <td colspan="2">
                        <asp:Button id="Submit" onclick="Submit_Click" runat="server" Text="Submit" />
                   </td
               </tr>
           </tbody>
      </table>
</form>
</body>
</html>

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s