在IFrame中打印PDF后自动关闭窗口

时间:2015-11-27 15:29:33

标签: javascript c# asp.net pdf itextsharp

我正在开发一个Web应用程序,该应用程序要求用户能够运行报告并让它自动在客户端计算机上显示打印对话框以进行打印。

我的一切正常。

当用户点击打印按钮时,会生成RDLC LocalReport,并在新窗口/窗体中借助iTextSharp作为pdf文档返回,并显示打印对话框。

我希望能够在用户打印或取消打印对话框后自动关闭表单。

aspx目前如下(略微剥离版本):

    <body>
        <form id="form1" runat="server">
            <div>
                <iframe id="frmPrint" name="IframeName" width="500" height="200" runat="server"> </iframe>
            </div>
        </form>
    </body>

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();

我尝试过使用JavaScript关闭窗口的各种方法,但到目前为止还没有成功。

非常感谢任何想法。

1 个答案:

答案 0 :(得分:0)

我最终找到了解决方案。

使用我在别处找到的一些代码来处理来自PDF文档的消息。这使我能够为PDF打印事件添加一个监听器,并在主机容器上发送一条消息,该消息可以被JavaScript接收。

在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();
    });

在守则背后:

    // 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);

除了用户在打印对话框中单击取消之外,这种情况很有效,我发现无法检测到。