PC Review


Reply
Thread Tools Rate Thread

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

 
 
Tim
Guest
Posts: n/a
 
      17th Oct 2005
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


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      17th Oct 2005
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 Removed)

"Tim" <(E-Mail Removed)> wrote in message
news:m8P4f.18491$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
Peter
Guest
Posts: n/a
 
      31st Oct 2005
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


Nicholas Paldino [.NET/C# MVP] schrieb:

> 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 Removed)
>
> "Tim" <(E-Mail Removed)> wrote in message
> news:m8P4f.18491$(E-Mail Removed)...
> > 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
> >


 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      1st Nov 2005
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.

 
Reply With Quote
 
New Member
Join Date: Jul 2012
Posts: 1
 
      17th Jul 2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Number of pages should not include the Table of Content pages. =?Utf-8?B?TWFyaWx5bg==?= Microsoft Word Document Management 3 28th Jul 2005 05:37 PM
can get page number to display, but cannot get page number to pri. =?Utf-8?B?Ym9jaHJhbg==?= Microsoft Word Document Management 2 26th Jan 2005 01:07 AM
Number of pages excluding content pages =?Utf-8?B?TnVtYmVyIG9mIHBhZ2VzIGV4Y2x1ZGluZyBjb250 Microsoft Word Document Management 4 11th Jan 2005 03:21 PM
Re: Help! I have 14 pages in a worksheet, pages 8-14 are blank and print that way. How can I delete only the blank pages? I tried delete worksheet and it deleted the whole thing. Frank Kabel Microsoft Excel New Users 1 28th Apr 2004 09:20 PM
Pages pages and more pages Microsoft Word Document Management 1 16th Oct 2003 09:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:33 AM.