开发者

Detect when a PrintDocument successfully prints (not just previewed)

I'm doing some custom printing using a PrintDocument in my application. I have a requirement of logging when our items are successfully printed. I originally achieved this with something like:

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

To make my printed_callback get invoked when a print finished. However, now that I'm adding preview support, I'开发者_开发技巧m passing a PrintDocument constructed in exactly the same way into a PrintPreviewDialog. Doing so causes the EndPrint event to be invoked after the initial rendering of the printout needed for the preview.

As a result, even if a user clicks "Preview" and then just closes the preview, our logging code gets invoked.

Any suggestions for how to differentiate between a real printout and a "preview print" ? Unfortunately, I can't just not hook up to EndPrint for the PrintDocument passed to the PrintPreviewDialog since the user may click the "Print" button in the preview dialog and trigger a printout.


Ok, so I actually managed to figure this out myself, using the PrintDocument.PrintController property, and checking the IsPreview property of the controller. My final coded ended up as follows:

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

    print_callback ();
}


I too managed to figure out a different way that worked for me...

I had a list of MyPrintFileDetail classes each containing a PrintDocument and a StreamReader for said document.

While setting up my PrintDocument, I added an PrintPage event. In the PrintPage event handler I identified which PrintDocument I was working with via casting the "sender" to a PrintDocument. Then wrote a foreach loop to identify the working MyPrintFileDetail object from the list to get the StreamReader I was using to print. Once there were no more lines to print, I disposed of the StreamReader and set it to null.

Then within my Timer callback for processing the list of MyPrintFileDetail objects, I checked the StreamReader for null and if null, I was done printing.

Kind of clunky but it worked.

    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;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜