在第二页中未设置打印边距

时间:2014-09-29 06:05:59

标签: c# winforms

private StringReader myReader;

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 line = null;
Font printFont = new System.Drawing.Font("Times New Roman", 8, FontStyle.Regular);
SolidBrush myBrush = new SolidBrush(Color.Black);
float linesPerPage = 0;
float topMargin = 590;
float yPosition = 590;
int count = 0;
float leftMargin = 70;

linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
{
if (count == 0)
{
yPosition = 590;
topMargin = 590;
}
else
{
yPosition = 100;
topMargin = 100;
}
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();
}
}
}
}
请问我的错误在哪里。我要打印第一页是顶级marigin是590,如果更多页面第二页应该是打印顶部marigin是100。 上面给出的代码是打印是好的,但打印marigin没有解决 帮我核心。

2 个答案:

答案 0 :(得分:0)

您正在根据count设置上边距,但count不是页数,而是行数。你需要保持页数并使用它。

答案 1 :(得分:0)

如果某个字段是第一页,请使用该字段,请在调用true之前将其设置为printDocument1_PrintPage,例如:

bool Isfirstpage = true;

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

   if (count == 0 && Isfirstpage)
   {
     yPosition = 590;
     topMargin = 590;
     Isfirstpage = false;
   }

///....