2016-02-19 24 views
0

嘗試使用PrintDialog,PrintDocumentPrintPageEventHandler類嘗試使用C#打印文檔時,出現特殊情況。爲什麼我的代碼在使用C#在橫向模式下打印時會後臺無限頁面?

每當我以橫向模式打印一串文本時,我的應用程序開始後臺打印無限數量的頁面,直到停止調試應用程序爲止。

以下是與點擊我的「打印」按鈕相對應的代碼。

private void button_print_Click(object sender, EventArgs e) 
{ 
    PrintDialog myPrintDialog = new PrintDialog(); 
    PrintDocument myPrintDocument = new PrintDocument(); 

    // assign the print document 
    myPrintDialog.Document = myPrintDocument; 

    // prompt the user with the dialog 
    if (myPrintDialog.ShowDialog() == DialogResult.OK) 
    { 
     StringReader myStringReader = new StringReader(this.rtb_results.Text); 
     myPrintDocument.PrintPage += new PrintPageEventHandler(myPrintDocument_PrintPage); 
     myPrintDocument.Print(); 
    } 
} 

然後,這裏是對應於「打印頁面」事件的代碼...

void myPrintDocument_PrintPage(object sender, PrintPageEventArgs e) 
{ 
    StringReader myStringReader = null; 
    SolidBrush mySolidBrush = null; 
    String line = String.Empty; 
    Font myFont = null; 
    float linesPerPage = 0; 
    float leftMargin = 0; 
    float topMargin = 0; 
    float yPosition = 0; 
    int count = 0; 

    // assign the string-reader 
    myStringReader = new StringReader(this.rtb_results.Text); 

    // assign the brush type 
    mySolidBrush = new SolidBrush(Color.Black); 

    // assign the font 
    myFont = new Font("Consolas", 8.0F, FontStyle.Regular); 

    // assign the left margin 
    leftMargin = e.MarginBounds.Left; 

    // assign the top margin 
    topMargin = e.MarginBounds.Top; 

    // assign the number of lines per page 
    linesPerPage = e.MarginBounds.Height/myFont.GetHeight(e.Graphics); 

    while (count < linesPerPage && ((line = myStringReader.ReadLine()) != null)) 
    { 
     yPosition = topMargin + (count * myFont.GetHeight(e.Graphics)); 
     e.Graphics.DrawString(line, myFont, mySolidBrush, leftMargin, yPosition, new StringFormat()); 
     count++; 
    } 

    if (line != null) 
    { 
     e.HasMorePages = true; 
    } 
    else 
    { 
     e.HasMorePages = false; 
    } 

    // not sure why this would be here... 
    mySolidBrush.Dispose(); 
} 

我在爲什麼這種奇怪的行爲可能發生的猜測,將有不似乎是任何限制右邊緣的東西(但這只是我的猜測)。我試圖用RectangleF這個對象來測試這個理論,但我得到了同樣的結果......請讓我知道我做錯了什麼,如果有任何問題讓我知道。謝謝!

+1

嗯,你必須返回HasMorePages的爲真,所以每次你myPrintDocument_PrintPage叫你重新啓動的所有進程,並開始在一開始的時候,你必須存儲位置當再次調用函數時,StreamReader再次尋求它 – Gusman

+0

@Gusman爲什麼這對於縱向工作呢? – Snoopy

+0

因爲它適合於一個頁面,並且您沒有將HasMorePages設置爲true,所以我的猜測是 – Gusman

回答

1

問題是您每次都重新創建streamreader,因此如果文檔有多個頁面,則重新啓動該進程。你需要做的是每次使用相同的流媒體閱讀器。

下面是一個例子:

public Form1 : Form 
{ 
    StringReader srPrint; 

    public void PrintButton_Click(object sender, EventArgs e) 
    { 
     srPrint = new StringReader(this.rtb_results.Text); 
     //DO all your normal stuff to start the print 
    } 

    void myPrintDocument_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     StringReader myStringReader = null; 
     SolidBrush mySolidBrush = null; 
     String line = String.Empty; 
     Font myFont = null; 
     float linesPerPage = 0; 
     float leftMargin = 0; 
     float topMargin = 0; 
     float yPosition = 0; 
     int count = 0; 

     // assign the brush type 
     mySolidBrush = new SolidBrush(Color.Black); 

     // assign the font 
     myFont = new Font("Consolas", 8.0F, FontStyle.Regular); 

     // assign the left margin 
     leftMargin = e.MarginBounds.Left; 

     // assign the top margin 
     topMargin = e.MarginBounds.Top; 

     // assign the number of lines per page 
     linesPerPage = e.MarginBounds.Height/myFont.GetHeight(e.Graphics); 

     while (count < linesPerPage && ((line = srPrint.ReadLine()) != null)) 
     { 
      yPosition = topMargin + (count * myFont.GetHeight(e.Graphics)); 
      e.Graphics.DrawString(line, myFont, mySolidBrush, leftMargin, yPosition, new StringFormat()); 
      count++; 
     } 

     if (line != null) 
     { 
      e.HasMorePages = true; 
     } 
     else 
     { 
      e.HasMorePages = false; 
      srPrint.Close(); 
      srPrint.Dispose(); 
      srPrint = null; 
     } 

     mySolidBrush.Dispose(); 
    } 
} 
+0

我想我的問題來自於對StreamReader的理解不夠...所以,您將StringReader分配給全局StreamReader,並且這會產生所有差異?我看到我做的基本上是一樣的方式...除了使StreamReader全局... – Snoopy

+0

我的版本中的StreamReader是不是被認爲是「非 - 本地」的調用/使用它的while循環? – Snoopy

+0

不是,這裏有很大的區別,每次你輸入你的函數時,你都會創建一個新的流,因此它從0開始,但是如果你在全局創建它,那麼這個位置在調用之間被保留,因爲對象是相同的 – Gusman

相關問題