在特定选项卡中打印富文本框多个页面C#

时间:2011-10-28 16:10:12

标签: c# winforms printdocument printdialog

我正在使用多个标签进行网络浏览,每个标签可能会有一个新网站与其他标签不同。 现在我想要做的是在特定选项卡上打印页面,当我尝试打印时,页面可能包含多个页面。 这是我的代码和代码的问题是它只打印一页并在最后一个选项卡上打开。任何建议:

  //this is the printDocemnt from the toolbox
  private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Font font1 = new Font("Arial", 16, FontStyle.Regular);
        e.Graphics.DrawString(rtb.Text, font1, Brushes.Black, 100, 100);//rtb.Text is a richtextbox object that i initialize in the beginning of the form

    }

    //this is the printbutton just a normal button
    private void PrintButton_Click(object sender, EventArgs e)
    {
        printDialog1.ShowDialog();
        printDocument1.Print();
    }

1 个答案:

答案 0 :(得分:0)

我就是这样做的:打印文档包含以下代码:

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Font font1 = new Font("Arial", 10, FontStyle.Regular);
        //e.Graphics.DrawString(tabsProperties[tabsProperties.IndexOf(new TabProperties(this.tabControl1.SelectedIndex))].TabHtml, font1, Brushes.Black, 100, 100);



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

    }