我做一个项目的打印模块时使用了PrintDocument,代码如下:
PrintDocument doc;
for(int i=0;i<doc.PrinterSettings.PaperSizes.Count;i++)
{
  if(doc.PrinterSettings.PaperSizes[i].PaperName.ToLower() == "fs")
  {
    doc.DefaultPageSettings.PaperSize = doc.PrinterSettings.PaperSizes[i];
    doc.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0,0,0,0);
    doc.OriginAtMargins = true;
  }
}
doc.Print();private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
  e.Graphics.DrawString("test",f,Brushes.Black,170,350);
}其中,在打印机上定义了纸张“fs”大小为20.85cm*10.20cm,也就是8.21in*4.02in,但是每次打印时放入相应大小的纸张进去就是打不出来,放一张大一些的纸进去就能打出来,然后两张纸重合发现,打印的字的位置是在小纸上的,但是不知道为什么就是打不出来,有高人指教一下吗?谢谢各位了!万分感谢

解决方案 »

  1.   

    试试用这个方法。
    using System.Drawing.Printing;
    using System.IO;
    using System.Drawing; private Font printFont;
    StreamReader streamToPrint;
    PrintDocument prtdoc = new PrintDocument();                                    private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
    {
    float linesPerPage = 0;
    float yPos = 0;
    int count = 0;
    float leftMargin = ev.MarginBounds.Left;
    float topMargin = ev.MarginBounds.Top;
    string line = null; // Calculate the number of lines per page.
    linesPerPage = ev.MarginBounds.Height / 
    printFont.GetHeight(ev.Graphics); // Print each line of the file.
    while(count < linesPerPage && 
    ((line=streamToPrint.ReadLine()) != null)) 
    {
    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
    ev.Graphics.DrawString(line, printFont, Brushes.Black, 
    leftMargin, yPos, new StringFormat());
    count++;
    } // If more lines exist, print another page.
    if(line != null)
    ev.HasMorePages = true;
    else
    ev.HasMorePages = false;
    }// box是我设定的选择打印机的名称
    public void Printing(ComboBox box)
    {
    try 
    {
    streamToPrint = new StreamReader(@"E:\abc.txt");
    //Print setting
    printFont = new Font("Arial", 18);
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
    pd.PrinterSettings.PrinterName = box.SelectedItem.ToString();
    pd.Print();
    streamToPrint.Close();
    }  
    catch(Exception ex) 
    {
    MessageBox.Show(ex.Message);
    }
    }
      

  2.   

    谢谢你的回复,这种方法我试过了。
    linesPerPage = ev.MarginBounds.Height/printFont.GetHeight(ev.Graphics);
    计算出来的是能够打印的行数,我的程序计算出来是27行,但是在纸张上只打印了22行就不打印了,再放纸进去,才把后面5行打印出来。