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());
}
}