欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Java RTFEditorKit 类使用示例

最编程 2024-04-29 15:52:27
...

实例1: RTF2TXT

import javax.swing.text.rtf.RTFEditorKit; //导入依赖的package包/类
public static final String RTF2TXT(String Str)
throws IOException, BadLocationException
  {
    if (Str != null && Str.startsWith("{\\rtf1") == true)
      {
        // There is a "questionable" bug in the RTF-to-Text routine in the Java library. With tables, text in
        // adjacent cells are concatenated without any
        // spacing. So, for example, a table with a cell containing "abc" followed by another call containing "123",
        // after conversion, you'll get "abc123".
        // With this hack, we capture the RTF cell delimiter "\cell$" and replace it with ". \cell$". This will
        // separate text in cells from other text and will
        // allow text processing to give better results.
        Str = RTF_CELL_PATTERN.matcher(Str).replaceAll(". $0");
        RTFEditorKit RTF = new RTFEditorKit();
        Document doc = RTF.createDefaultDocument();
        RTF.read(new StringReader(Str), doc, 0);
        Str = doc.getText(0, doc.getLength());
      }
    return Str;
  }
 

实例2: main

import javax.swing.text.rtf.RTFEditorKit; //导入依赖的package包/类
public static void main(String[] args) throws Exception{
    rtfEditorKit = new RTFEditorKit();
    robot = new Robot();

    SwingUtilities.invokeAndWait(() -> {
        frame = new JFrame();
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(600, 200);
        jTextPane = new JTextPane();
        frame.getContentPane().add(jTextPane);
        frame.setVisible(true);
    });

    test(StyleConstants.ALIGN_LEFT);
    test(StyleConstants.ALIGN_CENTER);
    test(StyleConstants.ALIGN_RIGHT);
    test(StyleConstants.ALIGN_JUSTIFIED);

    SwingUtilities.invokeAndWait(()->frame.dispose());

    System.out.println("ok");
}
 

实例3: extractText

import javax.swing.text.rtf.RTFEditorKit; //导入依赖的package包/类
/**
 * {@inheritDoc}
 */
public String extractText(InputStream stream, String type, String encoding) throws IOException {

	try {
		RTFEditorKit rek = new RTFEditorKit();
		DefaultStyledDocument doc = new DefaultStyledDocument();
		rek.read(stream, doc, 0);
		String text = doc.getText(0, doc.getLength());
		return text;
	} catch (Throwable e) {
		logger.warn("Failed to extract RTF text content", e);
		throw new IOException(e.getMessage(), e);
	} finally {
		stream.close();
	}
}
 

实例4: installWidgetsFromRTF

import javax.swing.text.rtf.RTFEditorKit; //导入依赖的package包/类
/**
 * @param db The {@link Dragboard} containing the dragged data.
 * @param selection_tracker Used to get the grid steps from its model to be
 *            used in offsetting multiple widgets.
 * @param widgets The container of the created widgets.
 */
private static void installWidgetsFromRTF (
    final DragEvent event,
    final SelectedWidgetUITracker selection_tracker,
    final List<Widget> widgets
) {

    final Dragboard db = event.getDragboard();
    final String rtf = db.getRtf();
    final RTFEditorKit rtfParser = new RTFEditorKit();
    final Document document = rtfParser.createDefaultDocument();

    try {
        rtfParser.read(new ByteArrayInputStream(rtf.getBytes()), document, 0);
        installWidgetsFromString(event, document.getText(0, document.getLength()), selection_tracker, widgets);
    } catch ( Exception ex ) {
        logger.log(Level.WARNING, "Invalid RTF string", ex);
    }

}
 

实例5: actionPerformed

import javax.swing.text.rtf.RTFEditorKit; //导入依赖的package包/类
@Override
public void actionPerformed(ActionEvent event) {
    try {
        boolean b = sysCaller.isPastingAllowed();
        System.out.println("Pasting allowed: " + b);
        if (!b) {
            JOptionPane.showMessageDialog(null, sysCaller.getLastError());
            return;
        }

        RTFEditorKit myEditorKit = (RTFEditorKit) myEditorPane.getEditorKit();
        Action edKitPasteAct = getActionByName(RTFEditorKit.pasteAction);
        edKitPasteAct.actionPerformed(event);
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(RTFEditor.this, "Exception while copying selected text: " + e.getMessage());
    }
}
 

推荐阅读