Print the string in the default printer
We can print the hello string into the default printer
Following is the code to print the hello string into the default printer
Namespace Part
using System;
using System.Drawing.Printing;
using System.Drawing;
Code Part
namespace ConsoleApplication1
{
public class Program
{
[STAThread]
static void Main(string[] args)
{
Program MyProgram = new Program();
MyProgram.RunSample();
}
public void RunSample()
{
Console.WriteLine("Printing the Hello sting into the default printer...");
try
{
PrintDocument MyPrintDocument = new PrintDocument();
MyPrintDocument.PrintPage += new PrintPageEventHandler(this.PrintPageEvent);
MyPrintDocument.Print();
}
catch (Exception ex)
{
Console.WriteLine("Error while printing ... " + ex.ToString());
}
Console.ReadLine();
}
private void PrintPageEvent(object sender, PrintPageEventArgs ev)
{
string MyHelloString = "Hello World!";
Font MyFont = new Font("Arial", 10);
Rectangle marginRect = ev.MarginBounds;
ev.Graphics.DrawRectangle(new Pen(System.Drawing.Color.Black), marginRect);
ev.Graphics.DrawString(MyHelloString, MyFont, new SolidBrush(System.Drawing.Color.Blue),
(ev.PageBounds.Right / 2), ev.PageBounds.Bottom / 2);
}
}
}
Code Explanation
1. Create the instance of PrintDocument
2. Add the event for print page event
3. Event will be fired for each page to print
3. Print the string in the event
By
Nathan
