public bool CheckDataList() { bool isPass = false; list.Clear(); m = 0; n = 1; if (dgvPrint.Rows.Count > 0) { for (int i = 0; i < dgvPrint.Rows.Count; i++) { DataGridViewCellCollection dvc = dgvPrint.Rows[i].Cells; bool isCheck = Convert.ToBoolean(dvc[0].Value); if (isCheck == true) { ProductExport pe = new ProductExport(); pe.SysNo = Convert.ToString(dvc[1].Value); pe.ExportData = Convert.ToString(dvc[2].Value); pe.ProductID = Convert.ToString(dvc[3].Value); pe.VendorID = Convert.ToString(dvc[4].Value); pe.ProductName = Convert.ToString(dvc[5].Value); list.Add(pe); } } } if (list.Count <= 0) { MessageBox.Show("请先选择要导出的数据"); return(false); } isPass = true; return(isPass); }
int n = 1; //页数 每页取左右 共10条数据 //打印详情 private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { try { if (list == null || list.Count <= 0) { e.Cancel = true; } int pageCount = 5; int listCount = 0;//剩余条数 if (list.Count > n * 10) { listCount = list.Count - (n - 1) * 10;//剩余条数 pageCount = 5; } else { listCount = list.Count - (n - 1) * 10; //剩余条数 pageCount = listCount / 2 + listCount % 2; //循环次数 } Font font1 = new Font("宋体", DWConver(3)); //设置字体类型和大小 Font font2 = new Font("Code 128", 30, FontStyle.Regular, GraphicsUnit.World); //条形码IntHrP36DlTt IntHrP72DlTt C39HrP60DhTt //font2.FontFamily = new FontFamily( Brush brush = new SolidBrush(Color.Red); //设置画刷颜色 Pen pen = new Pen(Color.Red, 0.1f); //创建画笔对象 WinPrint.Code128 _Code = new WinPrint.Code128(); //代码生成条形码方式 _Code.ValueFont = new Font("宋体", 11); for (int i = 0; i < pageCount; i++) { ProductExport peLeft = list[m]; //左 方框 //e.Graphics.DrawLine(pen, DWConver(2), DWConver((2 + 30 * i + 4 * i)), DWConver(42), DWConver((2 + 30 * i + 4 * i)));//上横 //e.Graphics.DrawLine(pen, DWConver(2), DWConver((2 + 30 * i + 4 * i)), DWConver(2), DWConver((2 + 30 * (i + 1) + 4 * i)));//左竖 //e.Graphics.DrawLine(pen, DWConver(42), DWConver((2 + 30 * i + 4 * i)), DWConver(42), DWConver((2 + 30 * (i + 1) + 4 * i)));//右竖 //e.Graphics.DrawLine(pen, DWConver(2), DWConver((2 + 30 * (i + 1) + 4 * i)), DWConver(42), DWConver((2 + 30 * (i + 1) + 4 * i)));//下横 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center;//商品名称 文本居中对其 Rectangle drawRectLeft = new Rectangle(DWConver(6), DWConver((2 + 30 * i + 4 * i + 2)), DWConver(34), DWConver(9)); e.Graphics.DrawString(peLeft.ProductName, font1, brush, drawRectLeft, sf); //e.Graphics.DrawString(peLeft.ProductName, font1, brush, DWConver(5), DWConver((2 + 30 * i + 4 * i + 2))); e.Graphics.DrawImage(writer.Write(peLeft.ProductID), DWConver(6), DWConver((2 + 30 * i + 4 * i + 13)));//第三方控件产生条形码 #region 字体和代码产生条形码 //e.Graphics.DrawString(GetCode128A(peLeft.ProductID), font2, brush, DWConver(5), DWConver((2 + 30 * i + 4 * i + 13)));//字体产生条形码 //Code128A:数字+大写字母+特殊字符 Code128B:数字+大小写字母+特殊字符 Code128C:双位数字 //if (i == 0)//图片压缩,不一定有效 //{ // e.Graphics.DrawImage(GetSmall(_Code.GetCodeImage(peLeft.ProductID, WinPrint.Code128.Encode.Code128B), 1.4, 2), DWConver(9), DWConver((2 + 30 * i + 4 * i + 13)));//代码产生条形码 //} //else //{ // e.Graphics.DrawImage(GetSmall(_Code.GetCodeImage(peLeft.ProductID, WinPrint.Code128.Encode.Code128B), 1.4, 1), DWConver(9), DWConver((2 + 30 * i + 4 * i + 13)));//代码产生条形码 //} #endregion //e.Graphics.DrawString(peLeft.ProductID, font1, brush, DWConver(14), DWConver((2 + 30 * i + 4 * i + 24))); //判断是否还有数据 无数据翻页打印 if ((i + 1) == pageCount && (2 * (i + 1) - 1) == listCount) { e.HasMorePages = false; break; } m++; ProductExport peRight = list[m]; //右 方框 //e.Graphics.DrawLine(pen, DWConver(44), DWConver((2 + 30 * i + 4 * i)), DWConver(84), DWConver((2 + 30 * i + 4 * i)));//上横 //e.Graphics.DrawLine(pen, DWConver(44), DWConver((2 + 30 * i + 4 * i)), DWConver(44), DWConver((2 + 30 * (i + 1) + 4 * i)));//左竖 //e.Graphics.DrawLine(pen, DWConver(84), DWConver((2 + 30 * i + 4 * i)), DWConver(84), DWConver((2 + 30 * (i + 1) + 4 * i)));//右竖 //e.Graphics.DrawLine(pen, DWConver(44), DWConver((2 + 30 * (i + 1) + 4 * i)), DWConver(84), DWConver((2 + 30 * (i + 1) + 4 * i)));//下横 Rectangle drawRectRight = new Rectangle(DWConver(47), DWConver((2 + 30 * i + 4 * i + 2)), DWConver(34), DWConver(9)); e.Graphics.DrawString(peRight.ProductName, font1, brush, drawRectRight, sf); //e.Graphics.DrawString(peRight.ProductName, font1, brush, DWConver(47), DWConver((2 + 30 * i + 4 * i + 2))); //e.Graphics.DrawString(peRight.ProductID, font2, brush, DWConver(47), DWConver((2 + 30 * i + 4 * i + 13))); e.Graphics.DrawImage(writer.Write(peRight.ProductID), DWConver(48), DWConver((2 + 30 * i + 4 * i + 13)));//第三方控件产生条形码 //e.Graphics.DrawString(peRight.ProductID, font1, brush, DWConver(56), DWConver((2 + 30 * i + 4 * i + 24))); m++; } n++; listCount = list.Count - (n - 1) * 10; //剩余条数 if (listCount > 0) //判断是否翻页打印 限定每页打印个数,超过即翻页打印 { e.HasMorePages = true; } else { e.HasMorePages = false; } } catch (Exception ex) { e.Cancel = true; e.HasMorePages = false;//不在 MessageBox.Show(string.Format("打印出现异常:{0} 异常详情:{1}", ex.Message, ex.ToString())); } #region 无用代码 //for (int i = 0; i < 2;i++ ) //{ // e.Graphics.DrawLine(pen, 2, 2, 42, 2);//上横 // e.Graphics.DrawLine(pen, 2, 2, 2, 32);//左竖 // e.Graphics.DrawLine(pen, 42, 2, 42, 32);//右竖 // e.Graphics.DrawLine(pen, 2, 32, 42, 32);//下横 // e.Graphics.DrawString("商品名称", font1, brush, 5, 4); // e.Graphics.DrawString("1234567", font2, brush, 9, 12); // e.Graphics.DrawString("商品编号", font1, brush, 14, 26); // e.Graphics.DrawLine(pen, 44, 2, 84, 2);//上横 // e.Graphics.DrawLine(pen, 44, 2, 44, 32);//左竖 // e.Graphics.DrawLine(pen, 84, 2, 84, 32);//右竖 // e.Graphics.DrawLine(pen, 44, 32, 84, 32);//下横 // e.Graphics.DrawLine(pen, 2, 36, 42, 36);//上横 // e.Graphics.DrawLine(pen, 2, 36, 2, 66);//左竖 // e.Graphics.DrawLine(pen, 42, 36, 42, 66);//右竖 // e.Graphics.DrawLine(pen, 2, 66, 42, 66);//下横 // e.HasMorePages = true; //} //m++; //if(m > 2) //{ // e.HasMorePages = false; //} #endregion }