如何以编程方式从PrintDocument打印特定页面? [英] How to print specific pages from PrintDocument programmatically?

查看:52
本文介绍了如何以编程方式从PrintDocument打印特定页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过程序(例如第3-5页)以编程方式仅打印 System.Drawing.Printing.PrintDocument 中的选定页面?

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

我正在使用以下代码:

myPrintDocument.Print();

但是那样我不能以编程方式跳过页面.我曾考虑过可能显示 PrintDialog类的修改版本,跳过不需要的页面,打印文档并以编程方式关闭PrintDialog窗口(以便它仅闪烁一次).但这会有点骇人听闻.

But that way I can't skip pages programmatically. I've thought 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.

推荐答案

您可以在PrintDocument.BeginPrint事件中设置PrinterSettings.

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

PrintDocument.PrintPage事件(用于放置打印代码)将由每页打印控制器"引发,直到在PrinterSettings中设置的最后一页.例如,如果您有一个10页的PrintDocument并设置了PrintDocument.PrinterSettings.ToPage = 5,则打印控制器将为每个页面引发PrintPage事件,直到处理第5页为止.

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.

如果要跳过页面,例如PrinterSettings.FromPage = 3到ToPage = 5,则打印控制器将引发3次PrintPage事件.那是每个计数从3到5(含3)的一个PrintPage事件.

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.

打印控制器无法确定PrintPage事件将打印哪些内容.它只会在您范围内的每个计数页面引发一次事件.例如,如果您指定FromPage = 4到FromPage = 6,则打印控制器将引发完全相同数量的PrintPage事件,并且它甚至不知道区别!它不知道通过PrintPage事件正在打印哪个页面.它只知道引发3次PrintPage事件.

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.

我的意思是,您在PrintPage事件中的代码必须处理所有跳过的页面,而无需绘制任何内容.然后,该SAME PrintPage事件中的代码必须处理并绘制要打印的当前页面",然后返回,以便打印控制器可以引发下一个PrintPage事件.

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.

这是伪代码:

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

这篇关于如何以编程方式从PrintDocument打印特定页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆