简体   繁体   中英

Save Background Color when saving RichTextBox to rtf or html file?

I am currently working on trying to save text from a richtextbox to a RTF file. I have it working so i can save the text to rtf file with colored text. However, I would like to preserve the background color of the box too in the document for viewing.

I am open to either saving the data into rtf or html as long as i can preserve all the color coded text, and display the appropriate back ground color in the document.

Below is the code i use to save as an RTF file with color coded text.

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFile1 = new SaveFileDialog();

    // Initialize the SaveFileDialog to specify the RTF extension for the file.
    saveFile1.DefaultExt = "*.rtf";
    saveFile1.Filter = "RTF Files|*.rtf";

    // Determine if the user selected a file name from the saveFileDialog. 
    if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveFile1.FileName.Length > 0)
    {
        // Save the contents of the RichTextBox into the file.
        richtextbox.SaveFile(saveFile1.FileName, RichTextBoxStreamType.RichText);
    }
}

Edit: I found out the following code can highlight the text with my background into an RTF file. For the moment this works, but I would still want to get opinions on better solutions.

Place below code before Savefile dialog open statement.

        richtextbox.SelectAll();
        richtextbox.SelectionBackColor = richtextbox.BackColor;
        richtextbox.DeselectAll();

My solution thus far has been to do the following before the Savefile dialog:

    richtextbox.SelectAll();
    richtextbox.SelectionBackColor = richtextbox.BackColor;
    richtextbox.DeselectAll();

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