【问题标题】:Save JTextPane superscript text as RTF将 JTextPane 上标文本另存为 RTF
【发布时间】:2013-06-27 04:16:43
【问题描述】:

如何将JTextPane上标文字保存为RTF?

我正在尝试创建类似于文本编辑器的写字板。我用RTFEditorKit。我可以使用下面的代码添加上标。 (例如:X 的 2 次方)

public void setSuperscript() {
   EditorKit editorKit = this.getEditorKit();
   MutableAttributeSet att = ((StyledEditorKit) editorKit).getInputAttributes();
   StyleConstants.setSuperscript(att, !StyleConstants.isSuperscript(att));
   super.setCharacterAttributes(att, false);
}

我将文档保存如下。

public void save(String fileName) {
     BufferedOutputStream out = new BufferedOutputStream(new  FileOutputStream(fileName));
   try {
     StyledDocument doc = this.getStyledDocument();
     OutputStream outputStream = new FileOutputStream(fileName);
     this.getEditorKit().write(out, doc, 0, doc.getLength());
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     out.close();
   }
}

保存成功。但是当我使用写字板或 Microsoft Word 打开它时,上标格式丢失了。

使用 AdvancedEditorKit 我得到了这个工作。

JTextPane pane = new JTextPane();
AdvancedRTFDocument document = new AdvancedRTFDocument();
AdvancedRTFEditorKit editor = new AdvancedRTFEditorKit();
editor.write("test.rtf", document);

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 谢谢安德烈。我会记住的

标签: java swing save rtf jtextpane


【解决方案1】:

您可以使用替代RTFEditorKithttp://java-sl.com/advanced_rtf_editor_kit.html

默认的功能有限,因此不支持许多 RTF 格式化功能。

【讨论】:

  • 我试过你的 AdvancedRTFEditorKit。但它没有保存任何东西。保存后文档为空白。 JTextPane textPane = new JTextPane(); AdvancedRTFEditorKit 套件 = 新的 AdvancedRTFEditorKit(); textPane.setEditorKit(kit); textPane.setDocument(new AdvancedRTFDocument()); StyledDocument doc = (StyledDocument) textPane.getDocument(); OutputStream out = new FileOutputStream("output.rtf"); textPane.getEditorKit().write(out, doc, 0, doc.getLength());
猜你喜欢
  • 1970-01-01
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 2015-05-10
相关资源
最近更新 更多