开发者

generate pdf report on button click

开发者 https://www.devze.com 2023-01-15 10:57 出处:网络
ijust want to see all the report on a pdf format on button click. i use ... protected void Button1_Click(object sender, EventArgs e)

i just want to see all the report on a pdf format on button click. i use ...

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

}

but not effective开发者_如何学运维


Your button click is simply creating a document in memory, writing to it and closing it.

You need to output the document to the Response.Output stream.


This works for me:

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();
    }
}

The main thing that you were missing was the PdfWriter which writes the document to the Response.OutputStream

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号