I have a rtf column on my grid view and want to convert that column in plain text and show it to grid wiew

960 views Asked by At

I have a rtf column in my sql server database and I want to convert it to plain text and so I can show as plain text in my grid view column. than I am converting grid view to word. I am stuck with converting rtf column to plain text

code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes; 
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
using Itenso.Rtf.Converter.Text;
using Itenso.Rtf.Support;
  public partial class WebForm1 : System.Web.UI.Page{
    protected void Page_Load(object sender, EventArgs e)
        {
            string strQuery = "select * from customers";
            SqlCommand cmd = new SqlCommand(strQuery);
            DataTable dt = GetData(cmd);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        private DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
        }
 protected void btnExportWord_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-word ";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            GridView1.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
}
1

There are 1 answers

5
Jagadeesh Govindaraj On
public string getplaintext(string rtftext)
     {  
        string plainText="";
       //Create the RichTextBox. (Requires a reference   to System.Windows.Forms.dll.)
       using(System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox());
       {
             // Convert the RTF to plain text.
             rtBox.Rtf = rtftext;
             plainText = rtBox.Text;

           // Now just remove the new line constants
           plainText = plainText.Replace("\r\n", ",");

          // Output plain text to file, encoded as UTF-8.

         }
          return plainText;
      }

Usage

var value=getplaintext(dt[0][1].toString());