0
/// <summary>
/// generate the .pdf
/// </summary>
/// <param name="path">path of the document</param>
/// <param name="fileName">name of the .pdf documen</param>
/// <param name="download">is this downloadable</param>
/// <param name="text">text to place in the .pdf</param>
private void GeneratePDF(string path, string fileName, bool download, string text)
{
    var document = new Document();
    try
    {
        if (download)
        {
            PdfWriter.GetInstance(document, Response.OutputStream);
        }
        else
        {
            PdfWriter.GetInstance(document, new FileStream(path + fileName, FileMode.Create));
        }

        // generates the grid first
        StringBuilder strB = new StringBuilder();
        document.Open();

        if (text.Length.Equals(0)) // export the text
        {
            BindMyGrid();
            using (StringWriter sWriter = new StringWriter(strB))
            {
                using (HtmlTextWriter htWriter = new HtmlTextWriter(sWriter))
                {
                    GridView1.RenderControl(htWriter);
                }
            }                
        }
        else // export the grid
        {
            strB.Append(text);
        }

        // now read the Grid html one by one and add into the document object
        using (TextReader sReader = new StringReader(strB.ToString()))
        {



            // #### error on the next line    
            List<IElement> list =HTMLWorker.ParseToList( sReader, new StyleSheet());**


            //iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sReader, null)
            foreach (IElement elm in list)
            {
                document.Add(elm);
            }
        }
    }
    catch (Exception ee)
    {
        lblMessage.Text = ee.ToString();
    }
    finally
    {
        document.Close();
    }
}

/// <summary>
/// Binds my grid.
/// </summary>
private void BindMyGrid()
{
    // sql for paging. In production write this in the Stored Procedure
    string sql = "SELECT * FROM ( " +
        " Select SampleForTutorials.*, ROW_NUMBER() OVER (ORDER BY AutoId DESC) as RowNum " +
        " FROM SampleForTutorials) as AddressList " +
        " WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex + @pageSize) - 1 " +
        "ORDER BY AutoId DESC";


    DataTable table = new DataTable();
    int totalCount = 0;

    // get the data now
    using (OleDbConnection conn = new OleDbConnection(_connStr))
    {
        using (OleDbCommand cmd = new OleDbCommand(sql, conn))
        {
            cmd.CommandType = CommandType.Text;
            OleDbParameter p = new OleDbParameter("@startRowIndex", SqlDbType.Int);
            p.Value = _startRowIndex + 1;
            cmd.Parameters.Add(p);
            p = new OleDbParameter("@pageSize", SqlDbType.Int);
            p.Value = _pageSize;
            cmd.Parameters.Add(p);

            conn.Open();
            // get the data first
            using (OleDbDataAdapter ad = new OleDbDataAdapter(cmd))
            {
                ad.Fill(table);
            }

            // get the total count of the records now
            sql = "select count(AutoId) from SampleForTutorials";
            cmd.Parameters.Clear();
            cmd.CommandText = sql;
            object obj = cmd.ExecuteScalar();
            totalCount = Convert.ToInt32(obj);

            conn.Close();
        }
    }

    // bind the data to the grid
    GridView1.DataSource = table;
    GridView1.DataBind();

}

我在指示的行上遇到错误。我该如何解决这个问题?我想从动态绑定的 gridview 和 iframe 中的视图创建动态 PDF。

4

1 回答 1

1

您需要将列表转换为泛型类型。使用 .Cast().ToList()

于 2013-07-11T14:49:47.320 回答