问题描述
我看到了很多关于这方面的报告,我尝试了一些建议的方法,但在我的情况下没有任何效果。我想就我而言,我不明白 PrintPreviewDialog 是如何工作的,所以我没有正确地将文档与打印按钮连接起来。
我在一个多页 A4 打印页面的富文本框中创建了一个文档。然后我从 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,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 台打印机?
- 如何让对话框打印实际页面而不是忽略它打印空白?
有关如何使其打印正确页面的任何想法。谢谢。
解决方法
经过多次摸索后,我意识到可以工作,只需将文本放回富文本框即可。我认为一旦它进行了预览,它就会打印预览。显然不是预览只给它打印的数字而不是要打印的图像。它从原始来源获取打印图像。所以现在最后当 Richtextbox 为空时,我说没有更多页面,然后替换文本。它现在可以工作了。
private StringBuilder sb = new StringBuilder(); //needed to replace text into richtextbox after preview
private void btn_SaveBitmap_Click(object sender,EventArgs e)
{
PrintDocument printDocument1 = new PrintDocument();
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Icon = new Icon("..\\..\\braille.ico");
sb.Append(rchtxtbx_braille.Text);
printDocument1.PrintPage += PrintDocument_PrintPage;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
printDocument1.Dispose();
printPreviewDialog1.Dispose();
}
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,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.
// replace the text when the window is empty so we have something to print
// otherwise we print a blank document.
if (rchtxtbx_braille.Text.Length > 0)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false; //say no more pages
rchtxtbx_braille.Text = sb.ToString(); //replace text
}
}