开发者

How to print specific pages from PrintDocument programmatically?

开发者 https://www.devze.com 2023-04-12 17:59 出处:网络
Is there a way to print only selected pages from a System.Drawing.Printing.PrintDocument programmatically (eg. page 3-5)?

Is there a way to print only selected pages from a System.Drawing.Printing.PrintDocument programmatically (eg. page 3-5)?

I am using this code:

myPrintDocument.Print();

But that way I can't skip pages programmatically. I've thought开发者_JAVA技巧 about the possibility to show a modified version of the PrintDialog class, skip the pages I don't want, print the document and programmatically close the PrintDialog window (so that it only flashes by). But that would be a bit of a hack.


You can set the PrinterSettings in the PrintDocument.BeginPrint event.

The PrintDocument.PrintPage event, where you put your printing code will be raised by the Print Controller for Every Page up to the last page set in the PrinterSettings. For example, if you have a 10 page PrintDocument and you set the PrintDocument.PrinterSettings.ToPage = 5, then the print controller will raise the PrintPage event for every Page until it processes the 5th Page.

If you are skipping pages, for example PrinterSettings.FromPage = 3 to ToPage = 5, then the print controller will raise a PrintPage event 3 times. That is one PrintPage event per count from 3 to 5 inclusive.

The print controller does not decide what Content gets printed by the PrintPage event. It only raises the event once per each counted page in your range. For example, if you specify FromPage = 4 to FromPage = 6, the print controller would raise the exact same amount of PrintPage events and it doesn't even know the difference! It does not know which page it is printing with the PrintPage event. It only knows to raise the PrintPage event 3 times.

My point is that your Code in the PrintPage event must process all of your skipped pages without Drawing anything. Then your Code in that SAME PrintPage event must process and Draw the "current page" that you want to print, and then return so the print controller can raise the next PrintPage event.

Here is the pseudo code:

void Setup_Printing()
{
  myPrintDocument.BeginPrint += On_BeginPrint;
  myPrintDocument.PrintPage += On_PrintPage;
}

// Page Index variable
int indexCuurentPage = 0;

void On_BeginPrint(object sender, PrintEventArgs e)
{
  indexCuurentPage = 0;
  ((PrintDocument)sender).PrinterSettings.PrintRange = PrintRange.SomePages;
  ((PrintDocument)sender).PrinterSettings.FromPage = 3;
  ((PrintDocument)sender).PrinterSettings.ToPage = 5;
}


void On_PrintPage(object sender, PrintPageEventArgs ppea)
{
  // Set up a loop to process pages.
  bool bProcessingPages = true;
  while (bProcessingPages)
  {
    indexCurrentPage++;

    // Set isPageInRange flag to true if this indexCurrentPage is in the selected range of pages
    bool isPageInRange = (theCurrentPage >= ppea.PageSettings.PrinterSettings.FromPage);
    isPageInRange = isPageInRange && (theCurrentPage =< ppea.PageSettings.PrinterSettings.ToPage);

    if (isPageInRange)
    {
      // Process your data and print this page then exit the loop.  
      try
      {
        //// TO DO - Process Data ////
        //// TO DO - Draw Data to ppea.Graphics ////
        // Note:  Do not set the ppea.HasMorePages to true.  Let the Printer Controller do it.
      }
      catch
      {
        // Abort printing more pages if there was an error.
        ppea.HasMorePages = false;
      }
      // Set the loop exit flag.  We could use "return;" instead if we do not want to do more.
      bProcessingPages = false;
    }
    else
    {
      // Process your data and Do Not Draw on the ppea.Graphics.
      try
      {
        //// TO DO - Process Data ////
      }
      catch
      {
        // Abort printing more pages if there was an error.
        ppea.HasMorePages = false;
        // Set the loop exit flag.  We could use "return;" instead if we do not want to do more.
        bProcessingPages = false;
      }
      // Stay in the processing loop until you either need to actually Print a Page
      // or until you run out of data and pages to print.
    }

    // check to see if we ran out of pages to print
    if (indexCurrentPage >= ppea.PageSettings.PrinterSettings.ToPage)
    {
      // Done.  We do not need to set the HasMorePages = false
      bProcessingPages = false;
    }
  } // end while processing pages

  // The print controller will decide to raise the next PrintPage event if it needs to.
  // If You set the ppea.HasMorePages = false, then it will stop any more page events. 
  // If You set the ppea.HasMorePages = true, 
  //    then I'm not sure what the print controller will do if it ran out of pages!
}


What about something like this?

PrinterSettings printSettings = new PrinterSettings()
            {
                FromPage = 3,
                ToPage = 5
            };
PrintDocument myPrintDocument = new PrintDocument()
            {
                PrinterSettings = printSettings
            };
myPrintDocument.Print();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号