2015-11-27 80 views
0

我正在研究一個Web應用程序,該程序要求用戶能夠運行報告並自動在客戶機上顯示打印對話框以進行打印。在IFrame內打印PDF後的自動關閉窗口

我得到了所有工作正常。

當用戶點擊一個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來關閉該窗口嘗試了各種方法但迄今爲止不成功。

任何想法將不勝感激。

回答

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

這從當用戶點擊打印對話框,其中我發現沒有辦法檢測取消分開運作良好。