打印超过一页的RichTextBox文本?

时间:2014-12-15 05:55:30

标签: c# winforms printing

我有以下代码,并在这里和谷歌查看了很多类似的问题,但是当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();
            }

        }
    }

提前感谢您的帮助。 // Nodnarb3

1 个答案:

答案 0 :(得分:0)

在“打印页面处理程序”中添加此代码

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();

    }