副本上的不间断空格丢失了从word粘贴到wpf文本框 [英] Non-breaking spaces lost on copy & paste from word to wpf textbox

查看:125
本文介绍了副本上的不间断空格丢失了从word粘贴到wpf文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我目前正在实施一个文本编辑器,需要能够处理不间断的空格。(  /& ; nbsp;)。



我的问题是我们需要将预先存在的MS Word内容复制并粘贴到TextBox(或RichTextBox)中。但是,在从Word复制并将其插入我的文本框之间的某个地方丢失了不间断的空间。



有没有人遇到过这个问题?如果是这样,你有没有想办法呢?

我在想,也许我忽略了一些非常明显的东西......





无论如何,以下是我用来查看粘贴文本的代码。



Hello all,

I'm currently implementing a text editor which needs to be able to handle non-breaking spaces .(  /  ).

My problem is that we need to copy&paste pre-existing MS Word content into the TextBox (or RichTextBox). However, the non-breaking space was lost somewhere between copying from Word and inserting it into my textbox.

Have any of you encountered this problem? If so, did you figure out what to do about it?
I'm thinking that maybe I'm overlooking something really obvious here ...


Anyway, the following is the code I used to look at the pasted text.

private void PasteHandler(object sender, DataObjectPastingEventArgs e)
        {
            var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText);

            if (!isText) return;

            var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
            string t = "";


            foreach (char c in text.ToCharArray())
            {

                if (c == (char)160)
                {
                    // handle the character for later exports                
                }
                else
                {
                    t += c;
                }
            }
        }





我尝试了什么:



好​​吧,我已经查看了DataObjectPastingEventArgs的SourceDataObject的内容,特别是粘贴文本的char值。

当我从Word粘贴文本时,我得到的是常规空格(32)而不是不间断空格(160)。

当我在文本框中输入一个不间断的空格时(alt + 0160)我复制并粘贴这个字符,保留了不间断的空间。



我也试过改变DataFormat无济于事。





提前感谢任何帮助/提示



我也是直接查看了剪贴板的内容,但它是一样的(不是我真正期待的那样)。



What I have tried:

Well, I've looked at the content of the SourceDataObject of the DataObjectPastingEventArgs, specifically at the char values of the pasted text.
When I paste text from Word, all I get are regular spaces (32) instead of non-breaking spaces (160).
When I enter a non-breaking space into the textbox (alt + 0160) and I copy and paste this character, the non-breaking space is preserved.

I've also tried changing the DataFormat to no avail.


Thanks in advance for any help/hints

I also looked at the content of the clipboard directly but it's the same thing (not that I was really expecting otherwise).

推荐答案

当剪贴板文本中没有该字符时但是在源代码中,它可能在Word创建剪贴板对象时被替换。



然后唯一的解决方案是不使用Unicode文本剪贴板格式而是另一种。使用Word,至少应该有RTF和HTML。但是,这些需要处理使用的格式以将数据转换为Unicode文本。



对于HTML格式,请参阅 HTML剪贴板格式(Windows) [ ^ ]和 HTML剪贴板格式(Internet Explorer) [ ^ ]。



当您的编辑控件是RTF文本框时,使用RTF可能是更好的选择,但可能需要删除元数据(我这里没有安装Word来检查放入剪贴板的内容)。



RTF和HTML剪贴板格式都是文本格式。因此,您可以使用支持某种粘贴特殊选项的编辑器(如Notepad ++)来检查它们。
When the character is not present in the clipboard text but within the source, it is probably replaced when Word creates the clipboard objects.

Then the only solution would be not using the Unicode text clipboard format but another one. With Word, there should be at least RTF and HTML. However, these require processing the used format to convert the data to Unicode text.

For the HTML format see HTML Clipboard Format (Windows)[^] and HTML Clipboard Format (Internet Explorer)[^].

When your edit control is a RTF text box, using RTF might be the better choice but may require removing meta data (I have no Word installed here to check what is put to the clipboard).

RTF and HTML clipboard formats are both text formats. So you can inspect them with an editor that supports some kind of paste special option (like Notepad++).


这篇关于副本上的不间断空格丢失了从word粘贴到wpf文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆