Oct 17, 2008

spot printing in c#

using System.Drawing.Printing;
using System.Drawing;
using System.IO;

namespace WindowsApplication1
{

public partial class Form3 : Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
private Font printFont;
private StreamReader streamToPrint;
PrintDocument printDocument1 = new PrintDocument();


public Form3()
{
InitializeComponent();

printDocument1.PrintPage += printDocument1_PrintPage;
}

private void printButton_Click(object sender, EventArgs e)
{
using (PrintDialog pd = new PrintDialog())
{
if (pd.ShowDialog() == DialogResult.OK)
printDocument1.PrinterSettings = pd.PrinterSettings;
printDocument1.Print();
}


void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font objFont=new Font ("Microsoft Sans Serif", 10F);//sets the font type and size
float fTopMargin = e.MarginBounds.Top;
float fLeftMargin = 50;//sets left margin
float fRightMargin = e.MarginBounds.Right - 150;//sets right margin
string employee_id = String.Concat("EMPLOYEE ID:- ", comboBox2.Text);//prints EMPLOYEE ID:- and text in combobox2
string employee_name = String.Concat("EMPLOYEE NAME:- ", textBox8.Text);

e.Graphics.DrawString(employee_id, objFont, Brushes.Black, fLeftMargin, fTopMargin);

fTopMargin += objFont.GetHeight() * 2;//skip two lines

e.Graphics.DrawString(employee_name, objFont, Brushes.Black, fLeftMargin, fTopMargin);

objFont.Dispose();

e.HasMorePages = false;
}