简体   繁体   中英

Send Print Job To Printer Directly (TCP)

I've got a requirement where I need to send a print job to a printer from an Android app. The actual info that needs printing is very basic, so nothing special required - Just text really. The app is a Xamarin.Forms app.

I am aware that I can use the standard Android Printing framework but this isn't what I need - This would mean the printer need pre-configuring on the device and would also mean the Print Preview screen is always displayed before the print is made.

I've been researching sending a direct print request via TCP sockets on Port 9100 however I can't seem to get this working.

Does anyone have a working example of how this can be done, either in .Net Standard or Android Native?

Is there also a standard protocol for sending print jobs to a printer via this method?

Thanks.

try this:

                var ipAddress = "XXX.XXX.XXX.XXX";
                var port = 9100;
                var fle = "file.pdf";

                var data = System.IO.File.ReadAllBytes(fle);

                var client = new System.Net.Sockets.TcpClient();
                client.Connect(ipAddress, port);

                var stream = client.GetStream();
                stream.Write(data, 0, data.Length);

                client.Close();
public static bool SendTestPage(string target) {
    string msg = "\n" +
                "################################\n" +
                "\x001BE1" + //bold on
                "This is a print test\n\n" +
                "\x001BE0" + //bold off
                DateTime.Now.ToLongTimeString() + "\n" +
                DateTime.Now.ToLongDateString() + "\n" +
                "################################\n" +
                "\n\n\n\n\n\n\n\n" +
                "\x1Bm\0\0"; //cut

    Byte[] data = Encoding.ASCII.GetBytes(msg);

    try {
        TcpClient client = new TcpClient();
        client.Connect(target, 9100);

        NetworkStream stream = client.GetStream();
        stream.Write(data, 0, data.Length);

        stream.Flush();
        stream.Close();
        client.Close();

    } catch {
        return false;
    }

    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM