tags:

views:

40

answers:

1

hi

how to print "Hello World" to Default printer ?

without crystalReport

in Winform C# program

thank's in advance

+2  A: 

I have a litle class that I made some time ago to print directly to the default printer. Hope It Helps. Keep in mind that it will crash if there is not default printer. It was not for a proffesional project, and it has some years so i'm sure this could be much better.

public class PrintTextDocument
{
    private PrintDocument pPd;
    private string[] pLineas;   //Lines
    private Font pFont;

    public PrintTextDocument(string[] lineas, Font fuente)
    {
        pLineas = lineas;
        pFont = fuente;

        pPd = new PrintDocument();
        pPd.DefaultPageSettings.PrinterResolution.Kind = PrinterResolutionKind.High;
        pPd.PrintPage += new PrintPageEventHandler(pPd_PrintPage);
    }

    void pPd_PrintPage(object sender, PrintPageEventArgs e)
    {
        float yPos;
        int count = 0;
        float leftMargin = 0;// e.MarginBounds.Left;
        float topMargin = 0;// e.MarginBounds.Top;
        string line;

        e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        for (int i = 0; i < pLineas.Length; i++)
        {
            line = pLineas[i];

            yPos = topMargin + count * pFont.GetHeight(e.Graphics);
            e.Graphics.DrawString(line, pFont, Brushes.Black, leftMargin, yPos, new StringFormat());
            count++;
        }
    }

    public void Print()
    {
        pPd.Print();

    }
Jonathan