How to print only some pages ?

Hi,

I have a PrintDocument, a PrintPreviewDialog, and a PrintDialog.

I have a event that create a new PrintDocument and show the content in the PrintPreviewDialog (generate by the PrintPage delegate...).

In the PrintPreviewDialog, I have added a button in the upper toolbar to call the PrintDialog to let the user chose the pages to print (FromPage - ToPage)

But those properties (FromPage - ToPage) does not change anything when I print.
The print page delegate does not seem to care about the page setting of the PrintDocument.

In another approche, I can stop the printing in the PrintPage delegate when the number of page go over the property (PageTo) by setting e.HasMorePage to false. (easy!)

But I just can't find a way to begin PrintPage at page X... there is clearly some stuff that I don't understand in this creazy .net way of printing...

Sorry for my bad english... Quebec City 400...

pacifixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

J1H1W1Commented:
You control the printing.  If you want to start printing on page X, go to page X and start printing.  When you're finished printing page X and you want to print another page, set e.HasMorePages to true and print the next page.  Continue the procedure until you're finished printing.
0
JimBrandleyCommented:
Bon jour pacifix. Do not apologize for your English. It is far better than my barely-remembered high school French. J1H1W1 is correct. If you are just printing pages from some document file, just skip to that page whan you start. If you are constructing this document with code, you need to calculate page sizes, skipping data for each page until you get to the starting page.

Jim
0
pacifixAuthor Commented:
Hi,

I'm constructing the PrintDocument by code, it take around 10 seconds to generate it, so it would be nice to work with this build "PrintDocument". When I want to print after the preview, I just don't understand how to skip the first page in the "PrintPage" delegate. Can I erase some page in the BeginPrint ?

J1H1W1, it sound easy to write "Go to page X and start printing", but how do you do that when you are in the printpage delegate ?

It look's like there is still some stuff that I don,t understant well here...
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

JimBrandleyCommented:
Here's an example from some code I have. The variable names will be different, but the idea is the same. Basically, when you are creating pages in code, and need to support printing a range of pages, you assume that the current page is one you need to skip. Then compare the page number to the start page. If less, subtract the hight of any header and footer from the page height. Then determine how much data will fit in the remainder of the page. Set your data pointer past that, and repeat until you reach the starting page number.

Then print the page starting with the data pointed to as the start of that remaining to be printed.

Jim
 
private void  printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
   // flag for handling printing some pages rather than all
   bool printthispage = false;
 
   // current printing position
   float printpos = printmargins.Top;
 
   // increment page number (initially zero) & check page range 
   CurrentPage++;
   if ((CurrentPage >= fromPage) && (CurrentPage <= toPage))
       printthispage = true;
 
   // if this is the first page, then measure the print area
   if (1 == CurrentPage)
       measureprintarea(e.Graphics);
 
   //-----------------------------------------------------------------
   // scan down heights until we're off this (non-printing) page
   //-----------------------------------------------------------------
 
   while (!printthispage)
   {
       // calculate and increment over the page we're not printing
       printpos = printmargins.Top + headerHeight;
       while (printpos < (pageHeight - footerHeight - printmargins.Top - printmargins.Bottom))
       {
           lastrowprinted++;
           printpos += rowheights[lastrowprinted];
       }
 
       // skip to the next page & see if it's in the print range
       CurrentPage++;
       if ((CurrentPage >= fromPage) && (CurrentPage <= toPage))
           printthispage = true;
 
       // bottom check~
       if ((lastrowprinted >= rowstoprint.Count-1) || (CurrentPage > toPage))
       {
           // we're done here - nothing more to print
           e.HasMorePages = false;
           return;
       }
   }
 
   //-----------------------------------------------------------------
   // print headers
   //-----------------------------------------------------------------
 
   // Print the rest of this page...
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pacifixAuthor Commented:
Thank you for your time :-)
0
pacifixAuthor Commented:
Hi Jim,

Thank you for this clear and elaborate anser!

Jean

0
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.