如何在c#中打印文本到页面的左下角
本文关键字:左下角 文本 打印 | 更新日期: 2023-09-27 18:12:58
下面的代码用于打印订单。订单可以有可变的行数写入第一个DrawString调用。
文本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);
}
测量你的字符串,然后从底部向上移动那个高度?
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);
}