【问题标题】:Detect when a PrintDocument successfully prints (not just previewed)检测 PrintDocument 何时成功打印(不仅仅是预览)
【发布时间】:2011-09-10 08:43:12
【问题描述】:

我正在我的应用程序中使用PrintDocument 进行一些自定义打印。当我们的项目成功打印时,我需要记录。我最初是通过以下方式实现的:

 print_doc.EndPrint += (o,e) => printed_callback ();

让我的printed_callback 在打印完成时被调用。但是,现在我添加了预览支持,我将以完全相同的方式将PrintDocument 传递给PrintPreviewDialog。这样做会导致在预览所需的打印输出初始呈现之后调用EndPrint 事件。

因此,即使用户单击“预览”然后关闭预览,我们的日志记录代码也会被调用。

关于如何区分真实打印输出和“预览打印”的任何建议?不幸的是,我不能只是不连接到EndPrint,因为用户可能会单击预览对话框中的“打印”按钮并触发打印输出。

【问题讨论】:

    标签: c# .net winforms printing


    【解决方案1】:

    好的,所以我实际上设法自己解决了这个问题,使用PrintDocument.PrintController 属性,并检查控制器的IsPreview 属性。我的最终编码结果如下:

    doc.EndPrint += (o,e) =>
    {
        if (doc.PrintController.IsPreview)
            return;
    
        print_callback ();
    }
    

    【讨论】:

    • 双+1,问答:-D
    • 上述方法是在 PrintDocument 中的所有页面都被假脱机后。它不检查作业是否已完成。如果您想检查作业何时完成,请查看PrintQueue
    【解决方案2】:

    我也设法找到了一种对我有用的不同方法……

    我有一个 MyPrintFileDetail 类的列表,每个类都包含所述文档的 PrintDocument 和 StreamReader。

    在设置我的 PrintDocument 时,我添加了一个 PrintPage 事件。在 PrintPage 事件处理程序中,我通过将“发送者”转换为 PrintDocument 来确定我正在使用的 PrintDocument。然后编写了一个 foreach 循环来从列表中识别正在工作的 MyPrintFileDetail 对象,以获取我用来打印的 StreamReader。一旦没有更多的行要打印,我就处理掉了 StreamReader 并将其设置为 null。

    然后在用于处理 MyPrintFileDetail 对象列表的 Timer 回调中,我检查了 StreamReader 是否为 null,如果为 null,则打印完成。

    有点笨拙,但确实有效。

        private void PD_PrintPage(object sender, PrintPageEventArgs e)
        {
            PrintDocument p = (PrintDocument)sender;
    
            PrintFileDetail pfdWorkingOn = null;
    
            foreach (PrintFileDetail pfd in pfds)
            {
                if (pfd._PrintDoc.DocumentName == p.DocumentName)
                {
                    pfdWorkingOn = pfd;
                    break;
                }
            }
    
            float yPos = 0f;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            float linesPerPage = e.MarginBounds.Height / _TextFilePrintingFont.GetHeight(e.Graphics);
    
            while (count < linesPerPage)
            {
                line = pfdWorkingOn._TxtFileBeingPrinted.ReadLine();
                if (line == null)
                {
                    break;
                }
                yPos = topMargin + count * _TextFilePrintingFont.GetHeight(e.Graphics);
                e.Graphics.DrawString(line, _TextFilePrintingFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                count++;
            }
    
            if (line != null)
            {
                e.HasMorePages = true;
            }
            else
            {
                pfdWorkingOn._TxtFileBeingPrinted.Dispose();
                pfdWorkingOn._TxtFileBeingPrinted = null;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多