打印长度超过一页的富文本框文本

本文关键字:文本 一页 打印 | 更新日期: 2023-09-27 18:34:21

我有以下代码,并在这里和谷歌上查看了许多类似的问题,但所有解决方案都有相同的缺陷,当RTB中的内容超过一页,或超过一两行时,它会打印无限数量的页面。应该更改什么以仅打印正确的页数?

private void PrintButton_Click(object sender, EventArgs e)
    {
        if (MainTabSet.TabCount > 0)
        {
            RichTextBox textbox = (RichTextBox)MainTabSet.SelectedTab.Controls["TabTextBox"];
            PrintDocument docToPrint = new PrintDocument();
            docToPrint.PrintPage += new PrintPageEventHandler(PrintPageHandler);
            docToPrint.DocumentName = MainTabSet.SelectedTab.Text;
            PrintDialog.Document = docToPrint;
            if(PrintDialog.ShowDialog() == DialogResult.OK)
            {
                docToPrint.Print();
            }
        }
    }
    private void PrintPageHandler(object sender, PrintPageEventArgs e)
    {
        if (MainTabSet.TabCount > 0)
        {
            RichTextBox textbox = (RichTextBox)MainTabSet.SelectedTab.Controls["TabTextBox"];
            StringReader reader = new StringReader(textbox.Text);
            float linesPerPage = 0.0f;
            float yPosition = 0.0f;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float rightMargin = e.MarginBounds.Right;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            Font printFont = textbox.Font; //maybe do selection font
            SolidBrush printBrush = new SolidBrush(textbox.ForeColor);//Maybe do selection color
            int charPos = 0;
            int xPosition = (int)leftMargin;
            linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            while (count < linesPerPage && ((line = reader.ReadLine()) != null))
            {
                xPosition = (int)leftMargin;
                yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
                count++;
                for (int i = 0; i < line.Length; i++)
                {
                    textbox.Select(charPos, 1);
                    if ((xPosition + ((int)e.Graphics.MeasureString(textbox.SelectedText, textbox.SelectionFont).Width)) > rightMargin)
                    {
                        count++;
                        if (!(count < linesPerPage))
                        {
                            break;
                        }
                        xPosition = (int)leftMargin;
                        yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
                    }
                    printBrush = new SolidBrush(textbox.SelectionColor);
                    e.Graphics.DrawString(textbox.SelectedText, textbox.SelectionFont, printBrush, new PointF(xPosition, yPosition));
                    xPosition += ((int)e.Graphics.MeasureString(textbox.SelectedText, textbox.SelectionFont).Width);
                    charPos++;
                }
            }
            if (line != null)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                printBrush.Dispose();
            }
        }
    }

提前感谢您的帮助。诺德纳布3

打印长度超过一页的富文本框文本

在 PrintPageHandler 中添加此代码

private void PrintPageHandler(object sender, PrintPageEventArgs e)
{
    int charactersOnPage = 0;
    int linesPerPage = 0;
    // Sets the value of charactersOnPage to the number of characters 
    // of stringToPrint that will fit within the bounds of the page.
    e.Graphics.MeasureString(stringToPrint, font1,
        e.MarginBounds.Size, StringFormat.GenericTypographic,
        out charactersOnPage, out linesPerPage);
    // Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, font1, Brushes.Black,
        e.MarginBounds, StringFormat.GenericTypographic);
    // Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage);
    // Check to see if more pages are to be printed.
    e.HasMorePages = (stringToPrint.Length > 0);
}

和打印按钮代码

   private void PrintButton_Click(object sender, EventArgs e)
    {
        stringToPrint = tabsProperties[tabsProperties.IndexOf(new TabProperties(this.tabControl1.SelectedIndex))].TabHtml;
        printDialog1.ShowDialog();
        printDocument1.Print();
    }