Connection to a printer...
Hi all,i have made a windows applicatuon which should print all the data whenevr i need to..how to implement it and support it?plz help//...
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
MuzakkirPosted Apr 10, 2008, 6:22 AM
Bechir BejaouiPosted Apr 7, 2008, 7:20 AM
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
to the head of your project Then define the stream to be printed
Open the tool box and add under the Printing tab select a printDocument then add it into your project, it iwill be named printDocument1 by default.
then define new stram reader
StreamReader streamToBePrinted;
then Add a button to your project and implement his event handler as follow
private void button1_Click(object sender, EventArgs e)
{
/* You can overload the constructor here by two way
if you have the stream witihn you application then
overload it with stream, the stream to be printed
is some where in your hard drive or in the local
* network then over load with the the file path
*/
string FilePath = @"C:\.....";
streamToBePrinted = new StreamReader(FilePath);
printDocument1.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.Print();
}
then add a new event handler to the PrintPage event of the printDocument s follow
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int MaxLinesPerPage = 10; //You can modify it to fit your needs
int i=0;
while (i < MaxLinesPerPage && streamToBePrinted.ReadLine() != null)
{
e.Graphics.DrawString(streamToBePrinted.ReadLine(), new Font(new FontFamily("Time New Roman"), FontStyle.Bold), null, new Point(e.MarginBounds.Top, e.MarginBounds.Left));
i++;
}
if (streamToBePrinted.ReadLine() != null)
{ e.HasMorePages = true; }
if (streamToBePrinted.ReadLine() == null)
{
e.HasMorePages = false;
streamToBePrinted.Close();
}
}
try this as a first step