【发布时间】:2013-05-27 14:33:43
【问题描述】:
private void printReceipt()
{
printDialog.Document = printDocument;
DialogResult result = printDialog.ShowDialog();
if (result != DialogResult.OK) return;
try
{
sPrint = new StreamReader(
new MemoryStream(
Encoding.ASCII.GetBytes(richTextBoxResult.Text)));
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
printDocument.Print();
}
catch (Exception e)
{
MessageBox.Show("Failed to print \n" + e.Message);
}
finally
{
if (sPrint != null)
sPrint.Close();
}
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// No of lines that fit on to the page
float linesPerPage = e.MarginBounds.Height / richTextBoxResult.Font.GetHeight(e.Graphics);
float fontHeight = richTextBoxResult.Font.GetHeight(e.Graphics);
for (int count = 0; count < linesPerPage && !sPrint.EndOfStream; count++)
{
e.Graphics.DrawString(sPrint.ReadLine(),
richTextBoxResult.Font,
Brushes.Black,
e.MarginBounds.Left,
e.MarginBounds.Top + (count * fontHeight),
new StringFormat());
}
e.HasMorePages = !sPrint.EndOfStream;
}
我的问题:-
- 我需要在打印时正确调整所有文本框内容。 (冗长的单行没有打印,而是跳出页面)
- 是否可以在打印菜单下启用打印范围选项? (在输出 GUI 上,假设我的文本内容可以放在多个页面上)
请帮忙。 谢谢。
【问题讨论】:
-
您是否考虑过使用报告?多做一些工作来实现,但必须满足你想要的。
-
正确打印富文本需要 hocus pocus。但在this KB article 中有详细记录
-
使用 ASCII 编码有什么特别的原因吗?丢弃 Unicode 支持似乎是一种浪费。您可以使用 StringReader 消除整个问题和 MemoryStream。
-
@HansPassant 非常感谢。非常有用的链接。