如何打印所有页面? [英] How do I print all the pages?

查看:90
本文介绍了如何打印所有页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在同一个文档中打印3页,但它只打印我的最后一页。



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

推荐答案

嗯...第2页& 3不要;打印任何东西,所以它们都是空白的。



除此之外,使用调试器做两件事:

1)检查值在按钮单击事件处理程序中的PageNumber。你没有显示任何设置或重置它的代码,所以它可以这么简单。

2)在PrintPage事件处理程序的顶部放置一个断点并通过代码查找确切地说它的位置。



我们不能为您做到这一点 - 我们无法访问您的机器!
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!

这篇关于如何打印所有页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆