健康一贴灵,专注医药行业管理信息化

C# winform e.Graphics.DrawString 旋转打印一例

前段时间的合格证标签打印老是卡纸,车间将纸竖过来放卡纸少很多,程序也要做修改,在原程序上加了以下两行代码;

                e.Graphics.TranslateTransform(285, 685);
                e.Graphics.RotateTransform(-90.0F);

第一行的两个坐标,要一点一点调试,没有找到什么科学的规律,(可能和我用喷墨打印机的居中打印有关系,在PDF虚拟机中测试了几十次才勉强看到效果)

第二行的,-90是向左转90度。如果改成 90 是向右转90度;

 附:打印代码

           try
            {

                string rq = DateTime.Now.ToString("yyyy-MM-dd");
                int num1 = 200;
                //标签文字开始X点
                int x0 = 125;
                //int x1 = 125;  小票打印机位置  
                int x1 = 350;
                int x2 = 360;

                int y1 = 70;
                int yStep = 39;  //行间距
                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;
                Pen pen = new Pen(Color.Black, 1f);
                Font font = new Font("黑体", 18f, FontStyle.Bold);
                Font font2 = new Font("宋体", 16f, FontStyle.Bold);
                Font font3 = new Font("宋体", 15f, FontStyle.Bold);

                Brush fontColor;
                if (ckbBlack.Checked)
                {
                    fontColor = Brushes.Black;
                }
                else
                {
                    fontColor = Brushes.Red;
                }
                 //Brush black = Brushes.Black;
                

                //创建黑色笔。   
                Pen blackPen = new Pen(Color.Black, 1);

                // 绘制到屏幕上。
                //加入旋转代码,仅供酊剂车间使用。2024-07-12
                //宽9.5CM ,高6.5cm


                //e.Graphics.TranslateTransform(555,505); // 已经有效果,能看到打印的文字了,285,505效果居中
                e.Graphics.TranslateTransform(285, 575);
                e.Graphics.RotateTransform(-90.0F);
                //e.Graphics.RotateTransform(90.0F);

                //品名
                e.Graphics.DrawString(txtName.Text, font2, fontColor, x1, y1);
                y1 = y1 + yStep;
                 //批号
                e.Graphics.DrawString(txtBatchNo.Text, font2, fontColor, x1, y1);
                y1 = y1 + yStep;
                //检验员
                e.Graphics.DrawString(txtChecker.Text, font2, fontColor, x1-10, y1);
                e.Graphics.DrawString(txtOperator.Text, font3, fontColor, x1+128, y1);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

 

posted @ 2024-07-12 15:29  一贴灵  阅读(6)  评论(0编辑  收藏  举报
学以致用,效率第一