如何以编程方式从PrintDocument打印特定页面?

时间:2011-10-14 00:21:50

标签: c# printing

有没有办法以编程方式从System.Drawing.Printing.PrintDocument中打印选定的页面(例如,第3-5页)?我正在使用此代码:

        myPrintDocument.Print();

但是这种方式不能以编程方式跳过页面。我已经考虑过显示modified version of the PrintDialog class(我想要的页面的索引作为参数)的可能性,跳过我不想要的页面,在我以编程方式关闭PrintDialog窗口时打印文档(所以它只闪烁)。但我意识到这真的很痛苦和肮脏。必须有一种更简单的方法,对吧?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

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

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

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

打印控制器不会决定PrintPage事件打印的内容。它仅在您的范围内每个计数页面提升一次事件。例如,如果您指定FromPage = 4到FromPage = 6,打印控制器将提高完全相同数量的PrintPage事件,它甚至不知道差异!它不知道使用PrintPage事件打印哪个页面。它只知道将PrintPage事件提升3次。

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

这是伪代码:

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

答案 1 :(得分:1)

这样的事情怎么样?

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