按钮点击生成pdf报告

时间:2010-09-10 20:41:19

标签: c# .net asp.net

我只想点击按钮,查看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

}

但效果不佳

2 个答案:

答案 0 :(得分:2)

您点击按钮只是在内存中创建文档,写入文档并关闭它。

您需要将文档输出到Response.Output流。

答案 1 :(得分: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();
    }
}

你缺少的主要是PdfWriter将文档写入Response.OutputStream