word剪贴板html,如何向HTML剪贴板数据添加空格,以便WinWord在粘贴时插入它们? - html代码 - 源码查...

博客讨论了如何在将数据复制到剪贴板时,以HTML格式保留格式信息,以便在支持HTML的程序(如WinWord)中粘贴时保持格式。作者发现单纯用空格在WinWord中无法正确粘贴,通过添加CSS属性`white-space:pre`解决了问题。WinWord处理多个空格的方式是使用自定义样式mso-spacerun:yes。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

In my application you can copy data to the clipboard. The data is copied to the clipboard in HTML format so that format information is preserved when pasting to other applications that can handle HTML data from the clipboard (e.g. WinWord).

Example (4 words, the last 2 bold):

This is bold text

Result: The text is correctly pasted in WinWord and the format (bold) is preserved.

Example (5 spaces):

Result: Nothing is pasted in WinWord.

Question: How can I achieve that WinWord preserves the spaces in the second example?

I tried encoding them as   but then WinWord shows them as a circle when paragraph marks are activated. And that looks kind of ugly.

解决方法

I solved this by adding the CSS value white-space:pre to the tags.

While white-space:pre-wrap seems to be the correct value, it is ignored by WinWord.

UPDATE:

I also analysed how WinWord creates its clipboard data. The HTML of several spaces looks like this:

Note:

The spaces are located in a separate tag with the style-attribute containing a custom key/value pair mso-spacerun:yes.

The HTML is UTF-8 encoded and the char code of the spaces is U+00A0 (no break space).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值