Access Your Network Printer RRS feed

  • Question

  • User-1163549985 posted
    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" %> <script language="VB" runat="server"> Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Try Dim pd as New PrintDocument() ' Set the printer name. pd.PrinterSettings.PrinterName = "\\NS5\hpoffice" 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 </script> <title>Network Printing</title> <form runat="server">

    Network Printing

    Quantity : <asp:TextBox id="Quantity" width="25" text="0" runat="server" />
    Inventory Number : <asp:TextBox id="InvNumber" text="12345" runat="server" />
    Price : <asp:TextBox id="Price" text="12.50" runat="server" />
    Description : <asp:TextBox id="Description" Text="Were having fun now!" runat="server" />
    <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.
    Thursday, May 27, 2004 12:23 PM

All replies

  • User-266921089 posted
    I am trying to create a scenario where a .NET web form sends it's results to a network printer. I copied and pasted your code onto a .aspx page and changed the printer variable to point to a network printer here in my office. When I hit submit I get an error. Do I need to apply permissions or perhaps I am not inserting the correct network printer path.
    Thursday, December 2, 2004 10:25 AM
  • User-1163549985 posted
    Can you share the error message? I'm still currently using this on our local Intranet, so far so good!
    Thursday, December 2, 2004 6:16 PM
  • User-1546193688 posted
    Hi SuperLite17, here is the error i got.... Really dont know what is it regarding... Error: System.Drawing.Printing.InvalidPrinterException: Tried to access printer '\\NS5\hpoffice' with invalid settings. at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal() at System.Drawing.Printing.PrinterSettings.GetHdevmode(PageSettings pageSettings) at System.Drawing.Printing.PrintController.OnStartPrint(PrintDocument document, PrintEventArgs e) at System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint(PrintDocument document, PrintEventArgs e) at System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at ASP.printer_aspx.Submit_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\test\printer.aspx:line 26 Regards life's Ng
    Friday, December 10, 2004 2:22 AM
  • User2104302234 posted
    you didn't change the printer from his code
    Saturday, December 11, 2004 1:31 AM
  • User-1101424280 posted

    Hi there,

    I am having the same problems. Tried to access printer with Invalid Setting. And yes, I did set the printer path to the actuall path on my network. Could this be a security issue? I trying to print server side from an aspx page. When debugging on my development machine it works fine, but when I deploy it on the live server i get the error.

    Has anybody else encountered this problem?? Is there a solution for it?

    Thanks in advance

    Monday, June 27, 2005 6:13 PM
  • User-614101622 posted

    I want to print crystal report directly to printer from my web application....

    when I try to print crystal report from the localhost it works fine but when i try to access the application from network computer it gives me the error Tried to access printer '\\ndainfaprt01\nda1-mf-bw01' with invalid settings....

    Please can any body advise why this error is coming .... any resolutions you have

    An early reply is highly appriciated


    Aadish Tiwari

    Tuesday, July 5, 2005 7:36 AM
  • User440487352 posted
    Kindly share the info. if you would have managed to print through a network printer.

    appeciate your helps..
    Monday, September 26, 2005 12:38 PM
  • User2011722611 posted
    I believe the problem with printing to a network printer is that the network printed doesn't exist unless there is a user logged into the server which the asp.net application is running.  Try creating a local printer that points directly to the network printer.  Local printers are accessable through code when a user is not logged in.
    Thursday, October 6, 2005 2:11 PM
  • User1431893619 posted

    I've gone through u r code and I did the same thing in my aspx form.

    But when I click on "SUBMIT" button, I am getting an errors stating:

    Error: System.Runtime.InteropServices.SEHException: External component has thrown an exception. at System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint(PrintDocument document, PrintEventArgs e) at System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at WebApplication1.WebApplication1.NetworkPrinting.Submit_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\WebApplication1\NetworkPrinting.aspx.vb:line 52

    Can u please help me out. Will be a great help.

    Thanks in advance.


    Wednesday, October 26, 2005 5:37 AM
  • User1497669028 posted
    Superlite - awesome code. This was something I've been looking for a while. It isn't exactly what I need, but the things that I was having a hard time figuring out you've done a fine job of getting me off to a good start with. Excellent addition to the developer community!
    Wednesday, May 17, 2006 1:00 PM
  • User583459717 posted

    when i use the code i get:


    Error Type:
    Microsoft VBScript compilation (0x800A03EA)
    Syntax error
    /webform/test2.asp, line 82, column 61
    objWSHNet.AddPrinterConnection "LPT1", strPrinterPath, False,

    Tuesday, May 30, 2006 12:13 PM
  • User-1210372306 posted
    I'm having the "settings to access printer "networkprinter" are invalid" problem as well.  Did anyone find a solution to this?  I couldn't figure out how to setup a local printer to point to a network printer.


    Monday, August 21, 2006 2:10 PM
  • User-1000199745 posted

    Hello, we have another forum going which is dealing with similar issue:


    this one seems to contain more specific ino however:

     I have been worjking on jthis for a little while now and have experimented with essentially two methods

    1. using printDocument class

    2. using MS sample RawPrinterHelper (taking over the winspool.Drv )

    I managed to get both methods working however they both fail when I try to get the printer to abide by the HTML formatting. The best I can do is get it to spit out the HTML tags as part of the ASCII stream.

    I'll be glad to share my code or provide help configuring the th network printers so that they are called correctlly (hint: you will have better success if you configure the Printer on it's own virtual port which requires that it's using a local driver on the application server)

    back to my issue: I have gone as far as saving my results as an HTML file on the server. Now all I am tring to do is tell the printer that it's HTML and that it should format it accordinglly and not print out the tags. Sounds fairly simple because we all know that you can print out HTML documents formatted properlly however both methods i've tried are actually parsing out text on the page as a stream and sends it to the printer as such without telling the printer that it's HTML.....










    Thursday, January 4, 2007 2:10 PM
  • User-1216251265 posted
    Tuesday, April 3, 2007 12:31 PM
  • User761576126 posted

    I might be pointing out something really obvious here, but maybe the  printer name you are using is a relative reference, when on the web the printer is not  on the server's network, and the printer needs to be referenced directly (maybe by IP address)?


    Just a thought,

    The King 

    Tuesday, April 17, 2007 7:30 PM
  • User-1821657828 posted

    As a potential supplement, i use the following function to get an array of printers on a server. You can bind this to a drop-down, etc and then the user can select from the available printers (on the server) to use.


    Imports System.Drawing.Printing

    Imports System.Collections.Generic 


    Public Shared Function ListPrinters(ByVal PrinterType As String) As String()

    Dim lst As New List(Of String)

    Dim PrintPermissions As New PrintingPermission(System.Security.Permissions.PermissionState.Unrestricted)

    PrintPermissions.Level = PrintingPermissionLevel.AllPrinting

    For Each printer As String In PrinterSettings.InstalledPrinters




    Return lst.ToArray

    End Function

    Friday, April 20, 2007 2:14 AM
  • User-509449696 posted

    Did you ever get any solution to this? I am still searching for one as well. I tried a product called aspprinter. Formating under this product works great, but you cannot set the printer you wish to print the html code to. I tried using System.management namespace to set the default printer first, but the SetDefaultPrinter method won't work in asp.net only in windows forms.

    Tuesday, July 31, 2007 1:21 PM
  • User47357567 posted

    Nice article it worked for me...

    Wednesday, January 9, 2008 12:15 PM
  • User47357567 posted

    I have a PDF file on server and I would like to print that PDF file to client machine's printer...client/user will sent me print Id on network..so how can I do this funnctionality...

    Quick respose appriciated...



    Friday, January 11, 2008 1:50 PM
  • User-784282280 posted

    It seems work for me but idon't understand VB . So any one can post in asp.net c#


    Sunday, April 25, 2010 4:44 AM
  • User-1737788559 posted

    Does any body  have a c# version of this.

    Pl send to nandyarun@yahoo.com 

    Wednesday, October 20, 2010 4:29 PM
  • User-516013588 posted
    using System.Drawing.Printing;
    using System.Collections.Generic;

    public static string[] ListPrinters(string PrinterType)

    List<string> lst = new List<string>();

    PrintingPermission PrintPermissions = new PrintingPermission(System.Security.Permissions.PermissionState.Unrestricted);

    PrintPermissions.Level = PrintingPermissionLevel.AllPrinting;

    foreach (string printer in PrinterSettings.InstalledPrinters) {



    return lst.ToArray();


    Does any body  have a c# version of this.

    Pl send to nandyarun@yahoo.com 

    Friday, November 5, 2010 2:45 PM