Thanks, I tried your code, I had some problems
1. Sometimes, I got "file not exists" error
2. The "file download" messagebox apperaed twice.
3. I tried to put html string as input of StreamNewPDF, the pdf only display html, what I want to do is exporting an HTML table (runat=server) to pdf.
Thanks,
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim _strWtr As New StringWriter
Dim _htmlWtr As New HtmlTextWriter(_strWtr)
Me.tblSIRView.RenderControl(_htmlWtr)
'tblSIRView is HTML table (runat=server)
StreamNewPDF(_strWtr.ToString)
End Sub
Private Sub StreamNewPDF(ByVal paragraphText As String)
Try
'stream to the server's memory browser.
Dim _pdfStream As MemoryStream = New MemoryStream
Dim pdfDoc As New Document(PageSize.A4)
PdfWriter.getInstance(pdfDoc, _pdfStream)
' Now, create the PDF
pdfDoc.Open()
pdfDoc.Add(New Paragraph(paragraphText))
pdfDoc.Close()
'Finalize the stream
_pdfStream.Flush()
_pdfStream.Close()
'Stream the result to the user's browser
'1. convert the memorystream into an array of bytes
Dim byteArray As Byte() = _pdfStream.ToArray()
'2. clear all content buffered in the Response
Response.Clear()
'3. add the necessary headers
Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf")
Response.AddHeader("Content-Length", byteArray.Length.ToString())
'4. set the MIME type
Response.ContentType = "application/pdf"
'5. Finally, send the PDF data to the user's browser
Response.BinaryWrite(byteArray)
Catch ex As Exception
Dim s As String = ex.Message
'TODO: handle error here..
End Try
End Sub
Richard XinMCAD(charter member),MCDBA,MCSD
Web Site:
www.richardxin.com (For ASP.Net tips and Code snippet)