question

RobWarning-5632 avatar image
0 Votes"
RobWarning-5632 asked DaisyTian-1203 edited

WPF Core Clipboard question

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-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndyONeill avatar image
0 Votes"
AndyONeill answered DaisyTian-1203 edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Andy,

I suppose the answer jou gave is not for the core version of dot net.
I do not have the document.ExecCommand or the Document.write(html) option.
It looks like the core version is completely different in this aspect.

regards,

Rob

0 Votes 0 ·

@RobWarning-5632 To paste the Clipboard to the Richtextbox, please try below code:

  public void Convert2()
         {
             Clipboard.Clear();
             string txtData = "I want to put this string on the clipboard.";
             Clipboard.SetData(DataFormats.Html, (object)txtData);
             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();
         }

You can go to WPF-Samples/Clipboard/ for using Clipboard in .core WPF.


0 Votes 0 ·
RobWarning-5632 avatar image
0 Votes"
RobWarning-5632 answered DaisyTian-1203 edited

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@RobWarning-5632 Clipboard.SetData(String, Object) Method stores the specified data on the Clipboard in the specified format.On my understanding, the store does not mean convert, you need to convert rtf alone.


0 Votes 0 ·
RobWarning-5632 avatar image
0 Votes"
RobWarning-5632 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.