Copy and paste data in multiple formats to the clipboard in C#

Copy and paste data in multiple formats to the clipboard

This example shows how to copy and paste data in multiple formats to the clipboard. It saves data in Rich Text (RTF), text, and HTML formats. When you paste the data, it separately displays each of those formats that are available.

The following code shows how the program copies data to the clipboard.

// Copy data to the clipboard in text, RTF, and HTML formats.
private void btnCopy_Click(object sender, EventArgs e)
{
    // Make a DataObject.
    DataObject data_object = new DataObject();

    // Add the data in various formats.
    data_object.SetData(DataFormats.Rtf, rchSource.Rtf);
    data_object.SetData(DataFormats.Text, rchSource.Text);

    string html_text;
    html_text = "\r\n";
    html_text += "  The Quick Brown Fox\r\n";
    html_text += "  \r\n";
    html_text += rchSource.Text + "\r\n";
    html_text += "  \r\n";
    html_text += "";
    data_object.SetData(DataFormats.Html, html_text);

    // Place the data in the Clipboard.
    Clipboard.SetDataObject(data_object);
}

First the code makes a new DataObject. It calls the object’s SetData method to save data in RTF and text formats. It then composes a small HTML document and uses the data object’s SetData method again to save the document in the HTML format.

The program then calls the Clipboard object’s SetDataObject method to save the DataObject on the Clipboard.

The following code shows how the program pastes data from the clipboard.

// Paste data from the clipboard in text,
// RTF, and HTML formats if they are available.
private void btnPaste_Click(object sender, EventArgs e)
{
    // Get the DataObject.
    IDataObject data_object = Clipboard.GetDataObject();

    if (data_object.GetDataPresent(DataFormats.Rtf))
    {
        rchRtf.Rtf =
            data_object.GetData(DataFormats.Rtf).ToString();
        txtRtfCode.Text =
            data_object.GetData(DataFormats.Rtf).ToString();
    }
    else
    {
        rchRtf.Clear();
        txtRtfCode.Clear();
    }

    if (data_object.GetDataPresent(DataFormats.Text))
    {
        txtText.Text =
            data_object.GetData(DataFormats.Text).ToString();
    }
    else
    {
        txtText.Clear();
    }

    if (data_object.GetDataPresent(DataFormats.Html))
    {
        txtHtml.Text =
            data_object.GetData(DataFormats.Html).ToString();
    }
    else
    {
        txtHtml.Clear();
    }
 	}

For each of the data RTF, text, and HTML data types, the code uses the Clipboard object’s GetDataPresent method to see if the format is available. If so, the code uses the Clipboard‘s GetData method to get the data.

Note that this method works no matter what program placed the data in the clipboard. For example, if you use Word to copy some text, you can then paste it into this program in RTF, text, and HTML formats. Try copying the initial text from this program into Word, and then copying it back from Word to this program. The result should be similar but slightly different from what you get when you copy from this program to itself.


Download Example   Follow me on Twitter   RSS feed




This entry was posted in clipboard and tagged , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Copy and paste data in multiple formats to the clipboard in C#

  1. Bob Lambert says:

    Does the HTML paste maintain the original sources formatting? I don’t see any tags to accommodate the various colors, bold, italicized and underline characters that are present in the source.

    • RodStephens says:

      What kinds of data are available is up to the application that performed the copy. For example, select some text in a browser and press Ctrl+C. Then click this example’s Paste button.

      You should see data in the HTML window. If the selected text includes different fonts and colors, you should see them in the HTML code. You should even see things link link tags.

  2. MFletcher says:

    Thank you for showing me what I needed to know.
    I just didn’t know how to combine the 2 formats into 1 object!!!
    By creating a dataobject, you can create multiple formats which allows you to create text which is used by word for the “Preview” in the clipboard viewer
    ie
    DataObject myDO = new DataObject();
    myDO.SetData(DataFormat.Text “whatever title you want to see in word here\r\n”);
    myDO.SetData(DataFormat.Html, mySerializableClass.stringWithHtml ) ;
    Clipboard.SetDataObject();
    where the serializable class has attribute [Serializable]
    and the string returned from the stringWithHtml has the
    Html Fragment format as specified on the web version 0.9

  3. Joe says:

    Thanks, exactly what I want!

Leave a Reply

Your email address will not be published. Required fields are marked *