How do you print a string to the printer in c#?
I need to print a string from a textbox. The string should print through the default printer weather it is a parallel printer or a usb printer. Can开发者_运维技巧 this be done.
BTW..I am using .net Forms
Sure, you can use the PrintDocument
class to print arbitrary content.
The MSDN page on PrintDocument contains a nice example which prints the contents of a file to the default printer. Adapting this to print the content of a text box instead should be trivial.
Refer to the PrintDocument class on MSDN and the System.Drawing.Printing namespace in general.
From MSDN:
The System.Drawing.Printing namespace provides print-related services for Windows Forms applications.
Typically, when you print from a Windows Forms application, you create a new instance of the PrintDocument class, set properties, such as DefaultPageSettings and PrinterSettings, that describe how to print, and call the Print method to actually print the document. Calling the PrintDocument.Print method raises the PrintDocument.PrintPage event, which should be handled to perform the document layout for printing.
Use the Graphics property of the PrintPageEventArgs object obtained from the PrintDocument.PrintPage event to specify the output to print. If you are printing a text file, use StreamReader to read one line at a time from the stream and call the DrawString method to draw the line in the graphics object. For more information about this process, see the Graphics and StreamReader classes. You can view an example of printing a text document in the PrintDocument class overview topic.
精彩评论