2013-05-09 26 views
0

以下代码用于打印订单。 订单可以通过第一个DrawString调用写出可变数量的行。如何在C#页面的左下角打印文本

文本

Bottom line1 
Bottom line2 

必须出现在左下角的页面。

以下代码使用硬编码值e.MarginBounds.Top + 460来打印此。 如何删除此硬编码值以便文本打印在页面底部?

var doc = new PrintDocument(); 
    doc.PrinterSettings.PrinterName = "PDFCreator"; 
    doc.PrintPage += new PrintPageEventHandler(ProvideContent); 
    doc.Print(); 

    void ProvideContent(object sender, PrintPageEventArgs e) 
    { 
     e.Graphics.DrawString("string containing variable number of lines", new Font("Arial", 12), 
      Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top); 

     e.Graphics.DrawString("Bottom line1\r\nBottom line2", new Font("Courier", 10), Brushes.Black, 
        e.MarginBounds.Left, e.MarginBounds.Top + 460); 
    } 

回答

2

测量你的字符串,然后从底部向上移动那个高度?

void ProvideContent(object sender, PrintPageEventArgs e) 
    { 
     e.Graphics.DrawString("string containing variable number of lines", new Font("Arial", 12), 
      Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top); 


     string bottom = "Bottom line1\r\nBottom line2"; 
     Font courier = new Font("Courier", 10); 
     Size sz = TextRenderer.MeasureText(bottom, courier); 
     e.Graphics.DrawString(bottom, courier, Brushes.Black, 
        e.MarginBounds.Left, e.MarginBounds.Bottom - sz.Height); 
    } 
相关问题