1 sr = new StringReader(str);
2 PrintDocument pd = new PrintDocument();
3 pd.PrintController = new System.Drawing.Printing.StandardPrintController();
4 PaperSize pageSize = new PaperSize("Custom", getYc(58), 600);//一定要Custom,写别的改变不了尺寸
5 pd.DefaultPageSettings.Margins.Top = 2;
6 pd.DefaultPageSettings.Margins.Left = 0;
7 pd.DefaultPageSettings.PaperSize = pageSize;
8 pd.PrinterSettings.PrinterName = pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机
9 pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
10 pd.Print();
11
12 ……………………………………
13
14 //厘米转换英寸
15 private static int getYc(double cm)
16 {
17
18 return (int)(cm / 25.4) * 100;
19
20 }
21
22
23
24
25 private static void pd_PrintPage(object sender, PrintPageEventArgs ev)
26 {
27 Font printFont = new Font("Arial", 9);//打印字体
28 float linesPerPage = 0;
29 float yPos = 0;
30 int count = 0;
31 float leftMargin = ev.MarginBounds.Left;
32 float topMargin = ev.MarginBounds.Top;
33 String line = "";
34 linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
35 while (count < linesPerPage && ((line = sr.ReadLine()) != null))
36 {
37 yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
38 ev.Graphics.DrawString(line, printFont, Brushes.Black,
39 leftMargin, yPos, new StringFormat());
40 count++;
41 }
42 // If more lines exist, print another page.
43 if (line != null)
44 ev.HasMorePages = true;
45 else
46 ev.HasMorePages = false;
47 }