2014-10-10 121 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