Skip to content

Connection to a printer...

Muzakkir

Asked Apr 7, 2008, 1:40 AM EDT2 answers
Share
LinkedInX
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//...

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    First add a
    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
    0
  • 0
    Heyy I think Iam getting to that response..will try to implement asap...thanx for this quick reply cuz i ddn't have an idea of it...Cud u plz b more clear?
    0