Copying text from Microsoft Word into JTextPane

1.1k Views Asked by At

I have a text editor that I have created using a JTextPane. The text is stored in html, so I am using the HtmlEditorKit with the JTextPane. The user wants to be able to copy text from a Microsoft Word document into the text editor. 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?
Is there a way around this?

1

There are 1 best solutions below

0
On

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. 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. The problem is: HTMLEditorKit can not understand the HTML dialect generated by Word.

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.