2010-09-10 58 views
1

我只想在按鈕上點擊查看PDF格式的所有報告。 我使用...在按鈕上點擊生成pdf報告

protected void Button1_Click(object sender, EventArgs e) 
{ 
    //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin 
    Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); 
    //Write some content 
    Paragraph paragraph = new Paragraph("This is my first line using Paragraph."); 
    Phrase pharse = new Phrase("This is my second line using Pharse."); 
    Chunk chunk = new Chunk(" This is my third line using Chunk."); 
    // Now add the above created text using different class object to our pdf document 

    doc.Add(paragraph); 

    doc.Add(pharse); 

    doc.Add(chunk); 
    doc.Close(); //Close document 

} 

但不能有效

+0

什麼是你的問題PdfWriter? – Oded 2010-09-10 20:44:28

+0

我想你必須將報告添加到響應流中... – 2010-09-10 20:46:04

回答

2

你按一下按鈕僅僅是在內存中創建一個文件,寫它,並關閉它。

您需要將文檔輸出到Response.Output流。

1

這個工作對我來說:

protected void PrintButton_Click(object sender, EventArgs e) 
{ 
    if (!Page.IsValid) return; 
    Response.ContentType = "application/pdf"; 
    using (var document = new Document()) 
    { 
     PdfWriter.GetInstance(document, Response.OutputStream); 
     document.Open(); 
     document.Add(new Paragraph("Hello PDF!")); 
     document.Close(); 
    } 
} 

,你是缺少的最主要的是該文件寫入Response.OutputStream