【问题标题】:PrintPreviewDialog prints blank pagesPrintPreviewDialog 打印空白页
【发布时间】: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 打印机。如果我点击运行并打印了一个页面,但它是空白的。

那么两个问题

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

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

【问题讨论】:

  • 如果我点击运行并打印了一个页面但它是空白的......因为rchtxtbx_braille.Text = rchtxtbx_braille.Text.Substring(charactersOnPage);。您正在清除RTB.Text,当您从预览对话框打印时,没有任何内容可打印。使用StringBuilder 在按钮单击事件中附加RTB.Text,并在您在代码中看到rchtxtbx_braille.TextPrintPage 事件中使用它。此外,printDocument1printPreviewDialog1 是一次性对象。你应该在之后处理它们。在printPreviewDialog1.ShowDialog(); 之后。

标签: c# winforms printing print-preview


【解决方案1】:

经过多次挠头后,我发现它可以工作,只需将文本放回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
             }
        }  

【讨论】:

    猜你喜欢
    • 2015-05-18
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多