C#打印标签


一个复杂的标签包括一个复杂的表格样式和二维码、条形码等内容。所以如果直接绘制的方式将会非常的麻烦,所以采用使用的方案是使用模板的方式:1.使用Excel创建出想要的模板的样式。2.对模板中的动态内容进行填充。3.打印Excel

一.ZXing.net(可以方便生成条形码、二维码和带有头像的二维码)

        /// <summary>
        /// 条形码
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private Bitmap Create1Code(string text)
        {
            BarcodeWriter writer = new BarcodeWriter();
            //使用ITF 格式,不能被现在常用的支付宝、微信扫出来
            //如果想生成可识别的可以使用 CODE_128 格式
            //writer.Format = BarcodeFormat.ITF;
            writer.Format = BarcodeFormat.CODE_128;
            EncodingOptions options = new EncodingOptions()
            {
                Width = 450,
                Height = 100,
            };
            writer.Options = options;
            Bitmap map = writer.Write(text);
            return map;
        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private Bitmap Create2Code(string text)
        {

            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions();
            options.DisableECI = true;
            //设置内容编码
            options.CharacterSet = "UTF-8";
            //设置二维码的宽度和高度
            options.Width = 180;
            options.Height = 180;
            //设置二维码的边距,单位不是固定像素
            options.Margin = 0;
            writer.Options = options;
            Bitmap map = writer.Write(text);
            return map;
        }

 

二.Epplus(可以方便的对Excel内容进行设置,导出,同时可以对Excel打印时的参数进行设置,遗憾的是他并没有集成相应的API)

 using (FileStream fileStream = new FileStream(TemplatePath, FileMode.Open))
            {
                using (ExcelPackage package = new ExcelPackage(fileStream))
                {
                    OfficeOpenXml.ExcelWorksheet sheet = package.Workbook.Worksheets["Sheet1"];
                    //当设置Excel中不存在的字体的时候,比如设置的字体过小的时候,会出现Dictionary不存在键值的错误

                    //填充第一行第一列的内容
                    sheet.Cells[1, 1].Value =“测试内容”;
                    sheet.Cells[1, 1].Style.Font.Name =“宋体”;
                    sheet.Cells[1, 1].Style.Font.Size = 10.5;
                    sheet.Cells[1, 1].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Bottom;
                    //设置打印格式
                    sheet.PrinterSettings.LeftMargin = 0.3m;
                    sheet.PrinterSettings.PaperSize = ePaperSize.A3;
                    sheet.PrinterSettings.TopMargin = 0.3m;
                    //填充二维码。
                    var map = Create2Code(“二维码测试”);
                    var pic = sheet.Drawings.AddPicture("1", map);
                    pic2.SetPosition(1, 0, 3, 0);
                    using (Stream stream = new FileStream(ExcelPath, FileMode.Create))
                    {
                        package.SaveAs(stream);
                    }
                }
            }

 

三.使用GemBox插件进行打印(这个插件对打印功能非常简单,只是集成了一个打印功能API提供打印而已,无法对打印机的属性设置)

               SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
              var workbook = ExcelFile.Load(ExcelPath);
              workbook.Print(cboPrinter.Text);

 通过上面的功能已经可以完成基本的功能。但是存在的不足就是不可以直接设置打印机的属性,有一款收费的插件可以实现,免费版的也可以使用打印的功能,但是会在文档的上面生成水印---Spire.xls

Spire.XLS是一个非常强大的插件,可以对Excel进行出来里,同时提供了相应的接口可以和Windows中打印的PrintDocument直接对接,可以设置打印机的属性。

 

            Workbook workbook = new Workbook();
            workbook.LoadFromFile(ExcelPath);
            Worksheet sheet = workbook.Worksheets[0];
             sheet.PageSetup.PrintArea = "C7:D7";
            sheet.PageSetup.PrintTitleRows = "$1:$1";
            sheet.PageSetup.FitToPagesWide = 1;
            sheet.PageSetup.FitToPagesTall = 1;
            sheet.PageSetup.Orientation = PageOrientationType.Landscape;
            sheet.PageSetup.PaperSize = PaperSizeType.PaperA3;
             PrintDocument pd = workbook.PrintDocument;
             pd.PrinterSettings.PrinterName = cboPrint.Text;
             pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
             pd.Print();

 补充:

1.关于打印,微软还提供了弹出对话框模式的打印PrintDialog.

2.关于微软的打印PrintDocument支持直接使用Graphics直接绘图,简单的样式可以考虑

     PrintDocument pd = new PrintDocument();
     pd.PrintPage += new PrintPageEventHandler
                    (this.pd_PrintPage);
     private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            ev.Graphics.DrawImage(Bitmap.FromFile(pngPath), new Point(0, 0));
            
        }

3.和使用Excel模板一样的思路,可以使用微软的报表模板RDLC制作标签样式,使用RDLC报表中的打印功能。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM