Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
                           /// Imagens de header
                           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);
                           }

                           /// Titulo
                           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);

                           /// Duração
                           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);

                           /// Pequena descrição

                           string infoString = "";  // enough space for one line of output
                           int descent;            // font family descent in design units
                           int lineSpacing;        // font family line spacing in design units
                           float lineSpacingPixel; // line spacing converted to pixels

                           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);

                           // Move down five lines.
                           rect3.Y += 5 * font.Height;

                           // Display the descent in design units and pixels.
                           descent = fontFamily.GetCellDescent(FontStyle.Regular);

                           // Display the line spacing in design units and pixels.
                           lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);

                           // Move down one line.
                           rect3.Y += 5 * font.Height;

                           // 18.398438 = 16.0 * 2355 / 2048
                           lineSpacingPixel =
                           font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular);
                           infoString = richTextBox1.Text.Trim();
                           e.Graphics.DrawString(infoString, font, solidBrush, rect3);

                           /// Imagens Footer

                           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);

                           /// Footer Logo

                           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:
                           /// Descrições

                           /// Titulo
                           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);

                           /// Imagens Footer

                           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);

                           /// Footer Logo

                           e.Graphics.DrawImage(Logo_Footer.Image, x8, y8, width8, height8);

                           PageNumber++;
                           e.HasMorePages = true;
                           return;

                       case 5:
                           /// Custos

                           /// Titulo
                           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);

                           /// Duração
                           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);

                           /// Termos Condições
                           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);

                           /// Tabela

                           e.Graphics.DrawImage(pictureBox3.Image, x9, y9, width9, height9);

                           /// Fontes e StringFormats da tabela

                           /// Font Header2
                           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);

                           /// Imagens Footer

                           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);

                           /// Footer Logo

                           e.Graphics.DrawImage(Logo_Footer.Image, x8, y8, width8, height8);

                           e.HasMorePages = false;
                           return;
                   }
Posted
Updated 5-Jun-18 8:22am

1 solution

Well ... pages 2 & 3 dont; print anything, so they will be blank.

Other than that, use the debugger to do two things:
1) Check the value of PageNumber in your button click event handler. You don't show any code for setting or resetting it, so it could be as simple as that.
2) Put a breakpoint at the top of the PrintPage event handler and step though the code to find out exactly where it is going.

We can't do that for you - we don't have any access to your machine!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900