I'm trying to print 3 pages in the same document, but it is only printing my last one.
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" || textBox2.Text != "" || richTextBox1.Text != "" || Imagemfoot_1.Image != null || Imagemfoot_2.Image != null || Imagemfoot_3.Image != null || Imagemhead_1.Image != null || Imagemhead_2.Image != null || Imagemhead_3.Image != null)
{
PrintDocument tPrinter = new PrintDocument();
tPrinter.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
tPrinter.Print();
}
}
What I have tried:
this is my code:
switch (PageNumber)
{
case 1:
if (numericUpDown1.Value == 1)
{
e.Graphics.DrawImage(Imagemhead_3.Image, x2, y2, width2, height2);
}
else if (numericUpDown1.Value == 2)
{
e.Graphics.DrawImage(Imagemhead_1.Image, x3, y3, width3, height3);
e.Graphics.DrawImage(Imagemhead_2.Image, x4, y4, width4, height4);
}
string stringToPrint = textBox1.Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
Font drawFont = new Font("Calibri", 32, FontStyle.Bold);
SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.FromArgb(78, 87, 88));
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 540, 830, 100);
e.Graphics.DrawString(stringToPrint, drawFont, drawBrush, rect, sf);
string stringToPrint2 = textBox2.Text;
StringFormat sf2 = new StringFormat();
sf2.Alignment = StringAlignment.Center;
Font drawFont2 = new Font("Calibri", 20, FontStyle.Bold, GraphicsUnit.Pixel);
SolidBrush drawBrush2 = new SolidBrush(System.Drawing.Color.FromArgb(98, 166, 10));
System.Drawing.RectangleF rect2 = new System.Drawing.RectangleF(0, 595, 830, 100);
e.Graphics.DrawString(stringToPrint2, drawFont2, drawBrush2, rect2, sf2);
string infoString = "";
int descent;
int lineSpacing;
float lineSpacingPixel;
FontFamily fontFamily = new FontFamily("Calibri Light");
Font font = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size, FontStyle.Regular, GraphicsUnit.Pixel);
System.Drawing.RectangleF rect3 = new System.Drawing.RectangleF(10, 450, 800, 350);
SolidBrush solidBrush = new SolidBrush(Color.Black);
rect3.Y += 5 * font.Height;
descent = fontFamily.GetCellDescent(FontStyle.Regular);
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);
rect3.Y += 5 * font.Height;
lineSpacingPixel =
font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = richTextBox1.Text.Trim();
e.Graphics.DrawString(infoString, font, solidBrush, rect3);
e.Graphics.DrawImage(Imagemfoot_1.Image, x5, y5, width5, height5);
e.Graphics.DrawImage(Imagemfoot_2.Image, x6, y6, width6, height6);
e.Graphics.DrawImage(Imagemfoot_3.Image, x7, y7, width7, height7);
e.Graphics.DrawImage(Logo_Footer.Image, x8, y8, width8, height8);
PageNumber++;
e.HasMorePages = true;
return;
case 2:
PageNumber++;
e.HasMorePages = true;
return;
case 3:
PageNumber++;
e.HasMorePages = true;
return;
case 4:
string stringToPrint5 = textBox1.Text;
StringFormat sf5 = new StringFormat();
sf5.Alignment = StringAlignment.Center;
Font drawFont5 = new Font("Calibri", 32, FontStyle.Bold);
SolidBrush drawBrush5 = new SolidBrush(System.Drawing.Color.FromArgb(78, 87, 88));
System.Drawing.RectangleF rect5 = new System.Drawing.RectangleF(0, 540, 830, 100);
e.Graphics.DrawString(stringToPrint5, drawFont5, drawBrush5, rect5, sf5);
e.Graphics.DrawImage(Imagemfoot_1.Image, x5, y5, width5, height5);
e.Graphics.DrawImage(Imagemfoot_2.Image, x6, y6, width6, height6);
e.Graphics.DrawImage(Imagemfoot_3.Image, x7, y7, width7, height7);
e.Graphics.DrawImage(Logo_Footer.Image, x8, y8, width8, height8);
PageNumber++;
e.HasMorePages = true;
return;
case 5:
string stringToPrint6 = textBox1.Text;
StringFormat sf6 = new StringFormat();
sf6.Alignment = StringAlignment.Center;
Font drawFont6 = new Font("Calibri", 32, FontStyle.Bold);
SolidBrush drawBrush6 = new SolidBrush(System.Drawing.Color.FromArgb(78, 87, 88));
System.Drawing.RectangleF rect6 = new System.Drawing.RectangleF(0, 90, 830, 100);
e.Graphics.DrawString(stringToPrint6, drawFont6, drawBrush6, rect6, sf6);
string stringToPrint7 = textBox2.Text;
StringFormat sf7 = new StringFormat();
sf7.Alignment = StringAlignment.Center;
Font drawFont7 = new Font("Calibri", 20, FontStyle.Bold, GraphicsUnit.Pixel);
SolidBrush drawBrush7 = new SolidBrush(System.Drawing.Color.FromArgb(98, 166, 10));
System.Drawing.RectangleF rect7 = new System.Drawing.RectangleF(0, 145, 830, 100);
e.Graphics.DrawString(stringToPrint7, drawFont7, drawBrush7, rect7, sf7);
string stringToPrint8 = "Rates and conditions";
StringFormat sf8 = new StringFormat();
sf8.LineAlignment = StringAlignment.Center;
sf8.Alignment = StringAlignment.Center;
Font drawFont8 = new Font("Calibri", 25, FontStyle.Bold, GraphicsUnit.Pixel);
SolidBrush drawBrush8 = new SolidBrush(System.Drawing.Color.White);
System.Drawing.RectangleF rect8 = new System.Drawing.RectangleF(0, 180, 830, 45);
e.Graphics.FillRectangle(drawBrush6, rect8);
e.Graphics.DrawString(stringToPrint8, drawFont8, drawBrush8, rect8, sf8);
e.Graphics.DrawImage(pictureBox3.Image, x9, y9, width9, height9);
StringFormat sfH2 = new StringFormat();
sfH2.Alignment = StringAlignment.Center;
Font drawFontH2 = new Font("Calibri Light", 20, FontStyle.Bold, GraphicsUnit.Pixel);
SolidBrush drawBrushH2 = new SolidBrush(System.Drawing.Color.White);
System.Drawing.RectangleF rectH21 = new System.Drawing.RectangleF(280, 250, 120, 50);
System.Drawing.RectangleF rectH22 = new System.Drawing.RectangleF(600, 250, 120, 50);
e.Graphics.DrawString(textBox3.Text, drawFontH2, drawBrushH2, rectH21, sfH2);
e.Graphics.DrawString(textBox4.Text, drawFontH2, drawBrushH2, rectH22, sfH2);
e.Graphics.DrawImage(Imagemfoot_1.Image, x5, y5, width5, height5);
e.Graphics.DrawImage(Imagemfoot_2.Image, x6, y6, width6, height6);
e.Graphics.DrawImage(Imagemfoot_3.Image, x7, y7, width7, height7);
e.Graphics.DrawImage(Logo_Footer.Image, x8, y8, width8, height8);
e.HasMorePages = false;
return;
}