gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:30:37 29 4
gpt4 key购买 nike

有没有办法以编程方式仅打印 System.Drawing.Printing.PrintDocument 中的选定页面(例如第 3-5 页)?

我正在使用这段代码:

myPrintDocument.Print();

但那样我就无法以编程方式跳过页面。我考虑过显示 modified version of the PrintDialog class 的可能性,跳过我不想要的页面,打印文档并以编程方式关闭 PrintDialog 窗口(以便它只闪烁)。但这有点像 hack。

最佳答案

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

PrintDocument.PrintPage 事件,您放置打印代码的地方将由打印 Controller 引发,用于每一页,直到 PrinterSettings 中设置的最后一页。例如,如果您有一个 10 页的 PrintDocument 并且您设置了 PrintDocument.PrinterSettings.ToPage = 5,那么打印 Controller 将提升 PrintPage 每个页面的事件,直到它处理第 5 个页面。

如果您正在跳过页面,例如 PrinterSettings.FromPage = 3ToPage = 5,那么打印 Controller 将引发一个 PrintPage事件3次。这是从 3 到 5(含)的每个计数一次 PrintPage 事件。

打印 Controller 不决定 PrintPage 事件打印什么内容。它只会在您范围内的每个计数页面引发一次事件。例如,如果您将 FromPage = 4 指定为 FromPage = 6,打印 Controller 将引发完全相同数量的 PrintPage 事件,但它不会甚至不知道区别!它不知道正在使用 PrintPage 事件打印哪一页。它只知道引发 3 次 PrintPage 事件。

我的观点是,您在 PrintPage 事件中的代码必须处理所有跳过的页面而不绘制任何内容。然后您在同一 PrintPage 事件中的代码必须处理并绘制您要打印的“当前页面”,然后返回以便打印 Controller 可以引发下一个 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!
}

关于c# - 如何以编程方式打印 PrintDocument 中的特定页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761897/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com