[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1887
  • Last Modified:

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...

0
pacifix
Asked:
pacifix
  • 3
  • 3
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now