find and solve ||
Please wait.....

.Net Core Print to Network Printer Using c#

.Net Core Print to Network Printer Using c#

This blog will give you a brief idea about how to find network printer by ip address and port number and pass raw data in this network printer using .net core.Raw Data Printer is a unique component that enables you to control the entire range of printer functions directly from within your code by sending native printer commands (escape codes or the other commands) in any printer command language (e.g. standard ESC commands, ZPL commands, PCL commands, etc.

This tutorial focuses on given below

  • How to find the network printer using printer IP address and port
  • If printer is not configured, then show the message as "Printer is not Connected"
  • How to print report using network printer runtime

Create New Application in visual studio 2019

  • Open your visual studio 2019 or grater then 2019
  • Click on Create a New Project
  • Select ASP.NET Core Web Application and then Next
  • Provide a Project name and confirm or change the Location. Select Create
  • Select the latest version of ASP.NET Core in the drop-down (.NET Core 5.0)and then select Web Application
  • Under Authentication, select Change and set the authentication to Individual User Accounts and then Select OK

Open your controller and past this code like as given below.

       public IActionResult PrintAPI(OrderItemGet model)
        {             SocketPermission socketPermission1 = new SocketPermission(PermissionState.Unrestricted);
            // Create a 'SocketPermission' object for two ip addresses.
            SocketPermission socketPermission2 = new SocketPermission(PermissionState.None);             SecurityElement securityElement1 = socketPermission2.ToXml();             // 'SocketPermission' object for 'Connect' permission
            SecurityElement securityElement2 = new SecurityElement("ConnectAccess");
            // Second 'SocketPermission' ip-address is '' for 'All' transport types and
            // for 'All' ports for the ip-address.
            SecurityElement securityElement4 = new SecurityElement("URI", "");
            // Obtain a 'SocketPermission' object using 'FromXml' method.
            // Obtain a 'SocketPermission' object using 'FromXml' method.
            Socket clientSock = new Socket(                 AddressFamily.InterNetwork,
            //clientSock.NoDelay = true;
            IPAddress ip = IPAddress.Parse("");
            IPEndPoint remoteEP = new IPEndPoint(ip, 4730);
            if (!clientSock.Connected)
                return BadRequest("Printer is not connected");
            }             Encoding enc = Encoding.ASCII;
            string GS = Convert.ToString((char)29);
            string ESC = Convert.ToString((char)27);
            string COMMAND = "";
            COMMAND = ESC + "@";
            COMMAND += GS + "V" + (char)1;
            //byte[] bse =
            char[] bse = COMMAND.ToCharArray();
            byte[] paperCut = enc.GetBytes(bse);
            // Line feed hexadecimal values
            byte[] bEsc = new byte[4];
            // Sends an ESC/POS command to the printer to cut the paper
            string t = (" " + model.PrintTitle.ToUpper() + "\r\n");
            t = t + ("----------------------------------------\r\n");
            t = t + ("Table: Table-C BillNo: 120 \r\n");
            t = t + ("----------------------------------------\r\n");
            t = t + ("Date :2022/01/21 Order: Sylvia \r\n");
            t = t + ("=======================================\r\n");
            t = t + ("\r\n");
            t = t + (" SN. 1 Item: MoMo Qty: 2 \r\n");
            t = t + ("\r\n");
            t = t + ("\r\n");             t = t + ("\r\n");
            t = t + ("\r\n");
            t = t + ("\r\n");
            t = t + ("\r\n");
            char[] array = t.ToCharArray();
            byte[] byData = enc.GetBytes(array);
            return Ok(200);

Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If felt valuable to you, feel free to share it.


Report Response