简体   繁体   English

如何将StyledDocument从textPane保存到.doc文件?

[英]How to save a StyledDocument from textPane into a .doc file?

I don't know if this is even possible, but what I would like to do is to save a Styled Document (user can change text: bold, underline, italic and 3 sizes of font) in a .doc file - so he could open it later on his own with any other text editor that supports styled text. 我什至不知道这是否可能,但是我想做的是将样式文档(用户可以更改文本:粗体,下划线,斜体和3种字体大小)保存在.doc文件中-这样他就可以以后可以使用任何其他支持样式文本的文本编辑器自行打开它。

I wrote the code below... the editor works, I can apply the styles on the text but when I save, it saves the text just black, with no styles. 我在下面编写了代码...编辑器起作用了,我可以在文本上应用样式,但是当我保存时,它将文本保存为黑色,没有样式。 I can't figure out where is the problem. 我不知道问题出在哪里。 Maybe the actions don't save. 动作可能无法保存。 I tried with writer and with buffered writer but it didn't work. 我尝试了作家和缓冲作家,但没有用。 I also tried to use HTML editor kit and it didn't work at all - it saved a blank document. 我还尝试使用HTML编辑器工具包,但它根本不起作用-它保存了一个空白文档。

Maybe anyone has an idea how can I save the styles? 也许有人知道如何保存样式? Appreciate the help :) 感谢帮助:)

public class EditFrame extends javax.swing.JFrame {

JFrame frameEdit = this;
File file; //A file I would like to save to -> fileName.doc
StyledDocument doc;   
HashMap<Object, Action> actions;
StyledEditorKit kit;

public EditFrame() {
    super();
    initComponents();
    JMenu editMenu = createEditMenu();
}

protected JMenu createEditMenu() {
    JMenu menu = editMenu;

    Action action = new StyledEditorKit.BoldAction();
    action.putValue(Action.NAME, "Bold");
    menu.add(action);

    action = new StyledEditorKit.ItalicAction();
    action.putValue(Action.NAME, "Italic");
    menu.add(action);

    //...

    return menu;
}

//I'm guessing this doesn't work correctly too (doesn't read styles), but this is another subject :)
public void readFile(File f) {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "windows-1250"));
        textPane.read(reader, null);
        textPane.requestFocus();
    } catch (IOException ex) {
        Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

//SAVE METHOD
private void save(java.awt.event.ActionEvent evt) {                      
    try {
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        kit = (StyledEditorKit) textPane.getEditorKit();
        doc = (StyledDocument) textPane.getDocument();
        kit.write(out, doc, 0, doc.getLength());
    } catch (FileNotFoundException ex) {
        Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedEncodingException | BadLocationException ex) {
        Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}                     

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new EditFrame().setVisible(true);
        }
    });
}
}

You can use RTFEditorKit , which supports Rich Text Format (RTF). 您可以使用RTFEditorKit ,它支持RTF格式 Lots of word processors, including MS Word, can work with this format. 许多文字处理程序(包括MS Word)都可以使用这种格式。 Stick with write() to an OutputStream , which writes "a format appropriate for this kind of content handler." 坚持使用write()OutputStream ,它编写“适合这种内容处理程序的格式”。 The other one that uses a Writer writes "to the given stream as plain text." 另一个使用Writer将“以纯文本形式写入给定流”。

Why is StyledEditorKit not working? 为什么StyledEditorKit不起作用?

StyledEditorKit gets its write() implementation from DefaultEditorKit , "which treats text as plain text." StyledEditorKitDefaultEditorKit获取其write()实现,“它将文本视为纯文本。” StyledEditorKit stores styled text internally, but it doesn't know about any external format. StyledEditorKit内部存储样式文本,但是不知道任何外部格式。 You have to go to one of the subclasses, HTMLEditorKit or RTFEditorKit , to get something that override's the default write() . 您必须进入子类之一HTMLEditorKitRTFEditorKit ,以获取覆盖默认write() The overridden method knows how to transform the internal format to an external format, like RTF. 重写的方法知道如何将内部格式转换为外部格式,例如RTF。

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

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