1

I have a WebBrowser displaying text. If i copy it to clipbaord it copy's all the html tags to and i don't want that.

I want to be able to select all then copy to clipboard.

I want to copy the text and its formatting to the clipboard.

When i highlight the text my self and click copy when i paste, its perfect just how i want it.

But when i use this code to copy just the Document text i get the Html tags to.

This is how i copy to clipboard:

void CopyCellText()
{
Clipboard.Clear();
if (webBrowser1 != null)
{
Clipboard.SetText(webBrowser1.DocumentText.ToString().Trim());
}
}
Pomster
  • 14,567
  • 55
  • 128
  • 204

4 Answers4

2

To Select all and copy to clipboard:

webBrowser1.Document.ExecCommand("SelectAll", true, null);
webBrowser1.Document.ExecCommand("Copy", true, null);

You wont see the html tags but have all there formatting.

Pomster
  • 14,567
  • 55
  • 128
  • 204
  • This is more like a hack and can be discontinued, if you wanted to retain all format there are tools like [XHTML to RTF](http://www.codeproject.com/Articles/7087/XHTML2RTF-An-HTML-to-RTF-conversion-tool-based-on) – Chibueze Opata Jul 09 '12 at 12:38
1

You mean you want to convert your html code to text and copy to clipboard? You will need HTML Agility Pack. Check this page for an easy guide.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
1

http://www.dreamincode.net/code/snippet1921.htm << check this code snippet. it would be better, if you strip the string while using regex!

davee
  • 156
  • 1
  • 10
  • 1
    Regex and Html [do not match very well](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags), plus the OP wants to keep formatting! – Hans Kesting Jul 09 '12 at 11:22
  • did't know that, because i've never used smth like this! thanks! – davee Jul 09 '12 at 11:30
  • 1
    This is good, but i would like to keep the html formatting, my highlighting and underlines and cross outs, How do i select all? – Pomster Jul 09 '12 at 11:34
  • it's not as easy as i thought, but i found this: http://blogs.msdn.com/b/jmstall/archive/2007/01/21/html-clipboard.aspx?Redirected=true – davee Jul 09 '12 at 11:48
1

I think the reason you are getting the HTML tags is webBrowser1.DocumentText will take the entire content of the HTML document itself, which will include all the generated HTML.

A quick search gave me the following:

Retrieving Selected Text from Webbrowser control in .net(C#)

Get all text from WebBrowser control

Community
  • 1
  • 1
markhlong
  • 55
  • 1
  • 9