PrintPreviewDialog显示页面但打印空白

时间:2015-08-20 07:02:44

标签: winforms c#-4.0 print-preview

我尝试使用之前创建的用户打印页面。 PrintPreviewDialog显示格式正确但当我单击打印按钮时,打印机打印一个空白页。 使用PrintDialog()我也会得到空白页面。我从表单设计器属性中为PrintDialog()PrintPreviewDialog()分配了PrintDocument值。 这是我的代码。谢谢你的帮助。问候。

private void printButton_Click_1(object sender, EventArgs e)
  {
   if (userGenerator != null)
        {
            try
            {
                printDocument.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1170);
                if (imageLogo == null)
                {
                    userCount = 0;
                    printDocument.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                }
                else
                {
                    userCount = 0;
                    printDocument.PrintPage += new PrintPageEventHandler(pd_PrintPage_WithImage);
                }
                printPreviewDialog.ShowDialog();

                if (printDialog.ShowDialog() == DialogResult.OK)
                {
                    printDocument.Print();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured while printing", ex.ToString());
            }
        }
        else
        {
            MessageBox.Show("You have to create users first.");
        }
    }

  private void pd_PrintPage_WithImage(object sender, PrintPageEventArgs ev)
    {
        int userX = 15, userY = 70;
        int lineCount = 0;
        int linesPerPage = 9;

        User.User printedUser = null;

        if (userCount < userGenerator.users.Count)
        {
            printedUser = userGenerator.users[userCount];
        }

        for (int i = userCount; i < userGenerator.users.Count; i++)
        {
            if (lineCount < linesPerPage)
            {
                printedUser = userGenerator.users[userCount];

                //Horizontal line
                ev.Graphics.DrawLine(Pens.Black, userX, userY, userX + 250, userY);

                //Draw logo
                ev.Graphics.DrawImage(imageLogo, userX + 100, userY + 5, 40, 40);
                ev.Graphics.DrawString("Username: " + printedUser.username + Environment.NewLine + "Password: " + printedUser.password, printFont, Brushes.Black, userX, userY + 60);

                //Horizontal lines
                ev.Graphics.DrawLine(Pens.Black, userX, userY + 50, userX + 250, userY + 50);
                ev.Graphics.DrawLine(Pens.Black, userX, userY + 120, userX + 250, userY + 120);

                //Vertical lines
                ev.Graphics.DrawLine(Pens.Black, userX, userY, userX, userY + 120);
                ev.Graphics.DrawLine(Pens.Black, userX + 250, userY, userX + 250, userY + 120);
                userCount++;

                if (userCount % 3 == 0)
                {
                    userX = 15;
                    userY += 120;
                    lineCount++;
                }
                else
                {
                    userX += 270;
                }
            }
        }

        if (printedUser != null && userCount < userGenerator.users.Count)
        {
            ev.HasMorePages = true;
        }
        else
        {
            ev.HasMorePages = false;
        }
    }

  private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        int userX = 15, userY = 70;
        int lineCount = 0;
        int linesPerPage = printFont.Height;

        User.User printedUser = null;

        if (userCount < userGenerator.users.Count)
        {
            printedUser = userGenerator.users[userCount];
        }

        for (int i = userCount; i < userGenerator.users.Count; i++)
        {
            if (lineCount < linesPerPage)
            {
                printedUser = userGenerator.users[userCount];
                ev.Graphics.DrawString("Username: " + printedUser.username + Environment.NewLine + "Password: " + printedUser.password, printFont, Brushes.Black, userX, userY);
                ev.Graphics.DrawLine(Pens.Black, userX, userY, userX + 250, userY);
                ev.Graphics.DrawLine(Pens.Black, userX, userY + 50, userX + 250, userY + 50);

                ev.Graphics.DrawLine(Pens.Black, userX, userY, userX, userY + 50);
                ev.Graphics.DrawLine(Pens.Black, userX + 250, userY, userX + 250, userY + 50);
                userCount++;

                if (userCount % 3 == 0)
                {
                    userX = 15;
                    userY += 50;
                    lineCount++;
                }
                else
                {
                    userX += 270;
                }
            }
        }

        if (printedUser != null && userCount < userGenerator.users.Count)
        {
            ev.HasMorePages = true;
        }
        else
        {
            ev.HasMorePages = false;
        }
    }

0 个答案:

没有答案