views:

56

answers:

2

I am dropping a text snippet (from IE) from a web page, that might contain links too.

string _rtf = (string) data.GetData("Rich Text Format"); RichTextBox box = new RichTextBox(); box.DetectUrls = true; box.SelectedRtf =_rtf; box.SelectAll(); _rtf = box.SelectedRtf;

The problem is, when i save the content of the rich text box, the links will not remain, they are changed to normal text. How can i keep the links to remain functional and save the text to an RTF or DOC file?

I am using HandleDropEvent function for handling the dragdrop that looks like this:

protected override bool HandleDropEvent(DragEventArgs e) {

        bool result = false;

        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            System.Windows.Forms.IDataObject data = e.Data;
            if (data.GetDataPresent("Rich Text Format"))
            {

            string _rtf = (string) data.GetData("Rich Text Format");                 
            RichTextBox box = new RichTextBox();
            box.DetectUrls = true;
            box.Text = _rtf;
            box.SelectedRtf =_rtf;
            box.SelectAll();                
            _rtf = box.SelectedRtf;
            box.SaveFile("filename.rtf", RichTextBoxStreamType.RichText);
            result = true;
            }
        }

        return result;
    }
A: 

Assuming in your question you mean that your load code works and you see links, does the following save code not work for you?

box.SaveFile("filename.rtf", RichTextBoxStreamType.RichText);
Codesleuth
The problem is when saving like that the links don't remain functional. For ex if i drop a text "Force allocating real memory" that is also a link, it will be saved like "Force allocating real memory </questions/3211063/force-allocating-real-memory>", and they don't remain links anymore.
So it doesn't work? Ok, you may need to perform some pre-processing on the code `data.GetData("Rich Text Format")` to format that text correctly for Rich Text (I don't understand why it would show links but won't persist them)
Codesleuth
I can't actually get any of your code working - `data.GetData("Rich Text Format")` is returning null when I drop links.
Codesleuth
No it does not work correctly, the links are saved like : linkname< link address> in plain text. I need them to remain clickable links.
A: 

Solved it!! First i saved it as a html page then opened it by word programatically and saved as an rtf file. The Word component made the whole conversion.

protected override bool HandleDropEvent(DragEventArgs e) {

    bool result = false;

    if (e.Data.GetDataPresent(DataFormats.Text))
    {
        System.Windows.Forms.IDataObject data = e.Data;
        if (data.GetDataPresent("HTML Format"))
        {

        byte[] rawHtmlBytes = HtmlFromIDataObject.GetHtml(data);
        string _html = Encoding.UTF8.GetString(rawHtmlBytes);

        object vk_missing = System.Reflection.Missing.Value;
        //1.Save the html file (in %temp% folder) 
        string    file = IOUtil.CreateTextFile(_html, ".html");

        object oFile = file;
        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

        //2.open the html file
        Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref oFile, ref
                vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
                ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref  vk_missing,
                ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing);

        //3.saving as rtf
        string directory = Path.GetDirectoryName(file);
        string fileName = Path.GetFileNameWithoutExtension(file);
        file = Path.Combine(directory, fileName+ "_" + ".rtf");

        object oSave = file;
        doc.SaveAs(ref oSave, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing);

        result = true;
        }
    }

    return result;
}