2014-10-10 115 views
-1

有人可以幫助解決這個問題嗎?打印多頁邊距未設置

enter image description here

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 


namespace notepad_demo 
{ 
    public partial class Form1 : Form 
    { 
     private StringReader myReader; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void printToolStripMenuItem_Click(object sender, EventArgs e) 
     { 

      printDialog1.Document = printDocument1; 
      string strText = this.richTextBox1.Text; 
      myReader = new StringReader(strText); 
      if (printDialog1.ShowDialog() == DialogResult.OK) 
      { 

       printDocument1.Print(); 
      } 
     } 

     private void printPrieviewToolStripMenuItem_Click(object sender, EventArgs e) 
     { 

      string strText = this.richTextBox1.Text;//read text for richtextbox 
      myReader = new StringReader(strText); 
      printPreviewDialog1.Document = printDocument1; 
      printPreviewDialog1.ShowDialog(); 


     } 

     private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
     { 

      string strDisplay = "Header"; 
      System.Drawing.Font fntString = new Font("Times New Roman", 28, FontStyle.Bold); 
      e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 100, 100); 
      string strDisplay1 = "Company name"; 
      System.Drawing.Font fntString1 = new Font("Times New Roman", 28, FontStyle.Bold); 
      e.Graphics.DrawString(strDisplay1, fntString1, Brushes.Black, 100, 150); 

      float linesPerPage = 0; 
      float yPosition = 590; 
      int count = 0; 
      float leftMargin = 70; 
      float topMargin = 590; 
      string line = null; 
      Font printFont = new System.Drawing.Font("Times New Roman", 8, FontStyle.Regular); 
      SolidBrush myBrush = new SolidBrush(Color.Black);    
      linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics); 
      while (count < linesPerPage && ((line = myReader.ReadLine()) != null)) 
      { 
       yPosition = topMargin + (count * printFont.GetHeight(e.Graphics)); 
       e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat()); 
       count++; 
      } 

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

      } 
      else 
      { 
       e.HasMorePages = false; 

      } 
      myBrush.Dispose(); 
     } 
    }  
} 

在所附的圖像,第一頁是不錯,但第二,第三和第四頁也開始同樣按照第一頁。 我只想在第一頁上顯示頁眉和公司名稱,而在第二頁上則在頂部頁邊上顯示「RichTextBox.text」。

我的錯誤在哪裏?

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2014-10-10 06:39:21

+0

@Varta,在您的問題中詢問有關同一主題[昨天](http://stackoverflow.com/questions/26279404):您使用什麼報告模塊?你是自己畫所有的頁面嗎? – 2014-10-10 07:01:12

+0

@林德林德,我想像ref圖像。 – Varta 2014-10-10 07:21:48

回答

0

要解決您在每個頁面上打印標題的問題,請按照您在here問題的評論部分提供的Hans Passant給出的建議。

簡而言之,它會創建一個名爲pageCount的類變量,在打印每個頁面時開始打印並將其遞增時將其設置爲0。
然後,將您的頁眉部分寫入頁面的部分應封裝在條件語句中,當您在第一頁以外的任何內容上進行打印時,條件語句將不會執行。

要解決第二個問題,即使在第二頁上,您的文本仍然打印在頁面的很遠處,這是因爲您正在初始化變量始終爲事件內的590。

你應該做的是將其初始化爲頁面頂部(打印標題的位置),然後根據當前正在打印的頁面進行調整。例如:
例如。在打印不同的標題和標籤時,首頁將打印來自上邊距的標題並增加topMargin變量。然後,當您到達第二頁時,只需將您的topMargin變量與您打印的每個標籤/行的高度相加(就像您當前在循環中執行的操作)。

你已經對變量yPosition有了這個想法,你只需要爲你正在打印的所有東西完全實現它,而不僅僅是標籤。

+0

,感謝您的及時suggesstion.but iam無法解決我自己的這個問題,所以請幫助代碼,所以我可以很容易地理解。謝謝 – Varta 2014-10-10 13:56:40

+0

我很抱歉,但我不會爲您編寫代碼。自從我發佈我的答案後不到10分鐘,沒有足夠的時間來正確實施並嘗試解決我自己和他人提出的問題。 – 2014-10-10 13:59:29