简体   繁体   中英

Creating a PDF or 'print report' in ASP.net

Is there a way of creating a PDF document or 'print report' directly using ASP.net? So the user enters data in the textboxes, then clicks save, it gets saved to SQL and when they click view it previews. I want to add a print button to it, or an option for it to open in a 'print report' so they can print a hard copy. I am using visual studio and coding in C#, I read people saying install this or that, but I want to know is there a way of doing it without installing additional software?

You will need an external resource for pdf generation.

There is a complete solution called Crystal Reports for Visual Studio .

You can try to the following stpes to generate a pdf file based on the gridview.

First, we need to install the nuget package itextSharp .

Second, here is code example you can refer to.

protected void btnPrint_Click(object sender, EventArgs e)
    {
        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));

        doc.Open();//Open Document to write


        Paragraph paragraph = new Paragraph("data Exported From Gridview!");

        //Create table by setting table value
        PdfPTable table = new PdfPTable(3);
        DataTable dt = (DataTable)GridView1.DataSource;

        //Create Table Header
        table.AddCell("Name");
        table.AddCell("Age");
        table.AddCell("ID");


        foreach (GridViewRow rows in GridView1.Rows)
        {

            string Name = GridView1.Rows[rows.RowIndex].Cells[0].Text;
            string Age = GridView1.Rows[rows.RowIndex].Cells[1].Text;
            string ID= GridView1.Rows[rows.RowIndex].Cells[2].Text;
            //Create Cells
            table.AddCell(Name);
            table.AddCell(Age);
            table.AddCell(ID);

        }
        doc.Add(paragraph);
        doc.Add(table);
       
        doc.Close(); //Close document
                     //
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "success" + "');", true);
    }

PDF file: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM