locked
C# Printing a String to printer...... RRS feed

  • Question

  • Ok I have looked all over the web ... And found so many ways to do this .... it's not funny...

    I just want the simplest way to print a string to a printer.. I have the

    using

    System.Drawing;

    using

    System.Drawing.Printing;

    at the top and added .... PrintDocument and Print Dialog and Print preview to the form.... Here is what I'm trying to print... As you can see I'm getting things from textbox and converting them to string in this...

     "Cost" + System.DateTime.Today.ToShortDateString() + Environment.NewLine +
       Environment.NewLine +
       txtRow1.Text + Environment.NewLine + Environment.NewLine + txtRow2.Text + Environment.NewLine + Environment.NewLine +  txtRow3.Text+ Environment.NewLine+                                          

     Environment.NewLine + txtRow4.Text + Environment.NewLine + Environment.NewLine + Environment.NewLine +
     "Count:" + (TotalNumcounter / 4).ToString() + "                       " + "Total Count:" + TotalNumcounter.ToString() + Environment.NewLine +
     "50c" + ((TotalNumcounter / 4) * .50).ToString("C") + "                              " + "50c" + (TotalNumcounter * .50).ToString("C") + Environment.NewLine +
     "$1.00" + ((TotalNumcounter / 4) * 1.00).ToString("C") + "                         " + "$1.00" + (TotalNumcounter * 1.00).ToString("C") + Environment.NewLine +
     "$5.00" + ((TotalNumcounter / 4) * 5.00).ToString("C") + "                         " + "$5.00 " + (TotalNumcounter * 5.00).ToString("C") + Environment.NewLine +
      Environment.NewLine +
      "-------------------------------------------------------------------------------------------------" + Environment.NewLine);

    I need some sample code to go by ... new to C# printing thing...

    Thanks
     

    Sunday, December 6, 2009 4:49 PM

Answers

  • Hi,

    This should be working

            private void btnPrint_Click(object sender, EventArgs e)
            {
                string s = "Cost Bla  Bla Bla.....";
    
                PrintDocument p = new PrintDocument();
                p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
                {
                    e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
    
                };
                try
                {
                    p.Print();
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception Occured While Printing", ex);
                }
            }
    • Marked as answer by superlurker Sunday, December 6, 2009 8:55 PM
    Sunday, December 6, 2009 5:58 PM

All replies

  • HI,

    You can try

      PrintDocument p = new PrintDocument();
                p.PrintPage += delegate(object sender, PrintPageEventArgs e)
                {
                    int i=0;
                    foreach (string s in arr)
                    {
                        i++;
                        e.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new ReftangleF(0,0,p.Width,p.Height));
                    }
                };
                try
                {
                    p.Print();
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception Occured While Printing", ex);
                }
    
    Sunday, December 6, 2009 4:54 PM
  • Were would you put the 

     "Cost" + System.DateTime.Today.ToShortDateString() + Environment.NewLine +
       Environment.NewLine +
       txtRow1.Text + Environment.NewLine + Environment.NewLine + txtRow2.Text + Environment.NewLine + Environment.NewLine +  txtRow3.Text+ Environment.NewLine+                                          

     Environment.NewLine + txtRow4.Text + Environment.NewLine + Environment.NewLine + Environment.NewLine +
     "Count:" + (TotalNumcounter / 4).ToString() + "                       " + "Total Count:" + TotalNumcounter.ToString() + Environment.NewLine +
     "50c" + ((TotalNumcounter / 4) * .50).ToString("C") + "                              " + "50c" + (TotalNumcounter * .50).ToString("C") + Environment.NewLine +
     "$1.00" + ((TotalNumcounter / 4) * 1.00).ToString("C") + "                         " + "$1.00" + (TotalNumcounter * 1.00).ToString("C") + Environment.NewLine +
     "$5.00" + ((TotalNumcounter / 4) * 5.00).ToString("C") + "                         " + "$5.00 " + (TotalNumcounter * 5.00).ToString("C") + Environment.NewLine +
      Environment.NewLine +
      "-------------------------------------------------------------------------------------------------" + Environment.NewLine);

    At in this ???

    Sunday, December 6, 2009 5:20 PM
  • Could you try this,

    string s="Cost" + System.DateTime.Today.ToShortDateString() + Environment.NewLine +
       Environment.NewLine +
       txtRow1.Text + Environment.NewLine + Environment.NewLine + txtRow2.Text + Environment.NewLine + Environment.NewLine +  txtRow3.Text+ Environment.NewLine+                                          

     Environment.NewLine + txtRow4.Text + Environment.NewLine + Environment.NewLine + Environment.NewLine +
     "Count:" + (TotalNumcounter / 4).ToString() + "                       " + "Total Count:" + TotalNumcounter.ToString() + Environment.NewLine +
     "50c" + ((TotalNumcounter / 4) * .50).ToString("C") + "                              " + "50c" + (TotalNumcounter * .50).ToString("C") + Environment.NewLine +
     "$1.00" + ((TotalNumcounter / 4) * 1.00).ToString("C") + "                         " + "$1.00" + (TotalNumcounter * 1.00).ToString("C") + Environment.NewLine +
     "$5.00" + ((TotalNumcounter / 4) * 5.00).ToString("C") + "                         " + "$5.00 " + (TotalNumcounter * 5.00).ToString("C") + Environment.NewLine +
      Environment.NewLine +
      "-------------------------------------------------------------------------------------------------" + Environment.NewLine;


     PrintDocument p = new PrintDocument();
                p.PrintPage += delegate(object sender, PrintPageEventArgs e)
                {
                                      e.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new ReftangleF(0,0,p.Width,p.Height));
                   
                };
                try
                {
                    p.Print();
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception Occured While Printing", ex);
                }

    Sunday, December 6, 2009 5:22 PM
  • So should it look like this..

    private void btnPrint_Click(object sender, EventArgs e)
    {
    string s="Cost" Bla  Bla Bla.....";

     PrintDocument p = new PrintDocument();
                p.PrintPage += delegate(object sender, PrintPageEventArgs e)
                {
                                      e.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new ReftangleF(0,0,p.Width,p.Height));
                   
                };
                try
                {
                    p.Print();
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception Occured While Printing", ex);
                }

    }

    Sunday, December 6, 2009 5:27 PM
  • Hi,

    This should be working

            private void btnPrint_Click(object sender, EventArgs e)
            {
                string s = "Cost Bla  Bla Bla.....";
    
                PrintDocument p = new PrintDocument();
                p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
                {
                    e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
    
                };
                try
                {
                    p.Print();
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception Occured While Printing", ex);
                }
            }
    • Marked as answer by superlurker Sunday, December 6, 2009 8:55 PM
    Sunday, December 6, 2009 5:58 PM
  • Works awsome... questions .... How to set side margins.... ?

    Like about half inch in??? 

    Thanks
    Sunday, December 6, 2009 8:55 PM