печатать текстовое поле в определенной вкладке несколько страниц C # - PullRequest
0 голосов
/ 28 октября 2011

Я использую веб-браузер с несколькими вкладками, и каждая вкладка, вероятно, будет иметь новый веб-сайт, который отличается от других вкладок. Теперь я пытаюсь напечатать страницу на определенной вкладке, и страница может состоять из нескольких страниц, когда я пытаюсь распечатать. это мой код, и проблема с кодом заключается в том, что он печатает только одну страницу и на последней открытой вкладке. любые предложения:

  //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 голосов
/ 30 октября 2011

Вот как я это сделал: в документе «Печать» содержится следующий код:

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

    }
...