紧凑框架套接字文档打印

时间:2018-04-27 10:50:58

标签: c# sockets compact-framework

我正在尝试从windows ce设备将文档打印到网络激光打印机。 因为我正在尝试打印示例文本没有问题,我可以做那个trought套接字,我的问题是我需要在文本下面打印一些文本和一个小图像,例如:

not formatted text
------------------
|    IMAGE       |
------------------
other not formatted text

我的第一个想法是构建一个Rtf文档并将其发送到打印机上的sokets,但它显然打印文档文本内容而不是文本格式...我尝试了许多其他方法来编码要发送的数据但没有结果...有办法做到这一点,或者我只是在浪费时间?如果没有,有一种方法可以实现发送PCL / PostScript数据吗? (通常,驱动程序是新激光打印机上基本命令的“标准”)。 这是我用来在网络打印机上打印的功能:

public static string print_data_to_IP(string printerIP, int printerPort, byte[] myEncoded_bytes)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //Not supported
            //socket.NoDelay = true;

            IPAddress ip = IPAddress.Parse(printerIP);
            IPEndPoint ipEndPoint = new IPEndPoint(ip, printerPort);
            socket.Connect(ipEndPoint);
            socket.Send(myEncoded_bytes);
            socket.Close();
            return null;
        }

和myEncoded_bytes由:

制作
MemoryStream ms = new MemoryStream();
            using (FileStream stream = File.Open("\\test.rtf", FileMode.Open))
            {
                byte[] stream_to_byte_array = new byte[stream.Length];
                stream.Read(stream_to_byte_array, 0, (int)stream.Length);
                ms = new MemoryStream(stream_to_byte_array);
                stream.Close();
            }
byte[] data = ms.ToArray();

感谢您的帮助,

军团

P.S。:不允许使用外部库,因此我无法使用printCE或类似的'帮助'。

1 个答案:

答案 0 :(得分:0)

我试着更好地解释我是如何达到目标的,这是在@josef link后面的一个示例函数,我在选择的位置写了一些文本并在屏幕的中心画一个红色框(评论解释了pcl命令的工作原理)。 按照PCL手册,您可以轻松自定义文本和绘图(HERE_AN_HP_MANUAL)。

        private void btn_pcl_printing_Click(object sender, EventArgs e)
        {
            string printer_ip = "192.168.1.6";

            char command_start_character = '\u001B';
            char carriage_return = '\u000D';
            char line_feed = '\u000A';
            string lane_escape_character = string.Concat(carriage_return, line_feed); //"\r\n";
#if PJL
            string printer_process_name = "prova.TXT";
            string raw_end_of_file = command_start_character + "%–12345X" + " @PJL EOF" + lane_escape_character;
            raw_end_of_file += command_start_character + "%–12345X";
            string raw_to_print = command_start_character + "%–12345X";
            raw_to_print += " @PJL JOB NAME = \"" + printer_process_name  + "\"" + lane_escape_character;
            raw_to_print += " @PJL ENTER LANGUAGE = PCL" + lane_escape_character;
#else
            //Reset the printer and start a new PAGE
            string raw_to_print = command_start_character + "E";
#endif
            //Set Printer resolution to 150 DPI
            string raw_pcl_commands = command_start_character + "&u150D" + command_start_character + "*t150R";
            //Write "Hello Wold!!" at the position 150x and 150y (the command is case sensitive).
            raw_pcl_commands += command_start_character + "*p150x150Y" + "Hello World!!!";
            //Another way to to what i've just done before
            //raw_pcl_commands += command_start_character + "*p150X" + command_start_character + "*p150Y" + "Hello World!!!";
            raw_pcl_commands += command_start_character + "*p150X" + command_start_character + "*p150Y" + "Hello World!!!";
            //I choose to use RGB colors (v6), select an rgb color (a0,b0,c0) and save it an a palette (i0) that i select (i0S). My selected color is RED (255,0,0)
            raw_pcl_commands += command_start_character + "*v6w255a0b0c0i0S";
            //Another way to to what i've just done before
            //raw_pcl_commands += command_start_character + "*v6W";
            //raw_pcl_commands += command_start_character + "*v255a0b0C" + command_start_character + "*v0i0S";
            //Draw a rectangle 300*150
            raw_pcl_commands += command_start_character + "*c300a150b5P";


            //COMANDI PCL
            raw_pcl_commands += command_start_character + "E";
            raw_to_print += raw_pcl_commands;
#if PJL
            raw_to_print += raw_end_of_file;
#endif

            byte[] myEncoded_bytes = Encoding.UTF8.GetBytes(raw_to_print);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //Not supported on CE
            //socket.NoDelay = true;
            int printerPort = 9100;
            IPAddress ip = IPAddress.Parse(printer_ip);
            IPEndPoint ipEndPoint = new IPEndPoint(ip, printerPort);
            socket.Connect(ipEndPoint);
            socket.Send(myEncoded_bytes);
            socket.Close();
        }

现在,对于图像,您有2种方法可以遵循:

1)使用PCL命令see from page 41 of that manual“手动”绘制图像以查看一些示例(如果是一个简单的例子,就像我的情况那样,简单的条形码是最好的方法)。

2)用PCL宏绘制图像......这就是我所能说的,因为阅读that documentation让我很头疼并迫使我选择方法1:D

希望有帮助, 军团