首页 > 解决方案 > PrintPreviewDialog 打印空白页

问题描述

我看到了很多关于此的报告,我尝试了一些建议的方法,但在我的情况下都没有。我认为在我的情况下,我不明白 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 打印机。如果我单击运行并打印了一个页面,但它是空白的。

所以两个问题

  1. 为什么我在预览对话框中看不到所有 7 台打印机?
  2. 你如何让对话框打印实际页面而不是忽略它打印空白?

有关如何使其打印正确页面的任何想法。谢谢。

标签: c#winformsprintingprint-preview

解决方案


经过多次挠头后,我发现它可以工作,只需将文本放回richtextbox。我认为一旦它进行了预览,它就会打印预览。显然不是预览只给它打印的数字而不是打印的图像。它从原始来源获取打印图像。所以现在最后当 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, 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.
            // 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
             }
        }  

推荐阅读