PaulD
PaulD

Reputation: 33

Printing a page number in each printed page in c#

I am new to using the printdocument feature in c# and needed some help. I need to print a header on each page that the printer prints and i need the page number on it.

I am currently using a string, concatenating to it and then printing it line by line in the printDocument_PrintPage method. i want to concatenate a variable to the string i print that keeps track of the page number that its being printed on.

Is this possible? This is my code so far for calling the printpage method:

    printDialog1.Document = printDocument1;

    if (printDialog1.ShowDialog() == DialogResult.OK)
        this.printDocument1.Print();

Upvotes: 3

Views: 5343

Answers (1)

Paul Sasik
Paul Sasik

Reputation: 81449

Yes. Just create a page counter field in the class that will be handling the print events. Something like:

private int _pageCount = 1;

The in your print page handler just append it to your header string and then increment it before exiting the handler. Something like:

private void PrintPageEventHandler(...
{
     string pageHeader = "Page # " + pageCount;

     //  printing code here

     pageCount++;
}

Upvotes: 5

Related Questions