Goran Bibic

Goran Bibic

  • 457
  • 2.9k
  • 192k

Print receipt from pos printer and add datagrid values c#

Jan 21 2019 10:41 AM

Datagrid rows number, name, tax and price

Need to add values on receipt from datagrid to PrintReceiptPage on line 126

Some help?

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Drawing.Printing;  
  7. using System.Linq;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11.   
  12. namespace POS_TEST  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public static string barKod = "";  
  17.         public static string osobaIme = "";  
  18.         public static string datumUlaska = "";  
  19.         public static string osobaFirma = "";  
  20.   
  21.   
  22.         public Form1()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.   
  27.         private void button1_Click(object sender, EventArgs e)  
  28.         {  
  29.             BarCodePrint.PrintReceiptForTransaction(barKod, osobaComboBox.Text, vrijemePrijavePicker.Value.ToString("dd.MM.yyyy. HH:mm:00"), firmaComboBox.Text);  
  30.   
  31.         }  
  32.   
  33.   
  34.         private static void PrintReceiptPage(object sender, PrintPageEventArgs e)  
  35.         {  
  36.   
  37.         }  
  38.   
  39.         class BarCodePrint  
  40.         {  
  41.             public static string barKod = "";  
  42.             public static string osobaIme = "";  
  43.             public static string datumUlaska = "";  
  44.             public static string osobaFirma = "";  
  45.             public static void PrintReceiptForTransaction(string bK, string osoba, string datum, string firma)  
  46.             {  
  47.                 barKod = bK;  
  48.                 osobaIme = osoba;  
  49.                 datumUlaska = datum;  
  50.                 osobaFirma = firma;  
  51.                 PrintDocument recordDoc = new PrintDocument();  
  52.   
  53.                 recordDoc.DocumentName = "BarKod gosta";  
  54.                 recordDoc.PrintPage += new PrintPageEventHandler(BarCodePrint.PrintReceiptPage); // function below  
  55.                 recordDoc.PrintController = new StandardPrintController(); // hides status dialog popup  
  56.                                                                            // Comment if debugging   
  57.                 PrinterSettings ps = new PrinterSettings();  
  58.                 ps.PrinterName = "POS-58";  
  59.                 recordDoc.PrinterSettings = ps;  
  60.                 recordDoc.Print();  
  61.                 // --------------------------------------  
  62.                 // Uncomment if debugging - shows dialog instead  
  63.                 //PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();  
  64.                 //printPrvDlg.Document = recordDoc;  
  65.                 //printPrvDlg.Width = 1200;  
  66.                 //printPrvDlg.Height = 800;  
  67.                 //printPrvDlg.ShowDialog();  
  68.                 // --------------------------------------  
  69.                 recordDoc.Dispose();  
  70.   
  71.             }  
  72.   
  73.   
  74.   
  75.   
  76.   
  77.             private static void PrintReceiptPage(object sender, PrintPageEventArgs e)  
  78.             {  
  79.                 float x = 10;  
  80.                 float y = 5;  
  81.                 float width = 170.0F; // max width I found through trial and error  
  82.                 float height = 0F;  
  83.   
  84.                 Font drawFontArial12Bold = new Font("Arial", 10, FontStyle.Bold);  
  85.                 Font drawFontArial11Regular = new Font("Arial", 8, FontStyle.Regular);  
  86.                 Font drawFontArial7Regular = new Font("Arial", 8, FontStyle.Regular);  
  87.                 Font drawFontArial7Italic = new Font("Arial", 8, FontStyle.Italic);  
  88.                 SolidBrush drawBrush = new SolidBrush(Color.Black);  
  89.   
  90.                 // Set format of string.  
  91.                 StringFormat drawFormatCenter = new StringFormat();  
  92.                 drawFormatCenter.Alignment = StringAlignment.Center;  
  93.                 StringFormat drawFormatLeft = new StringFormat();  
  94.                 drawFormatLeft.Alignment = StringAlignment.Near;  
  95.                 StringFormat drawFormatRight = new StringFormat();  
  96.                 drawFormatRight.Alignment = StringAlignment.Far;  
  97.   
  98.                 // Draw string to screen.  
  99.   
  100.                 string text = "T.R. LAD S.P.";  
  101.                 e.Graphics.DrawString(text, drawFontArial12Bold, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  102.                 y += e.Graphics.MeasureString(text, drawFontArial12Bold).Height;  
  103.   
  104.                 text = "22. BRIGADE VRS, Kotor Varoš";  
  105.                 e.Graphics.DrawString(text, drawFontArial7Italic, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  106.                 y += e.Graphics.MeasureString(text, drawFontArial7Italic).Height;  
  107.   
  108.                 text = "- MALOPRODAJNI RACUN -\n";  
  109.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  110.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;  
  111.   
  112.   
  113.   
  114.                 text = " ";  
  115.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  116.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;  
  117.                 text = " ";  
  118.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  119.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;  
  120.   
  121.   
  122.                 text = " ";  
  123.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  124.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;  
  125.   
  126.   
  127.   
  128.                 // ... and so on  
  129.   
  130.                 //BarcodeDraw bdraw = BarcodeDrawFactory.GetSymbology(BarcodeSymbology.Code128);  
  131.                 //Image barCodeImeage = bdraw.Draw(barKod, 60);  
  132.                 //Console.WriteLine(x + " " + y + " " + width + " " + height);  
  133.                 //e.Graphics.DrawImage(barCodeImeage, new RectangleF(x, y, width, 60));  
  134.                 //y += 65;  
  135.                 //Console.WriteLine(barCodeImeage.Size);  
  136.   
  137.                 text = osobaIme;  
  138.                 e.Graphics.DrawString(text, drawFontArial11Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  139.                 y += e.Graphics.MeasureString(text, drawFontArial11Regular).Height;  
  140.   
  141.                 //text = osobaFirma;  
  142.                 //e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  143.                 //y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;  
  144.   
  145.   
  146.                 text = "----------------------------------------";  
  147.                 e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  148.                 y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;  
  149.   
  150.                 text = "Datum: " + DateTime.Now.ToString();  
  151.                 e.Graphics.DrawString(text, drawFontArial7Italic, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  152.                 y += e.Graphics.MeasureString(text, drawFontArial7Italic).Height;  
  153.   
  154.   
  155.   
  156.                 text = "----------------------------------------";  
  157.                 e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  158.                 y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;  
  159.   
  160.   
  161.                 text = "HVALA VAM NA POSJETI";  
  162.                 e.Graphics.DrawString(text, drawFontArial7Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);  
  163.                 y += e.Graphics.MeasureString(text, drawFontArial7Regular).Height;  
  164.             }  
  165.         }  
  166.   
  167.         private void roba_uslugeBindingNavigatorSaveItem_Click(object sender, EventArgs e)  
  168.         {  
  169.             this.Validate();  
  170.             this.roba_uslugeBindingSource.EndEdit();  
  171.             this.tableAdapterManager.UpdateAll(this.lAD_2019DataSet);  
  172.   
  173.         }  
  174.   
  175.         private void Form1_Load(object sender, EventArgs e)  
  176.         {  
  177.             // TODO: This line of code loads data into the 'lAD_2019DataSet.roba_usluge' table. You can move, or remove it, as needed.  
  178.             this.roba_uslugeTableAdapter.Fill(this.lAD_2019DataSet.roba_usluge);  
  179.   
  180.         }  
  181.     }