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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim

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

    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


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: How to get number of pages for printing...?

    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]
    - mvp@spam.guard. caspershouse.co m

    "Tim" <tim@home.com > wrote in message
    news:m8P4f.1849 1$GH1.136711@ne ws20.bellglobal .com...[color=blue]
    > 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
    >[/color]


    Comment

    • Peter

      #3
      Re: How to get number of pages for printing...?

      Hi Nicholas, Hi Tim

      I have a similar Problem. Creating my own "PrintPrevi ew" 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 PrintPreviewCon trol.StartPage can not set to values >=
      "PageCount"-1

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

      int currentPage = this.ppv1.Start Page;
      this.ppv1.Start Page = 999999;
      int pages = this.ppv1.Start Page+1;
      this.ppv1.Start Page = 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 PrintPreviewCon trol 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:
      [color=blue]
      > 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]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Tim" <tim@home.com > wrote in message
      > news:m8P4f.1849 1$GH1.136711@ne ws20.bellglobal .com...[color=green]
      > > 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
      > >[/color][/color]

      Comment

      • Bruce Wood

        #4
        Re: How to get number of pages for printing...?

        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.
        [color=blue]
        >From the .NET Framework's point of view, your PrintDocument is a black[/color]
        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 PrintPreviewCon trol,
        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 PrintPreviewCon trol works only because the first
        thing that PrintPreviewCon trol 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.

        Comment

        Working...