2014-09-29 24 views
1
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不解决 帮助我的核心。打印marigin不是在第二页中设置

+0

有人解决这个。我困在这里[打印多页边距 – Varta 2014-09-29 13:49:29

+0

可能重复的不设置](http://stackoverflow.com/questions/26293174/printing-multiple-pages-margin-not-set) – 2014-10-10 13:33:20

+0

@BerndLinde实际上,严格来说,这个问题是这个问题的重复。 – 2014-10-10 13:59:23

回答

0

您正在根据count设置顶部边距,但count不是页数,它是行计数。你需要保持页面数量并使用它。

+0

有人可以解决这个问题 – Varta 2014-10-11 07:07:04

0

使用一个字段来保存,如果它的第一页,记得打电话printDocument1_PrintPage如之前将其设置为true

bool Isfirstpage = true; 

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

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

///.... 
+0

,Not解决了,如果我可以改变以上给定它将开始所有页面的顶部边际100 – Varta 2014-10-11 06:00:25

+0

如果我输入单行文本,那么它将开始590 – Varta 2014-10-11 06:01:01

+0

请coluld任何人的帮助? – Varta 2014-10-21 05:49:17