プリンターの種類 (マトリックス、インクジェット、レーザー) は関係ないと思います。より完全なコード例を次に示します。
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage.aspx
特定のシナリオでは、各フィールドの請求書フォーマット ファイルから x、y 位置情報を解析する必要があります。x と y を取得したらGraphics
、コード例のように PrintPage イベント引数オブジェクトに描画します。
トリッキーな部分は、正しい x および y 位置データのフォーマット ファイルを解析することです。非常に単純なフォーマットを使用することで、物事を簡単に行うことができます。たとえば、次のようにファイルをフォーマットできます。
x
y
[field1]
x
y
[field2]
...
たとえば、次のような単純なページを印刷するとします。
07-31-2013 Invoice Page 1
Item Quantity Price
-------- -------- --------
Sprocket 1 $100.00
Cog 2 $ 25.00
Total: $150.00
実際の書式設定された請求書ファイルは...
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
そして、それを印刷するコードは次のようになります。
// 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());
}
}