Display filters on pdf

Last Reply 2 days ago By dharmendr

Posted 5 days ago

Hi,

based upon below url i have generated pdf

https://www.aspforums.net/Threads/135338/Downlaod-Pdf-by-using-dynamic-controls/

currently my requirement in this is

I am going to display dynamic filters on pdf

My excepted o/p

Filter b/w textboxes:EName=Mahesh ,From Date:12-12-2019 and Name:mahesh

Could You please help me

You are viewing reply posted by: dharmendr 4 days ago.
Posted 4 days ago

What you need to do is after getting DataTable, create new datatable with clone of the resulted datatable and filter the resulted datatable and insert the filtered record in final datatable and do the rest process what already done.

Example:

[HttpPost]
public ActionResult PopulateTable(Detail values)
{
    TestEntities db = new TestEntities();
    // Getting the query from Report1 table.
    string query = db.Report1.Where(x => x.RId == values.RId).Select(x => x.RQuery).FirstOrDefault();
    for (int i = 0; i < values.details.Count; i++)
    {
        string key = "{" + values.details[i].Text + "}";
        string value = "'" + values.details[i].Value + "'";
        // Replace the place holder with value.
        query = query.Replace(key, value);
    }

    // Execute your final query and return DataTable.
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    DataTable dtResult = new DataTable();
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            sda.Fill(dtResult);
        }
    }

    DataTable dt = dtResult.Clone();
    for (int i = 0; i < values.details.Count; i++)
    {
        Details d = values.details[i];
        DataRow[] drs = dtResult.Select("=" + d.FromTable);
        foreach (DataRow dr in drs)
        {
            dt.ImportRow(dr);
        }
    }

    // Generating html table.
    string html = "<table border='1' width='100%'><tr>";
    for (int k = 0; k < dt.Columns.Count; k++)
    {
        html += "<th>" + dt.Columns[k].ColumnName + "</th>";
    }
    html += "</tr>";
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        html += "<tr>";
        for (int k = 0; k < dt.Columns.Count; k++)
        {
            html += "<td>" + dt.Rows[i][k].ToString().Trim() + "</td>";
        }
        html += "</tr>";
    }
    html += "</table>";
    // Converting html table to byte array.
    using (MemoryStream stream = new MemoryStream())
    {
        StringReader sr = new StringReader(html);
        Document pdfDoc = new Document(PageSize.B2, 5f, 5f, 10f, 5f);

        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
        pdfDoc.Open();
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
        pdfDoc.Close();
        // Save byte array in TempData.
        TempData["Data"] = stream.ToArray();
    }
    TempData["FileName"] = "Report.pdf";

    return new JsonResult() { Data = new { FileName = "Report.pdf" } };
}