0

I need to open pdf in the new tab, it works and file show perfect, but if I open File with notepadd++, after EOF there are some NULL char (see pics)enter image description here. It happen only I open it in new tab and use memorystream, the string after EOF create some problem the parser of client, whats wrong?.

This is code:

 Dim mswithPage As New MemoryStream()
        Dim SessValue As String = Request.QueryString("s")
        Dim NOrder As String = Request.QueryString("odv")
        mswithPage = CType(Session(SessValue), MemoryStream)


        Response.Clear()
        Response.ContentType = "Application/pdf"
        Response.AddHeader("content-disposition", "inline;filename=" & NOrder & ".pdf")

        Response.OutputStream.Write(mswithPage.GetBuffer(), 0, mswithPage.GetBuffer().Length)
        Response.OutputStream.Flush()
        Response.OutputStream.Close()
        Response.End()
user3699954
  • 31
  • 1
  • 4

1 Answers1

0

The issue

An issue is in this line:

 Response.OutputStream.Write(mswithPage.GetBuffer(), 0, mswithPage.GetBuffer().Length)

Even more precisely its final argument mswithPage.GetBuffer().Length - you should use the number of actually used bytes in the buffer but you use the size of the complete buffer.

A solution

Thus, use mswithPage.Length instead:

 Response.OutputStream.Write(mswithPage.GetBuffer(), 0, mswithPage.Length)

... and if the MemoryStream already is closed

If the MemoryStream already is closed, the solution above doesn't work anymore because its Length property can only be used on open streams.

What does work on closed streams, though, is the ToArray method! Thus, you can instead use

 Response.OutputStream.Write(mswithPage.ToArray())

(Actually it is funny that ToArray works on closed streams but Length does not. After all, ToArray essentially returns a copy of the first Length many bytes of the internal buffer...)

mkl
  • 90,588
  • 15
  • 125
  • 265
  • I got an exception "Cannot access a closed Stream" – user3699954 Jun 25 '19 at 06:40
  • Then *don't close the `MemoryStream` before retrieving its contents!* (As you have tagged your question [tag:itext], you might want to change the `writer.CloseStream` property of your `PdfWriter` or `PdfStamper.Writer`, see [this answer](https://stackoverflow.com/a/1196125/1729265).) – mkl Jun 25 '19 at 07:00
  • I use this page to open new tab pdf, and As you can see I pass to this page only memorystream, and I havent wirter. – user3699954 Jun 25 '19 at 07:36
  • Then why the [tag:itext] tag if you don't use iText? Nonetheless, if you get "Cannot access a closed Stream", then you have a closed `MemoryStream` which is inappropriate if you want to access the internal buffer of the stream and the used buffer size. If you are not bound to use the internal buffer, though, use `ToArray` instead. – mkl Jun 25 '19 at 08:17
  • You right, its my mistake isnt itext but itextsharp, I correct right now. So I try to resume: I create pdf with itextsharp, then I convert pdfdoc in memorystream, pass memorystream as session variable to other page and at the end I Show it in new tab with code you can see above. So what you suggest to use ToArray can you provide some code, please. – user3699954 Jun 25 '19 at 08:25
  • *"isnt itext but itextsharp"* - *iTextSharp* is the old name for *iText for .Net*; thus, the tags are synonyms. *"then I convert pdfdoc in memorystream"* - most likely it is here that you implicitly close the `MemoryStream` object. See the comment above for ways to prevent this. *"So what you suggest to use ToArray"* - see my edited answer. – mkl Jun 25 '19 at 08:41
  • Response.OutputStream.Write(mswithPage.ToArray(), 0, mswithPage.ToArray().Length) It works!! – user3699954 Jun 25 '19 at 10:05