我不認爲打印機的類型很重要(矩陣與噴墨與激光)。這是一個更完整的代碼示例。
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage.aspx
針對您的特殊情況下,你需要解析的x,y位置信息對每個字段中輸入您的發票格式文件。一旦你有了x和y,就像在示例代碼中一樣將它繪製到PrintPage事件參數Graphics
對象中。
棘手的部分將解析正確的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());
}
}
是這樣的事情是我需要的,現在我要實現我的想法,謝謝,你知道我有多打印線蒙山一個diferent大小和字母 –
的類型在上面的例子中,'printFont '確定打印文本的字體系列和大小。你可以按如下設置:printFont = new Font(「Arial」,10);'。查看此鏈接瞭解更多信息 - http://msdn.microsoft.com/en-us/library/system.drawing.font.aspx。 –
謝謝,我會研究這個 –