简体   繁体   English

将文字从Microsoft Word复制到JTextPane

[英]Copying text from Microsoft Word into JTextPane

I have a text editor that I have created using a JTextPane. 我有一个使用JTextPane创建的文本编辑器。 The text is stored in html, so I am using the HtmlEditorKit with the JTextPane. 文本存储在html中,因此我将HtmlEditorKit与JTextPane一起使用。 The user wants to be able to copy text from a Microsoft Word document into the text editor. 用户希望能够将Microsoft Word文档中的文本复制到文本编辑器中。 Right now, when they try to copy the text into the editor, it simply does nothing and the text does not appear. 现在,当他们尝试将文本复制到编辑器中时,它什么也没做,并且文本不会出现。 But when they copy from Notepad, it does work. 但是,当他们从记事本复制时,它确实可以工作。

Is there something about the JTextPane or the HtmlEditorKit (or HtmlDocument) that does not allow text from a program like Word to be inserted? 关于JTextPane或HtmlEditorKit(或HtmlDocument),是否存在不允许插入诸如Word之类的程序中的文本的问题?
Is there a way around this? 有没有解决的办法?

Well it's a complex question. 好吧,这是一个复杂的问题。

Word copies text into clipboard using different formats: text/plain, text/html, text/rtf and probably Word's internal format. Word使用不同的格式将文本复制到剪贴板:text / plain,text / html,text / rtf以及Word的内部格式。 All these formats are in the same time in clipboard. 所有这些格式都在剪贴板中同时显示。 So you can use each of them to paste into your text pane. 因此,您可以使用它们中的每一个粘贴到您的文本窗格中。 Your text pane tries to insert the text from clipboard using the HTML format. 您的文本窗格尝试使用HTML格式从剪贴板插入文本。 The problem is: HTMLEditorKit can not understand the HTML dialect generated by Word. 问题是:HTMLEditorKit无法理解Word生成的HTML方言。

What you can do? 你可以做什么? If you don't need to support the formatted text, you can implement your own paste action, which uses the text/plane format. 如果不需要支持格式化的文本,则可以实施自己的粘贴操作,该操作使用文本/平面格式。 For another case I have no direct solution except you can write your own HTML (or RTF) parser which supports the HTML generated by Word. 在另一种情况下,除了您可以编写支持Word生成的HTML的HTML(或RTF)解析器之外,我没有直接的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM