Itextsharp download PDF

2019-03-06 12:12发布

This code work perfect. The problem was the button was inside a control update panel and that cause some problem. But i already fixed and now works.

 Dim documentoPDF As New Document(PageSize.A4, 10, 10, 10, 10)
        Dim memStream As MemoryStream = New MemoryStream()
        Dim writer As PdfWriter = PdfWriter.GetInstance(documentoPDF, memStream)
        documentoPDF.Open()

        Dim Paragraph1 As Paragraph = New Paragraph("First paragraph")
        documentoPDF.Add(New Paragraph(Paragraph1))

        documentoPDF.Close()

        Dim bytesInStream As Byte() = memStream.ToArray()
        memStream.Close()

        'Dim memorystreaam As MemoryStream = New MemoryStream(bytesInStream)

        Response.Clear()
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf")
        Response.Buffer = True
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        'Response.OutputStream.Write(memStream.GetBuffer(), 0, memStream.GetBuffer().Length)

        Response.BinaryWrite(bytesInStream)
        Response.End()

1条回答
走好不送
2楼-- · 2019-03-06 12:32

It looks like you are not saving the PDF document into the stream somewhere.

Dim bin() As Byte
Dim stream As MemoryStream = New MemoryStream
documentoPDF.Save(stream, false)
bin = stream.ToArray

Response.ClearHeaders
Response.Clear
Response.Buffer = true
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", bin.Length.ToString)
Response.AddHeader("content-disposition", "attachment; filename="""" + txtnombre.Text + ".pdf, "")
Response.OutputStream.Write(bin, 0, bin.Length)
查看更多
登录 后发表回答