I want to select all and copy it to clipboard - c#

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());
}
}

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.

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.

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

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

Related

How to copy richtextbox content with link?

I want to copy richtextbox content with keeping format same and hyperlinks. But it is been copied as a plain text without hyperlinks. I am using linklabel in richtextbox.
private void Bttn_copy_Click(object sender, EventArgs e)
{
richtxtbx_email.SelectAll();
Clipboard.Clear();
Clipboard.SetText(richtxtbx_email.SelectedRtf, TextDataFormat.Rtf);
}
and trying this:
DataObject dto = new DataObject();
dto.SetText(mesrtf, TextDataFormat.Rtf);
dto.SetText(mes, TextDataFormat.UnicodeText);
Clipboard.Clear();
Clipboard.SetDataObject(dto);
Can you help me solve this issue ?
Hyperlinks are just a way of a using hypertext links inside an editor that is capable of rendering them as such.
When copying the text from the textbox, you can only copy the plaintext itself.
Note that RichTextBox.SelectedRtf is property of type string.
RichTextBox doesn't hold a hyperlink like HTML does.
It only detects if a certain text looks like a link and automatically colores it blue, add underline and detect if the user clicks on it.
It does so if the RichTextBox.DetectUrls Property is set to true.
If you are copying data to a new RichTextBox and don't see the link that was detected in the other RichTextBox then you just need to set this property to true before you copy the text.
On the other hand, if you need real links so that the text is one thing and the link is another have a look here.

How to copy both - HTML and text to the clipboard?

I'm trying to put in the clipboard piece of HTML and plain text at the same time, so that HTML-capable editors could paste HTML, and other editors could use plain text.
Clipboard.SetData(DataFormats.Html, htmlWithHeader);
Clipboard.SetData(DataFormats.UnicodeText, plainText);
But only the last format is actually put to the clipboard. In the sample above, clipboard would contain only plaintext (as shown by Clipboard.GetDataObject().GetFormats()). And if I swap the lines, the clipboard would have only the HTML format.
How can I put both formats into the clipboard at the same time?
You can NOT use Clipboard.SetData for setting both HTML and plain text. The second call of SetData will clear the content of clipboard that has been set by first call and store the new data.
You should use DataObject and Clipboard.SetDataObject().
Example:
DataObject dataObj = new DataObject();
dataObj.SetData(DataFormats.Html, htmlWithHeader);
dataObj.SetData(DataFormats.Text, plainText);
Clipboard.SetDataObject(dataObj);

C# Paste HTML to Excel or PowerPoint

How to paste HTML ( tables ) code into Excel or PowerPoint?
I've overcome some issues concerning pasting HTML into Excel and PowerPoint and noticed that a lot of people are asking that.
I'd like to share my research, solution I made out for it.
Let's say we have a html file named html and we would like to access it in Excel, let's do following:
Clipboard.SetText(html);
We copy our html into the Clipboard. The clipboard generates from the html a real table or image/chart from the input file.
System.Threading.Thread.Sleep(2000);
Let's wait a second to have a preview
sheet.Range(cellmapp).PasteSpecial();
Now, we paste the content into a range that we could like to paste it, by defining cellmap.
System.Threading.Thread.Sleep(1000);
Let's wait a second to see the output
sheet.UsedRange.Copy(Missing.Value);
Now, in order to copy the table image into PowerPoint, we must work the with UsedRange.Copy, because it will copy the currently selected Excel area.
In order to check that we paste it into the correct Powerpoint slide
foreach (PowerPoint.Slide slide in presentation.Slides)
{
foreach (PowerPoint.Shape pptshape in slide.Shapes)
{
if(<your condition satisfies>)
{
slide.Select(); // some position in any slide
pptshape.Delete();//delete old content that was in that slide
ppApp.ActiveWindow.View.PasteSpecial(); //paste the Excel content
}
}
}
Of course there are other solutions, like making an image out of the html code and pasting that, which was my initial idea.
Another post refering that manipulation:
Showing HTML in PowerPoint

How to remove html tag and just leave text in C#?

I see a similar case which answer my question in JQuery way:
Remove tag but leave contents - jQuery/Javascript
But I need to know how C# works for this? Is there any API in C# that I can use to strip out the tag and leave the plane text?
From:
some content and more and more content
To:
some content and more and more content
Thanks.
Use HtmlAgilityPack ( http://htmlagilitypack.codeplex.com/ ), load your HTML document into a HtmlDocument instance and query document.DocumentNode.InnerText.

ASP.NET Which HTML editor can do everything I want?

I have tried to use the standard AJAX HTMLeditor from here (http://www.asp.net/ajaxlibrary/act.ashx) and I have try to work with the FCKEditor (from http://ckeditor.com/)
But both don't do everything. I call the AJAX standard control A and the FCKeditor F.
With the A editor it is impossible to get your HTML text in the HTML content. You can only get it in the Design content. (this next code doesn't do the job: string htmlContentStr = Editor1.Content).
With F it is possible to get it in the HTML content (it does this by default), but to get your changes back in HTML is impossible. (this next code doesn't do the job: string htmlContentStr = FCKeditor1.Value).
So what I need is a HTML editor that is possible to put HTML text in HTML content, a user can make changes in the designcontent and after the changes 're make it must be possible to get the HTMLcontent and put it away in a string or database.
Is this possible or do I need a commercial one to get this feature?
If my question isn't clear, please let me know.
Thnx
I've used XStandard quite easily and it let me manipulate the HTML. I didn't bother using it as a control, but just read and wrote (escaped) the HTML where needed into the asp output.

Categories