C# 代码实现打印以及各种设置

1.有弹出窗口打印

private void btnPrint_Click(object sender, EventArgs e)
	{
			PrintDocument printDocument1 = new PrintDocument();
            //printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t";//打印机名称设置
            //printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", label_biaoda1.Width, label_biaoda1.Height);//设置纸张大小
            printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
            PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            printPreviewDialog1.Document = printDocument1;
            DialogResult result = printPreviewDialog1.ShowDialog();
            if (result == DialogResult.OK)
                printDocument1.Print();
	}

//以下代码获取到一张用作打印内容的图片
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
        	
            //Image image = Image.FromFile(@"D:\0000-临时文件\4.png");
            Bitmap _NewBitmap = (Bitmap)img;
            /* e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);//调整图片的大小
             e.Graphics.DrawImage(_NewBitmap, 0, 0, 570, 380);*/
			
			//各种调整,消除锯齿
            /*e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            _NewBitmap.SetResolution(583, 390);*/

            e.Graphics.DrawImage(image, 0, 0, image.Width, image.Height);
            //_NewBitmap.Save("D:\\0000-临时文件\\1.png", System.Drawing.Imaging.ImageFormat.Png);//图片保存
        }


2.无弹出窗口打印

内含绘图打印内容,二维码生成,条形码生成。

private void button2_Click(object sender, EventArgs e)
        {

            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(printDocument_PrintA4Page);
            pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t";  //打印机名称
            pd.DefaultPageSettings.Landscape = true;  //设置横向打印,不设置默认是纵向的
            pd.PrintController = new System.Drawing.Printing.StandardPrintController();
            //pd.PrinterSettings.Copies = (short)copies; //打印份数
            pd.Print();

        }

//设置打印的内容
private void PrintDocument_PrintA4Page(object sender, PrintPageEventArgs e)
        {
            LabelInfo ProductLabelInfo = RowChangeLabelInfo();
            //用于打印
            Brush solidBrush = new SolidBrush(Color.Black);//画刷           
            Pen p = new Pen(System.Drawing.Color.Black);   //线条颜色
            Font fntTxt1 = new Font("微软雅黑", 8, FontStyle.Bold);//动态内容字体
            Font fntTxt2 = new Font("微软雅黑", 9, FontStyle.Bold);//动态内容字体
            try
            {
                //居中
                StringFormat sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Center;

                e.Graphics.DrawString(ProductLabelInfo.ProductName, fntTxt2, solidBrush, new Rectangle(110, 90, 116, 30), sf);//point,size 26
                e.Graphics.DrawString(ProductLabelInfo.Quality, fntTxt2, solidBrush, new Rectangle(310, 90, 76, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.SteelGrade, fntTxt1, solidBrush, new Rectangle(110, 120, 116, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.Date, fntTxt1, solidBrush, new Rectangle(310, 120, 76, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.CoilNo, fntTxt1, solidBrush, new Rectangle(110, 150, 116, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.SurfaceQuality, fntTxt1, solidBrush, new Rectangle(310, 150, 76, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.Size, fntTxt1, solidBrush, new Rectangle(110, 180, 116, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.NW, fntTxt1, solidBrush, new Rectangle(310, 180, 76, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.Standard, fntTxt1, solidBrush, new Rectangle(110, 210, 116, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.GW, fntTxt1, solidBrush, new Rectangle(310, 210, 76, 30), sf);

                e.Graphics.DrawString(ProductLabelInfo.SurfaceStructure, fntTxt1, solidBrush, new Rectangle(480, 210, 70, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.ContractNo, fntTxt1, solidBrush, new Rectangle(110, 240, 116, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.WeightMarker, fntTxt2, solidBrush, new Rectangle(310, 240, 76, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.CoatingMass, fntTxt1, solidBrush, new Rectangle(480, 240, 70, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.SurfTreatment, fntTxt2, solidBrush, new Rectangle(110, 270, 116, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.PackagedForm, fntTxt2, solidBrush, new Rectangle(310, 270, 76, 30), sf);
                e.Graphics.DrawString(ProductLabelInfo.ReferenceLength, fntTxt1, solidBrush, new Rectangle(480, 270, 70, 30), sf);//492

                Bitmap bitmap = LabelInfo.CreateQRCode(ProductLabelInfo.ToStringPrint()); //绘制二维码
                bitmap.MakeTransparent(Color.White);//跟随颜色调整
                e.Graphics.DrawImage(bitmap, 350, 30, 240, 240);//二维码所处位置
            }

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

        }

		//生成二维码
        public static Bitmap CreateQRCode(string asset)//字符串内容为二维码内容
        {
            EncodingOptions options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8", //编码
                Width = 80,             //宽度
                Height = 80             //高度
            };
            BarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.QR_CODE,
                Options = options
            };
            return writer.Write(asset);
        }

		//左对齐函数(可用于调整二维码内字符的位置)
        public static string strLeft(string str)
        {
            Encoding coding = Encoding.Default;
            int count = 0;

            foreach (char ch in str.ToCharArray())
            {
                int byteCount = coding.GetByteCount(ch.ToString());
                if (byteCount == 2)
                    count++;
            }

            string result = str.PadRight(18 - count);
            return result;
        }
        
//生成条形码
public static void CreateBarCode(string filePath, string barCodeContent)
        {
            ZXing.Common.EncodingOptions options = new ZXing.Common.EncodingOptions()
            {
                Height = 120,//设置宽高
                Width = 200,
            };
            //生成条形码的图片并保存
            ZXing.BarcodeWriter wr = new ZXing.BarcodeWriter()
            {
                Options = options,//进行指定规格
                Format = ZXing.BarcodeFormat.CODE_128,//条形码的规格
            };
            System.Drawing.Bitmap img = wr.Write(barCodeContent);//生成图片
            img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            img.Dispose();
            System.Diagnostics.Process.Start(filePath);
        }

个人记录,大家可参考。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余余yu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值