0 votes
818 views
How to give a test print to a thermal printer ?
asked May 6, 2016 in Computer Science - IT by slow_learner | 818 views

1 Answer

0 votes
Selected Ans.
   private Font printFont;
        private StreamReader streamToPrint;

        static string filePath;
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            String line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);

            // Iterate over the file, printing each line.
            while (count < linesPerPage &&
               ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Print string
                streamToPrint = new StreamReader(@"D:\Q.txt");
                //Print Image
                //System.Drawing.Image img = System.Drawing.Image.FromFile("D:\\e.png");
               // Point loc = new Point(100, 100);
               // e.Graphics.DrawImage(img, loc);
                try
                {
                    printFont = new Font("squeaky chalk sound", 10);
                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                    // Print the document.
                    pd.PrinterSettings.PrinterName = "Thermal Printer Name goes here";
                    
                    pd.Print();
                }
                finally
                {
                   // streamToPrint.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
answered May 6, 2016 by slow_learner