How to get number of pages for printing...?

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

Hi,

I am creating my own method for printing. I am using PrintDocument and
cycling through the data and creating new pages as neccessary.

My question is, how can I know that total number of pages so that I can put
"Page 1 of 5" on the first page and also I would know when to draw the
report footer?

Can anyone offer any advice?

Tim
 
Tim,

Well, that's completely up to you. Since you are printing out
everything custom, you have to figure out, according to the data you are
laying out, what the number of pages will be. That's completely up to you.

Hope this helps.
 
Hi Nicholas, Hi Tim

I have a similar Problem. Creating my own "PrintPreview" I like to show
a Page-Selctor like: "Page ... of XYZ"

I think its not up to me....

E.G.
If there is a PrintDocument (created manually or by ReportGenerator
....) I can set the PrinterSettings and the PageSettings -->
consequently the number of Pages need to calculatet.

Besides :
The PrintPreviewControl.StartPage can not set to values >=
"PageCount"-1

There is a (not reliable) workaround to get the number of pages
(ppv1 is a PrintPreviewControl)

int currentPage = this.ppv1.StartPage;
this.ppv1.StartPage = 999999;
int pages = this.ppv1.StartPage+1;
this.ppv1.StartPage = currentPage;

No Exception happends but
pages contains now the number of pages of the PrintDocumnet "previewd"
with ppv1.

Funnily enough this works only it the PrintPreviewControl is visible on
screen !? So at the moment i do without "Page ... of XYZ"
Hope there is a propper way to get "Number of pages"


Peter

Tim,

Well, that's completely up to you. Since you are printing out
everything custom, you have to figure out, according to the data you are
laying out, what the number of pages will be. That's completely up to you.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
Hi,

I am creating my own method for printing. I am using PrintDocument and
cycling through the data and creating new pages as neccessary.

My question is, how can I know that total number of pages so that I can
put "Page 1 of 5" on the first page and also I would know when to draw the
report footer?

Can anyone offer any advice?

Tim
 
Tim, Peter:

The only way to get the number of pages of a custom PrintDocument is to
do a "dry run" of the entire printing process in order to determine the
page count, then do it again for real. Here is why.
From the .NET Framework's point of view, your PrintDocument is a black
box. All the Framework can do is call your OnBeginPrint following by
successive calls to OnPrintPage until OnPrintPage sets HasMorePages to
false. The outside agent doing the printing (the PrintPreviewControl,
for example) has absolutely no idea what's going on inside there, and
so has no idea how you determine which is the last page to print.

Given that, how could it ever determine, by itself, how many pages
there are to print? Since your routines are doing the page layout, your
routines are the only ones with any hope of knowing how many pages
there will be.

Peter's trick with PrintPreviewControl works only because the first
thing that PrintPreviewControl does when it displays is call your
PrintDocument object to render all of the pages. Since it's rendered
all of the pages once, it knows how many pages there are. That's why it
"knows" only when it's visible on the screen: because it's only at that
moment that it bothers to render the entire print document into visible
pages.

However, this doesn't help if you want to display "Page x of y" at the
bottom of each page. In this case, you yourself have to take a dry run
through the printing process and count the pages when you first create
an instance of your PrintDocument. Remember: your code is the only part
of the program that has any idea of what the pages look like, and
therefore how many there will be. As Nicholas said, it's up to you to
roll your own page counting algorithm. It may be as simple as counting
a particular kind of record ("one per page") or as complicated as
actually going through the motions of printing everything just to get
the page count. Whether you can take a short route depends upon how
you're laying out your data on the pages.
 
Hi Peter,

Your following code helped me a lot in finding out Total Pages in PrintPreviewControl.

int currentPage = this.ppv1.StartPage;
this.ppv1.StartPage = 999999;
int pages = this.ppv1.StartPage+1;
this.ppv1.StartPage = currentPage;

I use the following logic to put 'Pages x of xyz'.
1. While loading the PrintDocument in the PrintPreviewControl, I make the page numbers to print only 'Page 1' or 'Page 2' upto 'Page n'.

During this time (while document loading in print preview control), my PrintPage event has a Global Boolean variable xPrintPreview set to true.

2. After the document is loaded, I calculate the total pages (USING YOUR LOGIC)
3. Now I have print button, which again calls the 'PrintDoc.Print' method to start printing again. This also makes the global variable xPrintPreview to False temporarily.
4. In my printpage event, if xPrintPreview = false, I also pass on total pages using global variable and now I am printing 'page x or xyz'.

If you find better option, or a better print preview control with lot of navigations, please help me with your code
 
Back
Top