Q:

Auto close window after print PDF within IFrame

I am working on a web application that requires the user be able to run a report and have it automatically display the print dialog on the client machine for printing.
I have got that all working fine.
When the user clicks the print button an RDLC LocalReport is generated and returned as a pdf document with the assistance of iTextSharp in a new window/form and the print dialog is displayed.
I would like to be able to automatically close the form after the user either prints or cancels from the print dialog.
The aspx is currently as below (slightly stripped down version):
    <body>
        <form id="form1" runat="server">
            <div>
                <iframe id="frmPrint" name="IframeName" width="500" height="200" runat="server"> </iframe>
            </div>
        </form>
    </body>

The code within the aspx.cs:
        FileStream LFileStream = new FileStream(HttpContext.Current.Server.MapPath(Session[LReportNameSrcString].ToString()), FileMode.Create);
        LFileStream.Write(LResult, 0, LResult.Length);
        LFileStream.Close();
        //Open existing PDF
        Document LDocument = new Document(PageSize.A4);
        PdfReader LPdfReader = new PdfReader(HttpContext.Current.Server.MapPath(Session[LReportNameSrcString].ToString()));
        //Getting a instance of new PDF writer
        PdfWriter LPdfWriter = PdfWriter.GetInstance(LDocument, new FileStream(HttpContext.Current.Server.MapPath(Session[LReportNameString].ToString()), FileMode.Create));
        LDocument.Open();
        PdfContentByte LPdfContentByte = LPdfWriter.DirectContent;

        int LPageNumber = 0;
        int LNumberOfPages = LPdfReader.NumberOfPages;

        //Add Page to new document
        while (LPageNumber < LNumberOfPages)
        {
            LDocument.NewPage();
            LPageNumber++;

            PdfImportedPage LPdfImportedPage = LPdfWriter.GetImportedPage(LPdfReader, LPageNumber);
            LPdfContentByte.AddTemplate(LPdfImportedPage, LLocalReport.GetDefaultPageSettings().Margins.Left, LLocalReport.GetDefaultPageSettings().Margins.Top);
        }

        //Attach javascript to the document
        //PdfAction LPdfAction = PdfAction.JavaScript("alert('loaded');parent.beginPrint();\r", LPdfWriter);
        PdfAction LPdfAction = PdfAction.JavaScript("this.print();\r", LPdfWriter);
        LPdfWriter.AddJavaScript(LPdfAction);
        LDocument.Close();

        //Attach pdf to the iframe
        frmPrint.Attributes["src"] = Session[LReportNameString].ToString();

I have tried various methods using JavaScript to close the window but have been so far unsuccessful.
Any ideas would be greatly appreciated.

A:

I found the solution myself in the end.
Using some code I found elsewhere for handling messages from the PDF document. This enabled me to add a listener for the PDF printed event and fire a message at the host container which can be picked up by the JavaScript.
In the ASPX:
    // Acrobat JavaScript event handler to handle messages returned from     Acrobat document 
    function messageFunc(messageArray) {
        window.close();
    }

    // Waits until the HTML objects ready / loaded state has been reached
    // then add a listener for JavaScript messages being returned from Acrobat document
    function loadListener() {
        var pdfObject = document.getElementById("pdfObj");
        if (typeof pdfObject.readyState === 'undefined') { // ready state only works for IE, which is good because we only need to do this for IE because IE sucks in the first place
            pdfObject.messageHandler = { onMessage: messageFunc };
            return;
        }
        if (pdfObject.readyState == 4) {
            pdfObject.messageHandler = { onMessage: messageFunc };
        } else {
            setTimeout(loadListener, 500);
        }
    }

    // Wait until main HTML document has loaded then call function to set up JavaScript event listeners
    jQuery(document).ready(function() {
        loadListener();
    });

In the Code behind:
    // Add event listener to detect when the print has begun then return a message (in this case an empty string) to the browser 
    // window to be picked up by a JavaScript event listener which can then close the window
    const string LPostMessageString = "try { this.hostContainer.postMessage(['']); } catch(e) {app.alert(e.message);}";
    LPdfAction = PdfAction.JavaScript(LPostMessageString, LPdfWriter);
    LPdfWriter.SetAdditionalAction(PdfWriter.DID_PRINT, LPdfAction);

This works well apart from if the user clicks cancel in the print dialog, of which I found no way to detect.

