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

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

Related

How to programmatically create/edit an RTF header in C#?

So, the problem is there I already have the formatted text in RTF format but when I save the file it gets "wrong", the header disappears.
I'm using the RichTextBox Class to create the file, where finalText is the formatted RTF text, making the code look something like this:
System.Windows.Forms.RichTextBox RichTextBox = new System.Windows.Forms.RichTextBox();
string finalText = "{\rtf1\deff0{\fonttbl{\f0 Calibri;}{\f1 Arial;}{\f2 ..." //Formatted RTF here
RichTextBox.Rtf = finalText;
RichTextBox.SaveFile(_RtfPath_);
When I do this the final version of the generated file is almost complete, but the header disappears.
Editing the file in notepad++ I noticed that it removes the header and the information contained in it. After this I decided to manually create an RTF file. I opened the almost complete RTF file in notepad++, erased everything and pasted the finalText.
When I reopened this file the final version was the way I wanted it.
Is there any way I can save the text as RTF without having to use the RichTextBox Class to format the output file?

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.

Comparison Of two HTML

I am working on 2 pdf Compare where 1st I extracted PDF to get styles of PDF .PDF styles are extracted and converted to html. then at last I compare 2 HTML text.
For PDF Extraction I used itextsharp
Here is the code
this.result.AppendFormat(
"<tr><td>{0}</td><td>{1}</td><td>{2}pt</td><td>{3}</td><td>{4}</td><td>",
Form1.j, curFont, font_size_client, fontweight, fontstyle);
}
this.result.Append(renderInfo.GetText());
}
public string GetResultantText()
{
if (result.Length > 0)
{
result.Append("<tr><td></td><td></td>");
}
return result.ToString();
}
this is the code where i used for conversion. MY question is one sample PDF i used HTML text book itself for comparison the text .in HTMLbook there were some styles like background color, fontsize got appended to the text . instead of text it took style. Can you please tel me how to extract style. is the method i chosen to compare 2 PDF is wrong. OR any other method.
Once you extracted it to HTML you can use jQuery to get the .HTML() code.
Take a look here: https://api.jquery.com/html/
After that you can compare the two results.

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

I want to select all and copy it to clipboard

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

Categories