WPF Core Clipboard question

Rob Warning 1 Reputation point
2020-10-04T10:30:12.697+00:00

Hi, for weeks I try to transform a ms Access data table with a so called rich text field (witch is actually HTML ) to a rtf format that can be used in a SQLdatabase table and edited in a WPF richtextbox.

I can do it manually by loading my rich text string in a web browser control, copy its content into a Richtextbox, and finaly save the content of the richtextbox.

My Question is now, how to automate this browser Copy -> richtextbox Paste process.

I am trying to use the Clipboard component in WPF and came to this:

public void Convert2()
{
WebBrowser wb = new WebBrowser();
wb.NavigateToString(teststring);
Clipboard.Clear();
Clipboard.SetDataObject(wb);
Clipboard.SetData(DataFormats.Text, (object)wb.Document);
Clipboard.SetDataObject(myrtb);
Clipboard.GetData(DataFormats.Text);
}
(note: myrtb is a rich text box control )

It runs but nothing is copied.

Is there anybody who knows how this WPF CORE clipboard is supposed to work?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Andy ONeill 361 Reputation points
    2020-10-04T11:21:27.527+00:00

    I suggest you try the code here:

    https://stackoverflow.com/questions/5922626/c-sharp-net-converting-html-to-rtf

    string html = "...."; // html content
    RichTextBox rtbTemp = new RichTextBox();
    WebBrowser wb = new WebBrowser();
    wb.Navigate("about:blank");

    wb.Document.Write(html);
    wb.Document.ExecCommand("SelectAll", false, null);
    wb.Document.ExecCommand("Copy", false, null);

    rtbTemp.SelectAll();
    rtbTemp.Paste();

    Looks like it'd work to me.


  2. Rob Warning 1 Reputation point
    2020-10-07T12:22:16.647+00:00

    Hi DaistTian,

    thanks for your reply. it shows nice how to copy a string to a richTextBox. but it is not a string I want to put in my richTextBox but the content of the webBrowser.
    Since the conversion from HTML to RTF is apparently done inside the WebBrowser.
    I have tried now this:

    public void Convert2()
            {
                Clipboard.Clear();
                //string txtData = "I want to put this string on the clipboard";
                WebBrowser wb = new WebBrowser();
                wb.NavigateToString(teststring);
                Clipboard.SetDataObject(wb);
                Clipboard.SetData(DataFormats.Rtf, (object)wb.Document);
                //Clipboard.SetData(DataFormats.Html, (object)wb.Document);
                var dataObject = Clipboard.GetDataObject();
                var textData = dataObject.GetData(DataFormats.Html) as string;
                var textRange = new TextRange(myrtb.Document.ContentStart, myrtb.Document.ContentEnd) { Text = textData };
                myrtb.Focus();
    }
    

    but this still is not correct. I suppose the line 'Clipboard.SetData(DataFormats.Rtf, (object)wb.Document);' is not correct to get the real rtf content of the webbrowser. but I do not know how to do it corectly.

    regards

    Rob


  3. Rob Warning 1 Reputation point
    2020-10-09T09:10:27.263+00:00

    Hi @DaisyTian, You are probably right. I think it is the webbrowser that can convert a HTML string to rtf.
    Any way if jou create a wpf app put a webbrowser and a richtextbox in it. then load the Html in the webbrowser and copy paste the webbrowser content in the richtextbox and then save this as rtf string, it works great. the only problem is that this copy paste is a manual action. and I have a list with several thousand items.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.