2016-09-28 295 views
0

此代碼工作完美。問題在於該按鈕位於控制更新面板內,並導致一些問題。但我已經修好了,現在可以工作了。Itextsharp下載PDF

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

您的代碼無法使用哪種方式?即預期的行爲是什麼以及觀察到的行爲是什麼? – mkl

+0

@mkl看,我實現了代碼,問題是pdf永遠不會下載到我的電腦。 –

+2

當您嘗試下載時,究竟會發生什麼?您是否檢查過所有相關的日誌文件?你有沒有在調試器中運行代碼?您是否使用了tcpdump工具來捕獲和分析Web流量? – mkl

回答

1

它看起來好像不是將PDF文檔保存到某個地方的流中。

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) 
+0

:(沒有工作 documentoPDF是文檔,沒有.save功能 我把memorystream的參數放在pdfwriter裏面,所以不需要保存它。我沒有在調試時出現錯誤,但是頁面看起來像什麼都沒有發生。我替換了inicial代碼和我將在其他文章中發佈 Dim memStream As MemoryStream = New MemoryStream() Dim writer As PdfWriter = PdfWriter.GetInstance(documentoPDF,memStream) –