How to manipulate RTF Document in C#
1. how to read and save RTF Document in C#?
No.1 Read RTF to byte[] object
/// <summary>
/// ◆将RichTextBox的rtf文本转化为Byte[]
/// </summary>
/// <param name="richTextBox">▼富文本控件</param>
/// <returns>◇二进制数组</returns>
public static Byte[] GetBytes(RichTextBox richTextBox)
{
MemoryStream ms = new MemoryStream( );
try
{
richTextBox.SaveFile( ms, RichTextBoxStreamType.RichText );
int size = Convert.ToInt32( ms.Length );
Byte[] rtf = new Byte[size];
ms.Position=0;
ms.Read(rtf, 0, size);
return rtf;
}
finally
{
ms.Close();
ms = null;
}
}
No.2 Read byte[] object to RTF
/// <summary>
/// ◆将从数据库读出来的二进制文本对象显示在RichTextBox中
/// </summary>
/// <param name="obj">▼数据库中的二进制文本对象</param>
/// <param name="rtb">▼富文本控件</param>
public static void GetText(object obj, RichTextBox rtb )
{
if( Convert.IsDBNull(obj) )
{
rtb.Text = "";
}
else
{
MemoryStream buf = new MemoryStream( ( byte[] )obj );
try
{
Byte[] rtf = (byte[])obj;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
rtb.Rtf = encoding.GetString( rtf, 0, Convert.ToInt32(buf.Length) );
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
buf.Close();
buf = null;
}
}
}
Via above code, you can Write RTF document to Database and Read blob or clob object to RTF from Database. But the problem is that it’ ll take you much time when the RTF Document is too large. So if you find a better method, please send email to me, my email is juejue1984@hotmail.com. Thank you!
2. How to read and save Images in a Sql Server Database using ADO.NET and C#
No.1 Read
public bool ReadFlashCard(int x)
{
try
{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
}
pictureBox1.Image = null;
// form a query that will only select the flashcard we are interested in
sqlDataAdapter1.SelectCommand.CommandText = "Select * From Flashcards Where PrimaryKey = "
+ Convert.ToString(x);
// Fill the dataset using the query from the adapter
DataSet ds = new DataSet();
sqlDataAdapter1.Fill(ds, "Flashcards");
// if no row is returned it means we reached the end or something is wrong
if (ds.Tables[0].Rows.Count == 0) return false;
// populate the hidden answers: the language word and the pronunciation
label1.Text = ds.Tables[0].Rows[0]["LearnLanguage"].ToString();
label2.Text = ds.Tables[0].Rows[0]["Pronunciation"].ToString();
// now we need to get the picture from the DataRow
// and assign it to a byte array
byte[] MyData = null;
MyData = (byte[])ds.Tables[0].Rows[0]["Picture"];
// After retrieving the byte array, we want to get the
// number of elements of the array for use in writing the array
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
// Create a Filestream for writing the byte array to a gif file (the original format in this case)
FileStream fs = new FileStream("tmp.gif", FileMode.OpenOrCreate, FileAccess.Write);
// Write the stream of data that we read in from the database to the filestream and close it.
// This will create the file tmp.gif, which we can use to display in the picturebox
fs.Write(MyData, 0,ArraySize+1); // don't ask me why, but I had to add 1 here for this to work
// Assign the temporary gif file to the picture box
pictureBox1.Image = new Bitmap("tmp.gif");
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return false;
}
Finally
{
fs.Close();
fs = null;
}
}
No.2 Write
try
{
// Fill a DataSet with existing Flashcards
DataSet ds = new DataSet();
sqlDataAdapter1.Fill(ds, "Flashcards");
DataTable TheTable = ds.Tables[0];
// Create a new row for the FlashCard Table in Memory
DataRow aRow = TheTable.NewRow();
// Insert the information from the dialog into the Flashcard Table
// Such as the Primary Key, Language Word, Translation, and Pronunciation
aRow["PrimaryKey"] = TheTable.Rows.Count + 1;
aRow["LearnLanguage"] = textBox1.Text;
aRow["FirstLanguage"] = textBox2.Text;
aRow["Pronunciation"] = textBox3.Text;
// Create a new FileStream for reading data from the image file in which the PictureBox populated
// Its data from (the FileName is maintained after the picture button is pressed and a picture is selected).
FileStream fs = new FileStream (FileName, FileMode.OpenOrCreate, FileAccess.Read);
// Read the Data into the Byte Array
byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, (int)fs.Length);
fs.Close(); // maybe this sentence has been lied in finally clause
// Assign the DataRow Picture Column to the Byte Array to populate it in the DataRow
aRow["Picture"] = MyData;
// Add the DataRow to the DataTable
TheTable.Rows.Add(aRow);
// Write all the data (including the picture) to the Flashcards Sql Server database
sqlDataAdapter1.Update(ds, "Flashcards");
MessageBox.Show("Flashcard Added.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
3. How to print RTF Document in C#?
To find a solution. Please visit: http://blog.youkuaiyun.com/juejue1984/archive/2007/04/17/1567985.aspx