1
Vote

OutOfMemoryException While Creating PDF

description

Hi,
I am using this code from last few months and it always throws an OutOfMemoryException.

Can you please tell me why this error occurring?
  Private _BufferSize As Integer = 90000000
    Private _Margin As Integer = 54
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        If context.Request.ContentLength > 0 Then
            Dim incomingStream As System.IO.Stream = context.Request.InputStream
            'Get the incoming stream
            Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(incomingStream)
            'Create and image from the stream
            Dim pageHeight As Integer = img.Height + (_Margin * 2)
            Dim pageWidth As Integer = img.Width + (_Margin * 2)

            Dim pdfImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(img, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim pdfStream As New System.IO.MemoryStream()
           
            Dim pdfDoc As New iTextSharp.text.Document(New iTextSharp.text.Rectangle(pageWidth, pageHeight))
            Dim pdfWriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, pdfStream)

            'Insure this is set otherwise when the close method is called on the iTextSharp.text.Document the memory stream
            'we are writting to will be closed
            pdfWriter.CloseStream = False

            'Open PDF and add image to it
            pdfDoc.Open()
            pdfDoc.Add(pdfImage)

            'Close the PDF document to commit the changes
            pdfDoc.Close()

            'Set response headers
            context.Response.Clear()
            context.Response.ContentType = "application/pdf"
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=Export.pdf")
            context.Response.AppendHeader("Content-Length", pdfStream.Length.ToString())
            context.Response.Buffer = False

            'Insure the stream is at the beginning
            If pdfStream.Position > 0 Then
                pdfStream.Position = 0
            End If

            'Stream the exported pdf to the client this is needed since the file size could be large in size
            Dim bytesRead As [Byte]() = New [Byte](_BufferSize - 1) {}

            Dim toRead As Long = pdfStream.Length
            'Var used to determine how much to read
            While toRead > 0 AndAlso context.Response.IsClientConnected
                'Insure that _BufferSize do not overrun the end of the stream
                If _BufferSize > toRead Then
                    _BufferSize = CInt(toRead)
                    bytesRead = New [Byte](_BufferSize - 1) {}
                End If
                
                'Write pdf memory stream to bytesRead
                pdfStream.Read(bytesRead, 0, _BufferSize)

                'Read read bytes to outputStream
                context.Response.OutputStream.Write(bytesRead, 0, _BufferSize)

                'Flush response
                context.Response.Flush()

                'Create new byte array to store bytes to read
                bytesRead = New [Byte](_BufferSize - 1) {}

                'Update value used to check for end of stream
                toRead -= _BufferSize
            End While

            'Flush and close the stream
            context.Response.OutputStream.Flush()
            context.Response.[End]()

            'Close and displose of the memory stream
            If pdfStream IsNot Nothing Then
                pdfStream.Close()
                pdfStream.Dispose()
            End If
        End If
    End Sub
Thanks

comments