Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.EndEdit();
            PrintCommonSetting.Default_Border_Style = BordersEdgeStyle.All;
            //if (this.dataGridView1.Rows.Count == 0)
            //{
            //MessageBoxHelper.Show("请先添加需要打印的学员!");
            //}
            StringFormat sf = new StringFormat();

            sf.Alignment     = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            int        rowHeight = 34;
            PageMargin margin    = new PageMargin();

            margin.Left  = 35;
            margin.Right = 35;
            //margin.Bottom = 36;


            //margin.Top = 37;
            margin.Bottom = 40;
            margin.Top    = 33;
            PrinterContent content = new PrinterContent();

            content.CustomPageMargin = margin;
            PrinterHint header = this.GetHeader(content.CustomPageMargin.Width, this.comboBox1.Text);

            //header.Rectangle = new Rectangle((content.CustomPageMargin.Width - header.Rectangle.Width), 0, header.Rectangle.Width, header.Rectangle.Height);
            content.Header         = header;
            content.IsPrinterPages = false;


            int[]        columnWidth  = { 60, 90, 90, 60, 184, 135, 60, content.CustomPageMargin.Width - 679 };
            string[]     columnHeader = { "序号", "车号", "姓名", "性别", "身份证明号码", "准考证明号", "成绩", "备注" };
            int          len          = columnWidth.Length;
            PrinterTable table        = new PrinterTable();
            PrinterRow   row          = new PrinterRow();

            row.Rectangle = new System.Drawing.Rectangle(0, 0, content.CustomPageMargin.Width, 43);
            TextDraw text = null;
            int      tmp  = 0;
            Font     col  = new Font("宋体", 15);

            for (int i = 0; i < len; i++)
            {
                text           = new TextDraw(columnHeader[i]);
                text.Formater  = sf;
                text.Font      = col;
                text.Border    = BordersEdgeStyle.All;
                text.Rectangle = new System.Drawing.Rectangle(tmp, 0, columnWidth[i], 43);
                tmp           += columnWidth[i];
                row.Add(text);
            }
            table.Header = row;
            //table.Add(row);
            row = null;
            DataRow dr         = null;
            string  tmpCarType = string.Empty;
            // DataTable dt = this.MockData();
            DataTable dt = this.GetDataFromGrid();

            if (dt.Rows.Count == 0)
            {
                for (int i = 0; i < 24; i++)
                {
                    dt.Rows.Add(new string[] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty });
                }
            }
            else
            {
                int tmprowcount = dt.Rows.Count % 24;
                if (tmprowcount != 0)
                {
                    tmprowcount = 24 - tmprowcount;
                    for (int i = 0; i < tmprowcount; i++)
                    {
                        dt.Rows.Add(new string[] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty });
                    }
                }
            }
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                dr             = dt.Rows[j];
                tmp            = 0;
                row            = new PrinterRow();
                row.Rectangle  = new System.Drawing.Rectangle(0, 0, content.CustomPageMargin.Width, rowHeight);
                text           = new TextDraw(((j + 1) % 24 == 0?24:(j + 1) % 24).ToString());
                text.Formater  = sf;
                text.Border    = BordersEdgeStyle.All;
                text.Rectangle = new System.Drawing.Rectangle(tmp, 0, columnWidth[0], rowHeight);
                tmp           += columnWidth[0];
                row.Add(text);
                tmpCarType = dr[3].ToString();
                for (int i = 1; i < len; i++)
                {
                    text           = new TextDraw(Convert.IsDBNull(dr[i - 1]) ? string.Empty : dr[i - 1].ToString());
                    text.Border    = BordersEdgeStyle.All;
                    text.Formater  = sf;
                    text.Rectangle = new System.Drawing.Rectangle(tmp, 0, columnWidth[i], rowHeight);
                    tmp           += columnWidth[i];
                    row.Add(text);
                }
                table.Add(row);
            }
            table.Border = BordersEdgeStyle.None;
            content.Body = table;
            PrinterHint footer = this.GetFooter(content.CustomPageMargin.Width);

            content.Footer = footer;
            content.PrinterSetting();
            //content.Preview();
        }
Пример #2
0
 /// <summary>
 /// dt的列必须比widths数组列多一列,最后一列由打印自己计算剩余长度
 /// </summary>
 /// <param name="dt"></param>
 /// <param name="widths"></param>
 /// <returns></returns>
 protected PrinterTable BuildContent(DataTable dt, int[] widths)
 {
     StringFormat sf = new StringFormat();
     sf.Alignment = StringAlignment.Center;
     sf.LineAlignment = StringAlignment.Center;
     int all = 0;
     int[] columnWidth=new int[widths.Length+1];
     for (int i = 0; i < widths.Length; i++)
     {
         columnWidth[i]=widths[i];
         all+=widths[i];
     }
     columnWidth[widths.Length] = customMargin.Width - all;
     //string[] columnHeader ={ "序号", "车号", "姓名", "性别", "身份证明号码", "准考证明号", "成绩", "备注" };
     int len = columnWidth.Length;
     PrinterTable table = new PrinterTable();
     
     PrinterRow row = new PrinterRow();
     int tmp = 0;
     TextDraw text = null;
     int rowHeight = 20;
     if (this.isPrintColName)
     {
         row.Rectangle = new System.Drawing.Rectangle(0, 0, customMargin.Width, 43);
         
         
         Font coltitle = new Font("宋体", 12, FontStyle.Bold);
         
         for (int i = 0; i < len; i++)
         {
             text = new TextDraw(dt.Columns[i].ColumnName);
             text.Formater = sf;
             text.Font = coltitle;
             text.Border = BordersEdgeStyle.All;
             text.Rectangle = new System.Drawing.Rectangle(tmp, 0, columnWidth[i], 43);
             tmp += columnWidth[i];
             row.Add(text);
         }
         table.Header = row;
     }
     //table.Add(row);
     row = null;
     Font col = new Font("宋体", 10);
     DataRow dr = null;
     for (int j = 0; j < dt.Rows.Count; j++)
     {
         dr = dt.Rows[j];
         tmp = 0;
         row = new PrinterRow();
         row.Rectangle = new System.Drawing.Rectangle(0, 0, customMargin.Width, rowHeight);
         for (int i = 0; i < len; i++)
         {
             text = new TextDraw(Convert.IsDBNull(dr[i]) ? string.Empty : dr[i].ToString());
             text.Border = BordersEdgeStyle.All;
             text.Font = col;
             text.Formater = sf;
             text.Rectangle = new System.Drawing.Rectangle(tmp, 0, columnWidth[i], rowHeight);
             tmp += columnWidth[i];
             row.Add(text);
         }
         table.Add(row);
     }
     table.Border = BordersEdgeStyle.None;
     return table;
 }