4

Possible Duplicate:
Send document to printer with C#

I want to to Send a String Value directly to printer. of course it is very better that I can send a datatable to printer. but first of all I want to know how I can send my String Value without any prompting for end user to printer. I have searched for 3 hours in internet but found no response. please help me. Thx :)

Community
  • 1
  • 1
Shahrokh
  • 79
  • 1
  • 2
  • 6
  • 6
    Ironically this is the first hit on Google when you search for this. –  Nov 07 '13 at 15:16

2 Answers2

18

you can use PrintDocument under System.Drawing.Printing namespace. Print method will print the string using your default printer

string s = "string to print";

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

Found example from here

Damith
  • 62,401
  • 13
  • 102
  • 153
2

Not sure what you were searching for, but here are two articles I found in 1 minute on MSDN about printing. In a nutshell, a PrintDocument class wraps up the functionality with the PrintPage event being raised for each page being printed.

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.printdialog.aspx

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
  • 2
    I believe OP want to send text directly to text printer. like what we used to do pre-windows on com port printers. a text drawn on graphic is not actually a string sent to printer, it is a picture. – AaA Mar 09 '18 at 10:49