1

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.

Martin Crawley
  • 477
  • 1
  • 7
  • 16
  • this might help u https://stackoverflow.com/questions/18704839/send-text-file-directly-to-network-printer – styx Apr 17 '18 at 09:38
  • Unfortunately this doesn't seem to work with my particular make/model of printer. Thanks though. – Martin Crawley Apr 17 '18 at 10:40
  • @MartinCrawley 1) `..would also mean the Print Preview screen is always displayed...` This is absolutely false, previewing is optional 2) ***Most*** printers that use port 9100 are what most call RAW/LPR-based, if the link that `styx` listed does not work on your printer at all, then your printer is expecting some setup/escape sequence and you should consult the printer's protocol documentation. – SushiHangover Apr 17 '18 at 14:06
  • Thanks for the response. Can you please elaborate on the previewing via the Android Printing Framework being optional? No matter what Printing Service is used, the printer selection will always show. This is how the Android Printing Framework works. As for the escape sequence, I think you may be correct. I think I may need to pass the printer PCL. – Martin Crawley Apr 17 '18 at 19:06

2 Answers2

2

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();
1
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;
}
veni
  • 41
  • 4
  • is there possibility to send page margin (top margin) using that solution? – Arie Jun 02 '22 at 19:10
  • No. This is a plain-text solution. But you can add or remove empty lines (\n) at the beginning of your payload – veni Nov 18 '22 at 11:43