首页 > 代码库 > PrintDocument or PrintPreviewDialog 打印

PrintDocument or PrintPreviewDialog 打印

 

  /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnPreview_Click(object sender, EventArgs e)        {
       PackTypeReport report = null; _printTable
= GetPrintTable(); report.PrintData = _printTable; report.Preview(); } /// <summary> /// 打印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrint_Click(object sender, EventArgs e) {
PackTypeReport report = null; _printTable
= GetPrintTable(); report.PrintData = _printTable; report.Print(); }

 

 

 /// <summary>    /// 电子条码打印    /// </summary>    public class PackTypeReport    {        /// <summary>        /// 打印文档        /// </summary>        PrintDocument _printDoc;        /// <summary>        /// 打印预览窗体        /// </summary>        PrintPreviewDialog _printPreview;        /// <summary>        /// A4纸宽度        /// </summary>        const float PageWidth = 840f;//210mm        /// <summary>        /// A4纸高度        /// </summary>        const float PageHeight = 1188f;//297mm        const float PaddingVertical = 20f;//上下边距        const float PaddingHorizontal = 40f;//左右边距        const float BarCodeHeight = 50;//条码高度        const float ItemPadding = 5;//项目内部项上下间隔        private float BarCodeTop = PaddingVertical;        private float _barcodeLeft = PaddingHorizontal;        private float _barcodeWidth = 200;//每个条码的间隔量        private float _ItemHeight = 40;//每个项目间的上下间隔        private float CodeNameTHeight = 0f;//条码(包括下边距)和包名总高度,窗体加载中设置        private int PageIndex = 1;//打印当前页        private string Title = "器械包编码";//打印显示的标题;        private DataRow[] printRowsForC = null;//字母检索数据行集合        private Font TitleFont = new Font("Arital", 16f, FontStyle.Bold);//标题字体        private Font CodeFont = new Font("Arital", 10f);//条码字体        private Font NameFont = new Font("Arital", 12f);//包名字体        private char letter = 1;//非字母标实        private int rowIndex = 0;//行索引        private List<DataRow> printRowsForD = null;//非字母检索数据行集合        private DataTable _printTable = null;        /// <summary>        /// 打印数据        /// </summary>        public DataTable PrintData        {            set            {                _printTable = value;            }        }        /// <summary>        ///         /// </summary>        public PackTypeReport(PrintDocument pd, PrintPreviewDialog prd)        {            _printDoc = pd;            _printPreview = prd;            CodeNameTHeight = GetCodeAndNameHeight();            _printPreview.Width = 794;            _printPreview.Height = 506;            _printDoc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(20, 20, 25, 25);            _printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", (int)PageWidth, (int)PageHeight);            _printDoc.PrintPage += new PrintPageEventHandler(DrawPage);        }        /// <summary>        /// 打印        /// </summary>        public void Print()        {            _printDoc.Print();        }        /// <summary>        /// 预览        /// </summary>        public void Preview()        {            _printPreview.ShowDialog();        }        //条码(包括下边距)和包名总高度        private float GetCodeAndNameHeight()        {            return TitleFont.GetHeight() + ItemPadding + BarCodeHeight;        }        /// <summary>        /// 开始绘制        /// </summary>        private void DrawPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)        {            //定位到页头            BarCodeTop = PaddingVertical;            //临时的下一个定位点,用于绘制包名条码             float LSTop = 0f;            //第一页需要绘制标题            if (PageIndex == 1)            {                DrawTitle(e.Graphics, Title, ref BarCodeTop);            }            float y = 0;            while (BarCodeTop < PageHeight - PaddingVertical)            {                if (letter == 1)                {                    //检索非字母优化设置                    if (printRowsForD == null)                    {                        printRowsForD = new List<DataRow>();                        foreach (DataRow dr in _printTable.Select("", "name asc"))                        {                            if (dr["Spell"].ToString() == "" || !char.IsLetter(dr["Spell"].ToString()[0]))                            {                                printRowsForD.Add(dr);                            }                        }                        //0个非字母数据时,将转入字母A开始的数据,并初始化行索引rowIndex                        if (printRowsForD.Count == 0)                        {                            letter = A;                            rowIndex = 0;                        }                    }                    else                    {                        //行索引在指定行内                        if (rowIndex < printRowsForD.Count)                        {                            string packName = printRowsForD[rowIndex]["Name"].ToString();                            string barcode = printRowsForD[rowIndex]["Bar"].ToString();                            if (rowIndex % 4 == 0)                            {                                //判断包名和条码组合高度是否超出指定高度                                _barcodeLeft = PaddingHorizontal;                                BarCodeTop += y + _ItemHeight;//加上Y偏移                                if (BarCodeTop + CodeNameTHeight >= PageHeight - PaddingVertical)                                {                                    break;                                }                                else                                {                                    y = DrawCodeAndName(e.Graphics, packName, barcode,letter, _barcodeLeft, BarCodeTop);                                    LSTop = y + BarCodeTop + 10;                                }                            }                            else                            {                                y = DrawCodeAndName(e.Graphics, packName, barcode, 1,_barcodeLeft, BarCodeTop);                            }                            _barcodeLeft += _barcodeWidth;                            rowIndex++;                        }                        else                        {                            //数据全部加载完设置                            printRowsForD = null;                            letter = A;                            //奇偶条码终点定位设置,奇数需要跳转到下一个定位点高度                            if ((rowIndex - 1) % 4 == 0)                            {                                BarCodeTop = LSTop;                                y = 0;//Y偏移量已经加在LSTOP上                            }                            rowIndex = 0;                            //类型名与条码包名的组合高度超过指定高度,需要跳转到下一页,用于分类名显示不会显示在页尾的情况                            if (BarCodeTop + CodeNameTHeight >= PageHeight - PaddingVertical)                            {                                break;                            }                        }                    }                }                else                {                    //字母检索优化设置                    if (printRowsForC == null || printRowsForC.Length == 0)                    {                        //判断当前定位点是否能够包含类型名绘制点                        if (BarCodeTop >= PageHeight - PaddingVertical)                        {                            break;                        }                        else                        {                            printRowsForC = _printTable.Select(string.Format("Spell like ‘{0}%‘", letter), "Name asc");                            //判断该分类数据是否存在,存在时显示其分类名                            if (printRowsForC.Length == 0)                            {//如果该分类不存在数据,且不是Z字母时,初始化行索引和进入下一个字母,若为Z,初始化后退出循环                                if (letter < Z)                                {                                    rowIndex = 0;                                    letter = (char)(letter + 1);                                }                                else                                {                                    rowIndex = 0;                                    letter = (char)(letter + 1);                                    printRowsForC = null;                                    break;                                }                            }                        }                    }                    else                    {                        //行索引在指定行内                        if (rowIndex < printRowsForC.Length)                        {                            string packName = printRowsForC[rowIndex]["Name"].ToString();                            string barcode = printRowsForC[rowIndex]["Bar"].ToString();                            if (rowIndex % 4 == 0)                            {                                BarCodeTop += y + _ItemHeight;                                _barcodeLeft = PaddingHorizontal;                                //判断包名和条码组合高度是否超出指定高度                                if (BarCodeTop + CodeNameTHeight >= PageHeight - PaddingVertical)                                {                                    break;                                }                                else                                {                                    y = DrawCodeAndName(e.Graphics, packName, barcode, letter, _barcodeLeft, BarCodeTop);                                    LSTop = y + BarCodeTop + 10;                                }                            }                            else                            {                                y = DrawCodeAndName(e.Graphics, packName, barcode, 1, _barcodeLeft, BarCodeTop);                            }                            _barcodeLeft += _barcodeWidth;                            rowIndex++;                        }                        else                        {                            //加载该字母分类数据完毕时,需要判断该字母是否为字母Z。若不是,进入奇偶条码的下一个定位点判断,并初始化。若是,则设置字符为Z的下一个,并退出循环                            if (letter >= Z)                            {                                letter = (char)(letter + 1);                                break;                            }                            else if ((rowIndex - 1) % 4 == 0)                            {                                BarCodeTop = LSTop;                                y = 0;                            }                            rowIndex = 0;                            printRowsForC = null;                            letter = (char)(letter + 1);                            //类型名与条码包名的组合高度超过指定高度,需要跳转到下一页,用于分类名显示不会显示在页尾的情况                            if (BarCodeTop + CodeNameTHeight >= PageHeight - PaddingVertical)                            {                                break;                            }                        }                    }                }            }            PageIndex++;            //当退出循环时,使用字符判断是否为Z的下一个字符,来确定当前页是否是最后一页            if (letter <= Z)            {                e.HasMorePages = true;            }            else            {                PageIndex = 1;                letter = 1;                rowIndex = 0;                printRowsForD = null;                printRowsForC = null;                e.HasMorePages = false;            }        }        /// <summary>        /// 绘制标题        /// </summary>        private void DrawTitle(Graphics g, string strTitle, ref float y)        {            float width = g.MeasureString(strTitle, TitleFont).Width;            float left = (PageWidth - width) / 2;            g.DrawString(strTitle, TitleFont, Brushes.Black, left, PaddingHorizontal);            y += TitleFont.GetHeight() + ItemPadding;        }        /// <summary>        /// 绘制条码和包名,返回下边纵坐标        /// </summary>        /// <returns>float</returns>        private float DrawCodeAndName(Graphics g, string name, string code,char letter, float x, float y)        {            float top = 0;            float y1 = 0;            string name1 = string.Empty;            string name2 = string.Empty;            if (name.Length > 10)            {                name1 = name.Substring(0, 10);                name2 = name.Substring(10);            }            else            {                name2 = " ";                name1 = name;            }            if (letter != 1)            {                g.DrawString(Convert.ToString(letter), NameFont, Brushes.Black, x, y);                y1 = NameFont.GetHeight() + ItemPadding;                top = y1;                y += y1;            }            else            {                g.DrawString("", NameFont, Brushes.Black, x, y);                y1 = NameFont.GetHeight() + ItemPadding;                top = y1;                y += y1;            }            g.DrawString(name1, NameFont, Brushes.Black, x, y);            y1 = NameFont.GetHeight() + ItemPadding;            top = y1;            y += y1;            g.DrawString(name2, NameFont, Brushes.Black, x, y);            top += y1;            y += y1;            float y2 = 0;            g.DrawString(code, CodeFont, Brushes.Black, x, y);            y2 = CodeFont.GetHeight() + ItemPadding;            top += y2;            y += y2;            Image codeImg = BLL.Report.Print.IReport.GetBarcodeImg(code);            g.DrawImage(codeImg, x, y);            top += codeImg.Height + ItemPadding;            return top;        }    }

 

PrintDocument or PrintPreviewDialog 打印