0

I am developing an ASP.NET web form application, in which I have to generate pdf and print it on client side. My friend gave this code:

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

This code generates pdf but downloads automatically on client side. I don't want it to get downloaded but to be printed on client's side directly.

I don't know how to proceed further, but I think the pdf should not be written as 'Response' but as memory stream and printed as a background process of adobe reader. How to do it? Please share some code.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Manoj Maximum
  • 43
  • 2
  • 8
  • Check this out :) It may help you :) http://stackoverflow.com/questions/8294057/how-to-open-pdf-file-in-a-new-tab-or-window-instead-of-downloading-it-using-asp – Jonny Jan 08 '15 at 07:33
  • You can not prevent that a document is downloaded because a PDF viewer needs the file on disk in order to render it. See http://stackoverflow.com/questions/22880444/disable-save-button-in-adobe-pdf-reader-and-hide-menu-bar-in-ie-window – Bruno Lowagie Jan 08 '15 at 08:43
  • 1
    You can not "force-print" a PDF document that is downloaded to a client, because that is considered being a security hazard: http://stackoverflow.com/questions/26751910/print-pdf-created-using-itextsharp – Bruno Lowagie Jan 08 '15 at 08:45

1 Answers1

0

From ASP.NET alone there is no way to trigger it to automatically print on the client side. The best you'll be able to do is pop up the print dialog. Perhaps that's good enough for your use case. Please see this answer: https://stackoverflow.com/a/9639241/94853

Community
  • 1
  • 1
Loren Paulsen
  • 8,960
  • 1
  • 28
  • 38