2015-01-08 103 views
0

我正在开发一个ASP.NET Web窗体应用程序,其中我必须生成pdf并在客户端打印它。我的朋友给出了这段代码:在服务器端生成PDF并使用itextsharp.dll在客户端打印它

 Response.ContentType = "application/pdf"; 
     Response.AddHeader("content-disposition", a); 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 
     n.RenderControl(hw); 
     deg.RenderControl(hw); 
     n.RenderControl(hw); 
     StringReader sr = new StringReader(sw.ToString()); 
     Document pdfDoc = new Document(PageSize.A4, 70f, 70f, 70f, 70f); 
     HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
     PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
     pdfDoc.Open(); 
     pdfDoc.NewPage(); 

     htmlparser.Parse(sr); 
     pdfDoc.Close(); 

     Response.Write(pdfDoc); 
     Response.End(); 

此代码生成pdf,但在客户端自动下载。我不希望它被下载,而是直接打印在客户端。

我不知道如何继续下去,但我认为pdf不应该写为'响应',而应该写成内存流并作为Adobe Reader的后台进程打印。怎么做?请分享一些代码。

+0

检查出来:)它可能会帮助你:) http://stackoverflow.com/questions/8294057/how-to-open-pdf-file-in-a-new-tab- or-window-instead-of-downloading-it-using-asp – Jonny

+0

由于PDF查看器需要磁盘上的文件才能呈现,因此无法阻止文档被下载。见http://stackoverflow.com/questions/22880444/disable-save-button-in-adobe-pdf-reader-and-hide-menu-bar-in-ie-window –

+1

你不能“强制打印”一个PDF文件下载到客户端,因为这被认为是一个安全隐患:http://stackoverflow.com/questions/26751910/print-pdf-created-using-itextsharp –

回答

0

仅从ASP.NET就无法触发它在客户端自动打印。您可以做的最好的是弹出打印对话框。也许这对你的用例足够了。请看这个答案:https://stackoverflow.com/a/9639241/94853

相关问题