有没有办法以System.Drawing.Printing.PrintDocument编程方式仅打印选定的页面(例如第 3-5 页)?
我正在使用这段代码:
myPrintDocument.Print();
Run Code Online (Sandbox Code Playgroud)
但这样我就无法以编程方式跳过页面。我考虑过显示PrintDialog 类的修改版本、跳过我不想要的页面、打印文档并以编程方式关闭 PrintDialog 窗口(以便它仅闪烁)的可能性。但这有点麻烦。
PrinterSettings您可以在事件中设置PrintDocument.BeginPrint。
您放置打印代码的事件PrintDocument.PrintPage将由打印控制器针对每一页引发,直到PrinterSettings. 例如,如果您有 10 页PrintDocument并且设置了PrintDocument.PrinterSettings.ToPage = 5,则打印控制器将为PrintPage每个页面引发该事件,直到处理第 5 页。
如果您要跳页,例如PrinterSettings.FromPage = 3跳至ToPage = 5,则打印控制器将引发PrintPage事件 3 次。PrintPage从 3 到 5(含),每计数一个事件。
打印控制器不决定事件打印什么内容PrintPage。它只会在范围内的每个计数页面引发一次事件。例如,如果您指定FromPage = 4to FromPage = 6,打印控制器将引发完全相同数量的PrintPage事件,并且它甚至不知道差异!它不知道该PrintPage事件正在打印哪一页。它只知道引发该PrintPage事件 3 次。
我的观点是,您的代码PrintPage必须处理所有跳过的页面,而不绘制任何内容。然后,该同一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!
}
Run Code Online (Sandbox Code Playgroud)