求一个能获取打印机数据的C#控制台解决方案

比如本地建立一个文本文件 打印一个数字456在本地的打印机上,同时控制台能截获这个数据,并保存就算成了

现本人通过监听打印端口 已经截获一段十六进制数据包 到好像加密的 估计这个方法行不通 看看大家有没有其他思路办法

程序如何与打印机数据通讯,通讯解决了就抓包获取数据



```c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Printer
{undefined
    public partial class Form1 : Form
    {undefined
        public Form1()
        {undefined
            InitializeComponent();
        }


        private Font printFont;
        private Font titleFont;
        private StringReader streamToPrint;
        private int leftMargin = 0;

         /// <summary>
        /// 设置PrintDocument 的相关属性 调用打印机方法
        /// </summary>
        /// <param name="str">要打印的字符串</param>
        public void print(string str)
        {undefined
            try
            {undefined
                //打印文字
                streamToPrint = new StringReader(str);
                printFont = new Font("宋体", 10);
                titleFont = new Font("宋体", 15);
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                //调用的打印机名
                pd.PrinterSettings.PrinterName = "Hewlett-Packard HP LaserJet Pro MFP M126a";
                pd.DocumentName = pd.PrinterSettings.MaximumCopies.ToString(); 
                pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage);
               // pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.MyPrintDocument_PrintPage);
                pd.PrintController = new System.Drawing.Printing.StandardPrintController(); 
                pd.Print();
            }
            catch(Exception ex)
            {undefined
                MessageBox.Show(ex.ToString());
            }
        }


        /// <summary>
        /// 打印格式(字符串)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ev"></param>
        private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {undefined
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = this.leftMargin;
            float topMargin = 0;
            String line = null;
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
            while (count < linesPerPage &&
            ((line = streamToPrint.ReadLine()) != null))
            {undefined
                if (count == 0)
                {undefined
                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                    ev.Graphics.DrawString(line, titleFont, Brushes.Black, leftMargin + 10, yPos, new StringFormat());
                }
                else
                {undefined
                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                }
                count++;
            }
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;

        }


        private void button1_Click(object sender, EventArgs e)
        {undefined
            //打印报表解决方案一:

            string ss = "菜单";
            //调用本地打印机成功:注意的问题的是打印机的名字和打印机的驱动安装
            print("主题\n------------\n被冷冷地:"+ss+"\n-------------\n说到了");


            //获取本地打印机的名字
            GetPrintName();
        }

        /// <summary>
        /// 获取打印机的名字
        /// </summary>
        private void GetPrintName()
        {undefined
            //获取本地打印机的名字
            PrintDocument print = new PrintDocument();
            string sDefault = print.PrinterSettings.PrinterName;//默认打印机名

            label1.Text = sDefault;
            
            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {undefined
                listBox1.Items.Add(sPrint);
                textBox1.Text += sPrint + "\n";
                if (sPrint == sDefault)
                    listBox1.SelectedIndex = listBox1.Items.IndexOf(sPrint);

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {undefined
            // TODO:  这行代码将数据加载到表“weijiaweiDataSet.xinxibiao”中。您可以根据需要移动或删除它。
           // this.xinxibiaoTableAdapter.Fill(this.weijiaweiDataSet.xinxibiao);

            this.reportViewer1.RefreshReport();
            this.reportViewer1.RefreshReport();
        }

        private void button2_Click(object sender, EventArgs e)
        {undefined


            print2();

        }

        /// <summary>
        /// 设置PrintDocument 的相关属性
        /// </summary>
        /// <param name="str">要打印的字符串</param>
        public void print2()
        {undefined
            try
            {undefined
               // streamToPrint = new StringReader(str);
                printFont = new Font("宋体", 10);
                titleFont = new Font("宋体", 15);
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                pd.PrinterSettings.PrinterName = "Hewlett-Packard HP LaserJet Pro MFP M126a";
                pd.DocumentName = pd.PrinterSettings.MaximumCopies.ToString();
                //pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage);
                pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.MyPrintDocument_PrintPage);
                pd.PrintController = new System.Drawing.Printing.StandardPrintController();
                pd.Print();
            }
            catch (Exception ex)
            {undefined
                MessageBox.Show(ex.ToString());
            }
        }


        /// <summary>
        /// 打印的格式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {undefined
            /*如果需要改变自己 可以在new Font(new FontFamily("黑体"),11)中的“黑体”改成自己要的字体就行了,黑体 后面的数字代表字体的大小
             System.Drawing.Brushes.Blue , 170, 10 中的 System.Drawing.Brushes.Blue 为颜色,后面的为输出的位置 */
            e.Graphics.DrawString("新乡市三月软件公司入库单", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 170, 10);
            e.Graphics.DrawString("供货商:河南科技学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Blue, 10, 12);
            //信息的名称
            e.Graphics.DrawLine(Pens.Black, 8, 30, 480, 30);
            e.Graphics.DrawString("入库单编号", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 35);
            e.Graphics.DrawString("商品名称", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 160, 35);
            e.Graphics.DrawString("数量", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 260, 35);
            e.Graphics.DrawString("单价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 330, 35);
            e.Graphics.DrawString("总金额", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 400, 35);
            e.Graphics.DrawLine(Pens.Black, 8, 50, 480, 50);
            //产品信息
            e.Graphics.DrawString("R2011-01-2016:06:35", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 55);
            e.Graphics.DrawString("联想A460", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 160, 55);
            e.Graphics.DrawString("100", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 260, 55);
            e.Graphics.DrawString("200.00", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 330, 55);
            e.Graphics.DrawString("20000.00", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 400, 55);


            e.Graphics.DrawLine(Pens.Black, 8, 200, 480, 200);
            e.Graphics.DrawString("地址:新乡市河南科技学院信息工程学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 210);
            e.Graphics.DrawString("经办人:任忌", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 220, 210);
            e.Graphics.DrawString("服务热线:15083128577", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 320, 210);
            e.Graphics.DrawString("入库时间:" + DateTime.Now.ToString(), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 230);
        }


        private void button3_Click(object sender, EventArgs e)
        {undefined
            // printDocument1 为 打印控件
            //设置打印用的纸张 当设置为Custom的时候,可以自定义纸张的大小,还可以选择A4,A5等常用纸型
            
            //先注释和不注释来看取打印的内容   格式一测试失败

            //格式一:
           // this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custom", 500, 300);
            //this.printDocument1.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

            //格式二:
          //  this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custom", 500, 300);
          //  this.printDocument1.PrintPage += new PrintPageEventHandler(this.MyPrintDocument_PrintPage);

            //格式:菜单
           // this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custom", 300,500);
           // this.printDocument1.PrintPage += new PrintPageEventHandler(this.Menu);

            //打印图片
            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custom", 300, 500);
            this.printDocument1.PrintPage += new PrintPageEventHandler(this.Image);


            //将写好的格式给打印预览控件以便预览
            printPreviewDialog1.Document = printDocument1;

            //显示打印预览
            DialogResult result = printPreviewDialog1.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {undefined
            print3();

        }


        /// <summary>
        /// 设置PrintDocument 的相关属性
        /// </summary>
        /// <param name="str"></param>
        public void print3()
        {undefined
            try
            {undefined
                /*
                // streamToPrint = new StringReader(str);
                printFont = new Font("宋体", 10);
                titleFont = new Font("宋体", 15);
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                pd.PrinterSettings.PrinterName = "Hewlett-Packard HP LaserJet Pro MFP M126a";
                pd.DocumentName = pd.PrinterSettings.MaximumCopies.ToString();
                //pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage);
                pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.Menu);
                pd.PrintController = new System.Drawing.Printing.StandardPrintController();
                pd.Print();
                 
                 */

        

                //新建打印对象
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                //打印机名字
                pd.PrinterSettings.PrinterName = "Hewlett-Packard HP LaserJet Pro MFP M126a";
                //打印文档显示的名字
                pd.DocumentName = "订单";
                //
                //pd.PrinterSettings.MaximumCopies.ToString();

                //打印的格式
                pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.Menu);

                //
                pd.PrintController = new System.Drawing.Printing.StandardPrintController();

                //开始打印
                pd.Print();
            }
            catch (Exception ex)
            {undefined
                MessageBox.Show(ex.ToString());
            }
        }

        //绘图需要使用的数组 后期可以套用变量list
        public string[] menu = { "菜一", "菜二","中级菜单" };

        /// <summary>
        /// 打印的格式:菜单
        /// </summary>
        /// <param name="sender">自定义报表(原理是绘图)</param>
        /// <param name="e"></param>
        private void Menu(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {undefined

              //绘制(输出,文字格式(字体,大小),颜色,位置起始位置x,y轴坐标)                 
            e.Graphics.DrawString("店名", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 138, 10);
           
            //打印两个点的坐标(颜色,坐标1,坐标2)
            e.Graphics.DrawLine(Pens.Black, 8, 30, 292, 30);


            e.Graphics.DrawString("订单号:()", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 10, 35);

            e.Graphics.DrawString("编号", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 10, 55);
            e.Graphics.DrawString("菜名", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 75, 55);
            e.Graphics.DrawString("单价", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 150, 55);
            e.Graphics.DrawString("数量", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 200, 55);

            int i = 0;
            //循环输出变量
            foreach (string element in menu)
            {undefined
             
                i=i+20;
                e.Graphics.DrawString(element, new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 75, 80+i);

            }


            e.Graphics.DrawLine(Pens.Black, 8, 200, 292, 200);

         }


        private void button5_Click(object sender, EventArgs e)
        {undefined
            print4();
        }

        public void print4()
        {undefined
            try
            {undefined

                //新建打印对象
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                //打印机名字
                pd.PrinterSettings.PrinterName = "Hewlett-Packard HP LaserJet Pro MFP M126a";
                //打印文档显示的名字
                pd.DocumentName = "订单";
                //
                //pd.PrinterSettings.MaximumCopies.ToString();

                //打印的格式
                pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.Image);

                //
                pd.PrintController = new System.Drawing.Printing.StandardPrintController();

                //开始打印
                pd.Print();
            }
            catch (Exception ex)
            {undefined
                MessageBox.Show(ex.ToString());
            }
        }


        /// <summary>
        /// 打印的格式:图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {undefined
            //直接调用图片对象绘制
            e.Graphics.DrawImage(pictureBox1.Image, 20, 20);

        }

        private void button6_Click(object sender, EventArgs e)
        {undefined
            printDocument1.PrintController.OnEndPrint(printDocument1, new System.Drawing.Printing.PrintEventArgs());
        }

    }
}


```

这个实际还是要看什么打印机 支持哪些功能

  1. 它提供相应的sdk工具包 那就简单直接接口里面拿打印信息
  2. 抓打印机管理后台网页,之前遇到过一类打印机监控需求,各类方法都试了不行最后发现有专门的一个驱动安安装了之后会有一个工具箱程序,会启动一个网页上面可以直接读取到对应的打印信息 后面直接就抓这个网页拿信息 但一般不会有打印内容信息
  3. 中间截流中继,就是先创建一个虚拟打印机,类似与pdf打印那种,你监控这个虚拟打印目录,将第三发的打印机换成这个虚拟的打印机,收到pdf后进行技术解析,既然是打单对应的订单信息应该都有,最后你再给他发到最终的单子打印机

打印机什么接口

先分析一下打印字符的格式,贴出来帮你看一下(需要对照一下打印内容和十六进制字符),没加密的话可以实现转换。