Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否打印长度超过一页的RichTextBox文本?_C#_Winforms_Printing - Fatal编程技术网

C# 是否打印长度超过一页的RichTextBox文本?

C# 是否打印长度超过一页的RichTextBox文本?,c#,winforms,printing,C#,Winforms,Printing,我有下面的代码,并且在这里和google上查看了很多类似的问题,但是所有的解决方案都有相同的缺陷,当RTB中的内容超过一页,或者超过一两行时,它会打印无限多的页面。应更改哪些内容以仅打印正确的页数 private void PrintButton_Click(object sender, EventArgs e) { if (MainTabSet.TabCount > 0) { RichTextBox textbox = (R

我有下面的代码,并且在这里和google上查看了很多类似的问题,但是所有的解决方案都有相同的缺陷,当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

在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);
}
还有PrintButton代码

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

        printDialog1.ShowDialog();
        printDocument1.Print();

    }

您需要一个处理程序外部的变量来跟踪您在richtextbox中的行位置,每次打印新页面时,您都会重新触发处理程序,导致所有变量都被重置,这会给您带来无尽的循环。代码中还有很多其他问题需要解决。仅供参考下次您发布代码snippit时,请确保它将按原样运行,而无需尝试重新创建整个应用程序。以下是包含格式的示例!