Pregunta

excuse me, my English

well, i see in a program when it need print something the first thing is read a File that contain the line and column of the fields that will print in a matrix printer (a printer of points, i don't find the correct word)

my idea is write in a file the position of the fields i need to print like this

(x,y) fields1, (x,y) fields2, (x,y) fields3,

and this way print every fields, so my program need to read the file for print, this way allow me change the position of a fields in the format if the preprinted document that i need print change something

i wanna do this because i have many diferent preprinted invoice and i need adjust the printer way

i read about PrintDocument but i don't found something well explanation of it and i read this, but is a very simple example

i hope someone can guide me in the right direction

¿Fue útil?

Solución

I don't think that the type of printer matters (matrix vs. inkjet vs. laser). Here is a more complete code example.

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage.aspx

For your specific scenario, you will need to parse the x,y position information out of your invoice format file for each field. Once you have x and y, just draw it to the PrintPage event arguments Graphics object just like in the example code.

The tricky part will be parsing the format file for the correct x and y position data. You can make things easier for yourself by using a very simple format. For example, you could format the file as follows.

    x
    y
    [field1]
    x
    y
    [field2]
    ...

So let's say you want to print a simple page that looks like this.

    07-31-2013         Invoice             Page 1

    Item               Quantity            Price
    --------           --------            --------
    Sprocket           1                   $100.00
    Cog                2                   $ 25.00

    Total: $150.00

Your actual formatted invoice file would be...

    1
    1
    07-31-2013
    1
    20
    Invoice
    1
    40
    Page 1
    3
    1
    Item
    3
    20
    Quantity
    3
    40
    Price
    4
    1
    --------
    4
    20
    --------
    4
    40
    --------
    5
    1
    Sprocket
    5
    20
    1
    5
    40
    $100.00
    6
    1
    Cog
    6
    20
    2
    6
    40
    $ 25.00
    8
    1
    Total: $150.00

And your code to print it would be something like this.

// The PrintPage event is raised for each page to be printed. 
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
    int row = 0;
    int col = 0;
    float xPos = 0;
    float yPos = 0;
    float leftMargin = ev.MarginBounds.Left;
    float topMargin = ev.MarginBounds.Top;
    string line = null;

    // Print each line of the file. 
    while (true)
    {
        try
        {
            row = Convert.ToInt32(streamToPrint.ReadLine());
            col = Convert.ToInt32(streamToPrint.ReadLine());
            line = streamToPrint.ReadLine();
        }
        catch
        {
            break;
        }

        xPos = leftMargin + (col * ev.Graphics.MeasureString(" ", printFont, ev.PageBounds.Width));
        yPos = topMargin + (row * printFont.GetHeight(ev.Graphics));

        ev.Graphics.DrawString(line, printFont, Brushes.Black, xPos, yPos, new StringFormat());
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top