0

I have pdf file in one URL , and what i'm looking for is to create one button in silverlight when I click it downloads this PDF file

The PDF's url download this file (if we use Browser) witch is generated in ASP.net and here the code :

        var ms = new MemoryStream();

        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(new Paragraph("Welcome to dotnetfox"));

        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(new Uri(url));
        pdfDoc.Add(jpg);
        pdfDoc.Close();

        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("content-disposition", "attachment;" +
                                       "filename=demo.pdf");
        Response.Buffer = true;

        Response.Clear();
        ms.Position = 0;
        var bytes = ms.ToArray();
        Response.OutputStream.Write(bytes, 0, bytes.Length);

        Response.OutputStream.Flush();
VIPUL PARMAR
  • 280
  • 2
  • 4
  • 29
mostafaGIS
  • 11
  • 3
  • You are not doing anything with the `MemoryStream`, then you are getting the 0 bytes of this empty memory stream and sending them to the response, you are also just throwing away all the PDF stuff you are doing. – Ben Robinson Dec 22 '14 at 12:17

1 Answers1

0

You are making a similar mistake as the person who posted this question: How to convert a pdf generating in response.outputStream to a Base64 encoding

You first tell iTextSharp to write PDF syntax straight to the Outputstream of the Response:

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

This Outputstream is automatically closed in this line:

pdfDoc.Close();

Everything that follows this Close line happens too late:

  • You can not set headers after content has been written to the Response.OutputStream.
  • You can no add extra content after the Response.OutputStream has been closed.

Moreover, you are converting an object ms to a byte array, but we don't see you adding any bytes to this MemoryStream anywhere.

Finally, the question is badly phrased.

  • When you say I have PDF file in one URL, people will assume that you have a static PDF on a file system that can be reached using an URL. Your code sample contradicts this. In your code sample, you create a PDF on the fly in a web application. This is not the same as having a PDF in one URL.
  • You say that you want a button in Silverlight that downloads the PDF when somebody clicks it, but I can't see any code that provides this button anywhere. My guess is that this part of your question is irrelevant. You want to create a PDF in a web application. It probably doesn't matter to you if you get this PDF from the application server using a browser or a button in Silverlight. If this assumption is wrong, then you are providing the wrong code sample. It is wrong because it can never work (for the reasons explained in my answer) and it is wrong because it is irrelevant if the actual problem is the button, not the PDF. If you want to get a document from an URL in Silverlight, then you shouldn't tag your question as a PDF or an iTextSharp question. Instead you should focus on the essence of your question: How to create a button to fetch a document from a server?
Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165