Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print to a network printer using IP address

Tags:

c#

.net

winforms

I want to send in a filename and a printer's IP address to specify which printer to print to.

I am getting an error saying "Settings to access printer 'xxx.xxx.xxx.xxx' are not valid." when I get to printdoc.Print().

How to I set which printer to print to based on the IP Address?

printdoc = new PrintDocument();
printdoc.PrinterSettings.PrinterName = IPAddress.Trim;
printdoc.DocumentName = FileName;
printdoc.Print();

How to solve this issue? It's a C# vs2010 standalone Windows application.

like image 721
user3541403 Avatar asked Sep 13 '14 12:09

user3541403


2 Answers

here is the complete working code of IP printer (Model GK420t ZPL And you can access any IP printer). Just replace only three things 1) Add you IP address 2) add your port number 3) Add you PNG File path

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Net.Http;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Http;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Printing;
using System.Net.NetworkInformation;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Printing;





namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {     
            string ipAddress = "Your IP address";
            int port = Your port number;

            string zplImageData = string.Empty;
            string filePath = @"your png file path";
            byte[] binaryData = System.IO.File.ReadAllBytes(filePath);
            foreach (Byte b in binaryData)
            {
                string hexRep = String.Format("{0:X}", b);
                if (hexRep.Length == 1)
                    hexRep = "0" + hexRep;
                zplImageData += hexRep;
            }
            string zplToSend = "^XA" + "^FO50" + "50^GFA,120000,120000,100" + binaryData.Length + ",," + zplImageData + "^XZ";
            string printImage = "^XA^FO115,50^IME:LOGO.PNG^FS^XZ";

            try
            {
                // Open connection
                System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
                client.Connect(ipAddress, port);

                // Write ZPL String to connection
                System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream(), Encoding.UTF8);
                writer.Write(zplToSend);
                writer.Flush();
                writer.Write(printImage);
                writer.Flush();
                // Close Connection
                writer.Close();
                client.Close();
            }
            catch (Exception ex)
            {
                // Catch Exception
            }


    }
        }

    }

enter image description here

like image 172
Asad Avatar answered Sep 26 '22 12:09

Asad


I was finally able to get this going

VB: dim

Imports System.IO
Imports System.Net, System.Net.Sockets
.
.
.
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
  Try
    Dim IPAddress As String = txtIPAddr.Text 'ie: 10.0.0.91;
    Dim port As Integer = txtPort.Text 'ie: 9100

    Dim client As System.Net.Sockets.TcpClient = New System.Net.Sockets.TcpClient()
    client.Connect(IPAddress, port)
    Dim reader As StreamReader = New StreamReader(txtFilename.Text) 'ie: C:\\Apps\\test.txt
    Dim writer As StreamWriter = New StreamWriter(client.GetStream())
    Dim testFile As String = reader.ReadToEnd()
    reader.Close()
    writer.Write(testFile)
    writer.WriteLine("Hello World!")
    writer.Flush()
    writer.Close()
    client.Close()
  Catch ex As Exception
    MessageBox.Show("Error: " + ex.Message)
  End Try
End Sub

C#:

using System.IO;
using System.Net;
using System.Net.Sockets;
.
.
.
private void btnPrint_Click(object sender, EventArgs e) {
  try {
    string ipAddress = txtIPAddr.Text.ToString(); ; //ie: 10.0.0.91
    int port = int.Parse(txtPort.Text.ToString()); //ie: 9100

    System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
    client.Connect(ipAddress, port);
    StreamReader reader = new StreamReader(txtFilename.Text.ToString()); //ie: C:\\Apps\\test.txt
    StreamWriter writer = new StreamWriter(client.GetStream());
    string testFile = reader.ReadToEnd();
    reader.Close();
    writer.Write(testFile);
    writer.WriteLine("Hello World!");
    writer.Flush();
    writer.Close();
    client.Close();
  }
  catch (Exception ex) {
    MessageBox.Show(ex.Message, "Error");
  }
}
like image 42
user1388706 Avatar answered Sep 26 '22 12:09

user1388706