private static void PrintBarCode(FieldModel fm, int adjustHeight, PrintPageEventArgs e) { try { if (string.IsNullOrEmpty(fm.BindingText)) { return; } int locationY = fm.LocationY + adjustHeight; var pickNoBarcode = new BarcodeX { Data = fm.BindingText, ShowText = false, Symbology = bcType.Code128 }; e.Graphics.DrawImage(pickNoBarcode.Image(fm.TextAreaWidth, fm.TextAreaHeight), fm.LocationX, locationY); } finally { CurrentPrintPage.PrintEntity.Fields.Remove(fm); } }
private static void CurrentPrinter_PrintPage(object sender, PrintPageEventArgs e) { //如果是打印快递单,先加载销售平台的底图 if (CurrentPrintPage.About == PrintAbout.Express && CurrentPrintPage.PrintEntity.Extends["SaleFilialeId"] != null) { PrintImage(new FieldModel() { FieldName = "express", BindingText = CurrentPrintPage.PrintEntity.Extends["SaleFilialeId"].ToString(), LocationX = 0, LocationY = 0, TextAreaWidth = CurrentPrintPage.DefaultPageSettings.PaperSize.Width, TextAreaHeight = CurrentPrintPage.DefaultPageSettings.PaperSize.Height }, 0, e); } //高度调整,如果数据块高度和设计的高度有差别,则adjustHeight保存差别值 int adjustHeight = 0; if (CurrentPrintPage.BlockChangeToNewPage) { //如果换页,偏移量是第一个打印数据的高度 FieldModel offsetFm = CurrentPrintPage.PrintEntity.Fields.OrderBy(f => f.LocationY).OrderBy(f => f.LocationX).FirstOrDefault(); if (offsetFm != null) { adjustHeight = -offsetFm.LocationY + 20; } } //先打印所有数据块之前的字段 FieldModel dbfm = CurrentPrintPage.PrintEntity.Fields.Find(f => f.FieldType == FieldType.DataBlock); if (dbfm != null) { List <FieldModel> beforedbfmData = CurrentPrintPage.PrintEntity.Fields.Where(f => f.LocationY < dbfm.LocationY).ToList(); foreach (FieldModel fm in beforedbfmData) { switch (fm.FieldType) { case FieldType.String: PrintString(fm, adjustHeight, e); break; case FieldType.Image: PrintImage(fm, adjustHeight, e); break; case FieldType.Line: PrintLine(fm, adjustHeight, DashStyle.Solid, e); break; case FieldType.BarCode: PrintBarCode(fm, adjustHeight, e); break; } } //然后打印数据块 adjustHeight = PrintDataBlock(dbfm, CurrentPrintPage.PrintEntity.DataEntity, e); //打印数据块需要换页 if (CurrentPrintPage.DataBlockContinue) { return; } } //首先判断剩下的纸张高度够不够打印 如果adjustPoint.Y > 0,且 不分页块+留白块 < 剩下的打印空间,则换页 //(暂时将数据块后所有内容表示为不分页块,以后拓展程序可以改进) if (adjustHeight > 0) { CurrentPrintPage.BlockChangeToNewPage = e.HasMorePages = true; return; } //先对字段进行排序,剩下的字段从上往下顺序打 CurrentPrintPage.PrintEntity.Fields = CurrentPrintPage.PrintEntity.Fields. OrderBy(f => f.LocationY).OrderBy(f => f.LocationX).ToList(); for (int i = 0; i < CurrentPrintPage.PrintEntity.Fields.Count;) { FieldModel fm = CurrentPrintPage.PrintEntity.Fields[0]; switch (fm.FieldType) { case FieldType.String: PrintString(fm, adjustHeight, e); break; case FieldType.Image: PrintImage(fm, adjustHeight, e); break; case FieldType.Line: PrintLine(fm, adjustHeight, DashStyle.Solid, e); break; case FieldType.BarCode: PrintBarCode(fm, adjustHeight, e); break; } } //最后是留白部分(发货单专享打印图片和电子发票,先照搬之前的代码) if (CurrentPrintPage.About == PrintAbout.Order && CurrentPrintPage.PrintEntity.PageModel.BlankHeight > 0) { if (CurrentPrintPage.PrintEntity.Extends["OrderNos"] != null && CurrentPrintPage.PrintEntity.Extends["SalePlatformId"] != null && CurrentPrintPage.PrintEntity.Extends["DimensionalCodeDto"] != null) { var orderNos = CurrentPrintPage.PrintEntity.Extends["OrderNos"].ToString(); var salePlatformId = CurrentPrintPage.PrintEntity.Extends["SalePlatformId"].ToString(); var printType = (DataEntity.PrintType)CurrentPrintPage.PrintEntity.Extends["PrintType"]; var dimensionalCodeDto = CurrentPrintPage.PrintEntity.Extends["DimensionalCodeDto"] as DimensionalCodeDTO; int barCodeLeft; int currentY = CurrentPrintPage.PrintEntity.PageModel.Height - (int)CommonFunction.Pix2Millimeter(CurrentPrintPage.PrintEntity.PageModel.BlankHeight); QRCodeService.PrintEInvoiceAndQRCode(e, orderNos, salePlatformId, dimensionalCodeDto, ref currentY, out barCodeLeft); #region [打印总单号条形码] if (printType == DataEntity.PrintType.Auto && CurrentPrintPage.PrintEntity.Extends["TotalBillNo"] != null) { var totalBillNo = CurrentPrintPage.PrintEntity.Extends["TotalBillNo"].ToString(); var pickNoBarcode = new BarcodeX { Data = totalBillNo, ShowText = false, Symbology = bcType.Code128 }; e.Graphics.DrawImage(pickNoBarcode.Image(160, 40), CommonFunction.Millimeter2Pix(barCodeLeft), CommonFunction.Millimeter2Pix(currentY)); } #endregion } } CurrentPrintPage.BlockChangeToNewPage = e.HasMorePages = false; if (CurrentPrintPage.PrintEntity.Fields.Count > 0) { throw new Exception("打印数据不完全"); } }