首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以编程方式打印PrintDocument中的特定页面?

如何以编程方式打印PrintDocument中的特定页面?
EN

Stack Overflow用户
提问于 2011-10-14 08:21:50
回答 2查看 9.4K关注 0票数 1

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

我使用的是以下代码:

代码语言:javascript
复制
myPrintDocument.Print();

但这样我就不能以编程方式跳过页面。我考虑过这样一种可能性:显示一个modified version of the PrintDialog class,跳过我不想要的页面,打印文档,并以编程方式关闭PrintDialog窗口(这样它只会闪过)。但这将是一种黑客行为。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-29 06:08:57

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

打印控制器将为PrinterSettings中设置的直到最后一页的每一页引发PrintDocument.PrintPage事件,您将在其中放置打印代码。例如,如果您有一个10页的PrintDocument,并且您设置了PrintDocument.PrinterSettings.ToPage = 5,那么打印控制器将为每一页引发PrintPage事件,直到它处理第5页。

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

打印控制器不决定PrintPage事件要打印的内容。它只在您的范围内的每个计数页引发一次事件。例如,如果您将FromPage =4指定为FromPage = 6,则打印控制器将引发完全相同数量的PrintPage事件,但它甚至不知道其中的区别!它不知道使用PrintPage事件打印的是哪一页。它只知道引发PrintPage事件3次。

我的观点是,PrintPage事件中的代码必须处理所有跳过的页面,而不绘制任何内容。然后,同一PrintPage事件中的代码必须处理并绘制要打印的“当前页”,然后返回,以便打印控制器可以引发下一个PrintPage事件。

下面是伪代码:

代码语言:javascript
复制
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!
}
票数 4
EN

Stack Overflow用户

发布于 2011-10-14 08:33:01

像这样的怎么样?

代码语言:javascript
复制
PrinterSettings printSettings = new PrinterSettings()
            {
                FromPage = 3,
                ToPage = 5
            };
PrintDocument myPrintDocument = new PrintDocument()
            {
                PrinterSettings = printSettings
            };
myPrintDocument.Print();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7761897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档