【发布时间】:2021-04-22 07:33:30
【问题描述】:
我看到了很多关于此的报告,并且我尝试了一些建议的方法,但在我的情况下都没有。我认为就我而言,我不明白 PrintPreviewDialog 是如何工作的,所以我没有将文档与打印按钮正确连接。
我在许多 A4 打印页长的 Richtextbox 中创建了一个文档。然后我从 Winform 中的一个按钮使用以下内容。第二个功能是制作页面的事件。 (以下部分代码来自其他人,感谢他们)
private void btn_SaveBitmap_Click(object sender, EventArgs e)
{
PrintDocument printDocument1 = new PrintDocument();
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Icon = new Icon("..\\..\\braille.ico");
printDocument1.PrintPage += PrintDocument_PrintPage;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
//comes in here for every page it needs to make
int charactersOnPage = 0;
int linesPerPage = 0;
Font drawFont = new Font(rchtxtbx_braille.Font.ToString(), rchtxtbx_braille.Font.Size);
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(rchtxtbx_braille.Text, drawFont,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
e.Graphics.DrawString(rchtxtbx_braille.Text, drawFont, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed.
rchtxtbx_braille.Text = rchtxtbx_braille.Text.Substring(charactersOnPage);
// Check to see if more pages are to be printed.
e.HasMorePages = (rchtxtbx_braille.Text.Length > 0);
}
这一切在预览对话框中看起来都不错,我可以看到我有很多页面并且它们看起来都正确。在左上角的预览中,我按下打印图标,然后我看到虽然笔记本电脑可以访问 7 台打印机,但我在这里得到的只是 PDF 打印机。如果我点击运行并打印了一个页面,但它是空白的。
那么两个问题
- 为什么我在预览对话框中看不到所有 7 台打印机?
- 如何让对话框打印实际页面,而不是将其忽略为打印空白?
请提供有关如何使其打印正确页面的任何想法。谢谢。
【问题讨论】:
-
如果我点击运行并打印了一个页面但它是空白的......因为
rchtxtbx_braille.Text = rchtxtbx_braille.Text.Substring(charactersOnPage);。您正在清除RTB.Text,当您从预览对话框打印时,没有任何内容可打印。使用StringBuilder在按钮单击事件中附加RTB.Text,并在您在代码中看到rchtxtbx_braille.Text的PrintPage事件中使用它。此外,printDocument1和printPreviewDialog1是一次性对象。你应该在之后处理它们。在printPreviewDialog1.ShowDialog();之后。
标签: c# winforms printing print-preview