2011-10-28 30 views
0

我正在做一個帶有多個選項卡的Web瀏覽器,每個選項卡可能都會有一個與其他選項卡不同的新網站。 現在我想要做的是在特定的選項卡上打印頁面,並且當我嘗試打印時,該頁面可能包含多個頁面。 這是我的代碼,代碼的問題在於它只打印一頁,而最後一個選項卡已打開。任何建議:在特定選項卡中打印富文本框多頁C#

//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(); 
    } 
+0

如果您使用的是標準瀏覽器類型的對象應該有它自己的打印方法。 – Kell

+1

如何爲一個支持多個Web瀏覽器實例的程序提供一個RichTextBox? RTB甚至與瀏覽器有什麼關係? –

+0

我正在發送一個http請求並檢索數據爲html代碼,我沒有使用渲染或webbroswer工具,並且在每個新選項卡上都創建了富文本框 –

回答

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

    }