0

In my Aspx page I have two buttons, btnGenerateReceipt is for generating receipt and btnAddNew for adding new receord. OnClick event of btnGenerateReceipt I am generating and opening a receipt as below.

 protected void onGenerateReceipt(object sender, EventArgs e)
    {
          try
            {
                    byte[] document = receiptByte;
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.Buffer = true;
                    Response.ContentType = "application/vnd.ms-word";
                    Response.AddHeader("content-disposition", "inline;filename=" + "Receipt" + ".doc");
                    Response.Charset = "";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BinaryWrite(document);
                    //Response.Flush();
                }

            }
            catch (Exception ex)
            {

            }
            finally
            {
                //Response.End();
            }
        }
    }

This opens Open/Save/Cancel dialog box, followings are my problems,

  1. I need the word document to open automatically without the dialog box.
  2. My second button's click function doesn't fire after I click btnGenerateReceipt button.
  3. How can I generate&Open PDF file instead of word Doc?

Any idea?

vml19
  • 3,816
  • 11
  • 45
  • 63
  • 1
    1. You're writing server side code... where do you want to open the document? I can't see any code for that task. Please improve your sample. – Beachwalker Apr 16 '12 at 09:19
  • 2. >My second button's click function doesn't fire after I click btnGenerateReceipt button.< Where is this code? – Beachwalker Apr 16 '12 at 09:20
  • 3
    Weather to open or just to save (with or without the dialog box) is the preferences of the user (browser) and you cannot force open it. IMHO. – Oybek Apr 16 '12 at 09:20
  • 3. Do you (want) to use any external libs to produce/generate the pdf? – Beachwalker Apr 16 '12 at 09:21
  • 1
    you can't open a word doc in the browser (unless the browser it self is capable of it) so your user will have to download the file to view it. Depending on the browser and the users setup this might open the document in word directly when downloading is completed. Do you wish to display information to the user (Why is the word document then important?) or do you wish the user to be able to download the document? – Rune FS Apr 16 '12 at 09:21
  • As far as file type is concerned, you need some library that creates pdf document that suits to you. Then as usual you take the bytes of this pdf, add relevant content type and flush it. – Oybek Apr 16 '12 at 09:22
  • @oybek how's the _weather_ at your place? do you care _whether_ or not the sun is shining? – Rune FS Apr 16 '12 at 09:23
  • Thanks @RuneFS and Oybek for your replies. Oybek - Is there any libraries available in C#.Net 3.5? – vml19 Apr 16 '12 at 09:26
  • 4. Look for infos about client-server-architecture and differences between your service environment (server) and browser (client-) capabilities. Describe the workflow precisely in your question, e.g. server->transfer document to client->client opens/displays document. – Beachwalker Apr 16 '12 at 09:26
  • Here you go. [ITextSharp](http://sourceforge.net/projects/itextsharp/) – Oybek Apr 16 '12 at 09:27
  • @Roshe can you try Response.AddHeader("content-disposition", "inline"); to check if it displays the document inside browser provided browser is supports it (like IE which can embed docs inside the window) – Ramesh Apr 16 '12 at 09:39
  • I tried this, Error -The XML page cannot be displayed Does not display the document. – vml19 Apr 16 '12 at 10:05

2 Answers2

4

The content sent by the server is handled by the web browser. You can not control from server side code whether the browser opens, saves or asks the user by default, as this is a browser setting.

EDIT
As for the second question about generating a PDF: There are many libraries out there to generate PDFs. However, if you already have the Word file ready, one solution would be to print the Word document to a PDF printer and send the resulting PDF.

Printing the document can be achieved using ShellExecute or the Process class with the verb print, then you could use a PDF printer like PDF-Creator or Bullzip to generate a PDF file.

This is what I'd try instead of "manually" generating the PDF file.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Thanks, Do you have any idea for my second question? – vml19 Apr 16 '12 at 09:33
  • To an extent you would be able to control how the message is displayed. Content-Disposition if set to inline will display inline and if set to attachment will prompt save as dialog - "http://en.wikipedia.org/wiki/MIME#Content-Disposition". But it is not implemented consistently across all browsers. – Ramesh Apr 16 '12 at 09:34
  • Hi Ramesh - Thanks, My second question is about my second button on the same page doesn't fire after I open the document successfully by clicking my fist button, would you be able to suggest any solution for this problem? – vml19 Apr 16 '12 at 09:38
1

I need the word document to open automatically without the dialog box.

For the answer of the go with @Thorsten Dittmar answer.

My second button's click function doesn't fire after I click btnGenerateReceipt button.

Asp.net uses stateless connection, so do you think your written contents will remain in memory. i think it should not work as per my understanding. create response content and then write it to response and flush it.

How can I generate&Open PDF file instead of word Doc?

To generate pdf reference this. use iTextSharp like library to generate pdf then export/ save them as pdf.

Ref: ASP.NET 4: HttpResponse open in NEW Browser?

Response.AppendHeader("Content-Disposition", "inline; filename=foo.pdf");

You need to set the Content Type of the Response object and add the binary form of the pdf in the header. See this post for details: Ref: Opening a PDF File from Asp.net page

private void ReadPdfFile()
    {
        string path = @"C:\Swift3D.pdf";
        WebClient client = new WebClient();
        Byte[] buffer =  client.DownloadData(path);

        if (buffer != null)
        {
            Response.ContentType = "application/pdf"; 
            Response.AddHeader("content-length",buffer.Length.ToString()); 
            Response.BinaryWrite(buffer); 
        }

    }

Ref Links:
ASP.NET 4: HttpResponse open in NEW Browser?
Generate pdf file after retrieving the information
ASP.NET MVC: How can I get the browser to open and display a PDF instead of displaying a download prompt?

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • Thanks @Niranjan - I think with this method I can not implement second button. Is that so? – vml19 Apr 16 '12 at 10:07
  • yes.. your first button generate post back and then your another button generate postback. as i think it should not contain the data between postback of another button. put these on same button. – Niranjan Singh Apr 16 '12 at 10:11
  • Actually I have 3 buttons in a row. there is another one for different implementation so cant have all in one button click. Thanks. – vml19 Apr 16 '12 at 10:17