locked
Printing from web form RRS feed

  • Question

  • User1408931231 posted

    I have a server report that I have rendered to a byte[]. That's all good,

    I want to be able to print to a network printer. Which is where I am having the problem. Is there a good solution for this? Has anyone done it successfully?

    I have tried this method...

     Microsoft.Reporting.WebForms.ServerReport r = new 
                Microsoft.Reporting.WebForms.ServerReport();
                r.ReportServerUrl = new Uri("http://blah/reportserver");
                r.ReportPath = "**working path**";
    
                Byte[] mbytes = r.Render("PDF");
    
                //Response.BinaryWrite(mbytes);
                try
                {
                System.Net.Sockets.Socket s = new 
                System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                System.Net.Sockets.SocketType.Stream,
                System.Net.Sockets.ProtocolType.Tcp);
                
                s.NoDelay = true;
                
                System.Net.IPAddress ip = System.Net.IPAddress.Parse("**working IP**");
                
                System.Net.IPEndPoint ipep = new System.Net.IPEndPoint(ip,9100);
          
                s.Connect(ipep);
    
                s.Send(mbytes);
                s.Close();
                }
                catch (System.Net.Sockets.SocketException sex)
                {
                    Response.Write("Socket Exception " + sex.Message);
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }

    I don't get any errors. Any other ideas?

    Thursday, October 31, 2019 6:40 PM

All replies

  • User2053451246 posted

    The network printer needs to be installed on the PC running the code.  Server-side .Net code can't access a PC's local printers, usb, etc.

    Thursday, October 31, 2019 8:54 PM
  • User288213138 posted

    Hi peeweex10,

    System.Net.Sockets.Socket s = new 
                System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                System.Net.Sockets.SocketType.Stream,
                System.Net.Sockets.ProtocolType.Tcp);
                
                s.NoDelay = true;
                
                System.Net.IPAddress ip = System.Net.IPAddress.Parse("**working IP**");
                
                System.Net.IPEndPoint ipep = new System.Net.IPEndPoint(ip,9100);
          
                s.Connect(ipep);
    
                s.Send(mbytes);
                s.Close();

    As far as i know this code only a Client Side Programming.

    You creation TCP/IP Socket use Socket Class Costructor, then Connect Socket to the remote endpoint use Connect() method, fnally send your mbytes to the server.

    If you want to use network printer, you can refer to below links, you should search for the printer firstly, then do some operation.

    https://weblogs.asp.net/aghausman/get-list-of-installed-printers-using-c-wmi

    https://stackoverflow.com/a/3729262

    Hope this can help you.

    Best regards,

    Sam

    Friday, November 1, 2019 2:55 AM