C#打印标签

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

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

        /// <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.xlsthis

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();

 补充:spa

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

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报表中的打印功能。code

相关文章
相关标签/搜索