在特定坐标上打印

时间:2014-05-13 22:24:11

标签: c# asp.net .net webforms

我有一张高10厘米,宽15厘米的卡片。我试图在以下坐标上打印一个字符串:从上到下1.5厘米,从左到右3,5厘米。直到现在我试图使用我在msdn上找到的波纹管代码,但没有运气。我正在使用的代码是:

System.IO.StreamReader fileToPrint;
System.Drawing.Font printFont;
private void printButton_Click(object sender, EventArgs e)
 {
   string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
   fileToPrint = new System.IO.StreamReader(printPath + @"\myFile.txt");
    printFont = new System.Drawing.Font("Arial", 10);
    printDocument1.Print();
     fileToPrint.Close();
  }

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
  float yPos = 0f;
  int count = 0;
  float leftMargin = e.MarginBounds.Left;
  float topMargin = e.MarginBounds.Top;
  string line = null;
  float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
 while (count < linesPerPage)
   {
      line = fileToPrint.ReadLine();
      if (line == null)
    {
       break;
    }
      yPos = topMargin + count * printFont.GetHeight(e.Graphics);
      e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new      StringFormat());
       count++;
    }
     if (line != null)
   {
       e.HasMorePages = true;
   }
 }

0 个答案:

没有答案