Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Copy Paste of formatted text work?

I'm confused about what implements the functionality of copy and paste. This is exactly what I'm confused with:

When I copy formatted text from MS Word (which uses a different markup language than HTML) and paste into an RTF editor in a web browser like gmail or http://www.freerichtexteditor.com/index.php?inc=demo/index the formatting is preserved but now the markup is converted into HTML. How did this happen? What took care of the conversion?

And if I had pasted this text into some other application, it will be converted into that format. If I copied some html page and pasted it in word then there will a markup conversion from HTML to word. Again, how?

Then if I paste this copied formatted text into a text editor like Notepad then all the formatting is lost and markup is stripped off. Which application stripped the markup and converted it to plain text?

When I copy formatted text, what exactly is copied into the clipboard? I 'm a .NET C# programmer. How would I program this?

like image 395
claws Avatar asked Dec 11 '09 05:12

claws


People also ask

How do I copy and paste without messing up formatting?

You'll get just the text you copied as if you had typed it directly into the application you're pasting it in. To paste without formatting, press Ctrl+Shift+V instead of Ctrl+V. This works in a wide variety of applications, including web browsers like Google Chrome. It should work on Windows, Chrome OS, and Linux.

What is copying formatted text?

Press Ctrl + Shift + C. Drag over the text to which you want to copy the formatting. If you drag over characters only, Word will copy character formatting. If you drag over an entire paragraph or paragraphs, Word will copy character and paragraph formatting.

How do I copy and paste exact formatting in Word?

Click in text that has the formatting to replicate, and press CTRL-SHIFT-C. It'll copy the formatting, but not the text itself. Highlight the target text and press CTRL-SHIFT-V to paste the formatting.


1 Answers

The data on the clipboard is extended with FORMATETC records:

http://msdn.microsoft.com/en-us/library/ms682177%28VS.85%29.aspx

The FORMATETC record contains as first field a cfFormat member which describes the file format. cfFormat can be a predefined value like CF_UNICODETEXT or CF_BITMAP or an application defined type defined by e.g. Microsoft Word.

In .NET you can apparently query the Clipboard object to find out which data formats it contains:

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

The method you are looking for is Clipboard.SetData:

If you do not know the format of the target application, you can store data in multiple formats using this method.

Data stored using this method can be converted to a compatible format when it is retrieved.

To retrieve data from the Clipboard in a particular format, first use the ContainsData method to determine whether the Clipboard contains data in that format before retrieving it with the GetData method

As to your concrete question how it works in Word, the above links should give you enough information to write a little clipboard viewer yourself. Since Microsoft Word is able to output HTML files, my guess is that Word writes the data on the clipboard as simple Text, HTML, RTF and in Word format.

like image 147
Sebastian Avatar answered Sep 28 '22 00:09

Sebastian