Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I've been trying to figure out why my program only prints blank pages!

I use VS2010 with C# WPF. The program itself does not crash or throw any error, but it goes through the motions of printing what's in the textBox.
I don't really know what I'm missing here, I've looked at some examples in books, web, etc... but no avail!

I would welcome any suggestions if any.

Here's the printing part of the code:
C#
private void printNumberList(object sender, RoutedEventArgs e)
{
    PrintDocument textPrintPage = new PrintDocument();
    PrintDialog textPrintPreviewDialog = new PrintDialog();

    if (textPrintPreviewDialog.ShowDialog() == true)
       {
          textPrintPage.Print();
       }
    }

private void textPrintPage_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int x = e.MarginBounds.Left;
            int y = e.MarginBounds.Top;
            using (Font font = new Font("Times New Roman", 20))
         {

            string [] lines = new string[25];
            for (int i = 0; i< lines.Length-1; i++)
            {
                lines[i] = dataFileText.GetLineText(i);
            }

          e.Graphics.DrawString("List of numbers", font, Brushes.Black, x, y + 20);
          y += 40;

             for (int i = 0; i < lines.Length - 1; i++)
             {
                    e.Graphics.DrawString(lines[i], font, Brushes.Black, x, y);
                    y += 30;
             }

           }
            e.HasMorePages = false;

        }
Posted
Updated 29-Nov-12 8:00am
v2

1 solution

You need to connect the Print Event

C#
private void printNumberList(object sender, RoutedEventArgs e)
{
    PrintDocument textPrintPage = new PrintDocument();

    textPrintPage.PrintPage += new EventHandler<PrintPageEventArgs>(textPrintPage_PrintPage);

    PrintDialog textPrintPreviewDialog = new PrintDialog();
 
    if (textPrintPreviewDialog.ShowDialog() == true)
       {
          textPrintPage.Print();
       }
    }
 
Share this answer
 
v2
Comments
Remi Lebrun 29-Nov-12 17:18pm    
Thank you so much for your help Matthew, the problem is solved now!
I must admit i am a beginner at C# and you guys are PRO's.

After changing the line code to:

textPrintPage.PrintPage += new PrintPageEventHandler (textPrintPage_PrintPage);

I finally got good results; i just forgot to generate a stub for the function!

Best regards,

Remi Lebrun

ps, i hope i marked the right solution answered!

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