Java Code Examples for javax.swing.text.rtf.RTFEditorKit

Following code examples demonstrate how to use javax.swing.text.rtf.RTFEditorKitfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use javax.swing.text.rtf.RTFEditorKitand various code implementation of this class.

  public String parse(String fileName) throws Exception {
	  InputStream stream=new FileInputStream(fileName);
      RTFEditorKit kit = new RTFEditorKit();
      Document doc = kit.createDefaultDocument();
      kit.read(stream, doc, 0);
      String text = doc.getText(0, doc.getLength());
      stream.close();
      return (text);
  } 


    static String unRTF( String s ) {
        if( s == null ) return null;
        
        DefaultStyledDocument doc = new DefaultStyledDocument();
        RTFEditorKit kit = new RTFEditorKit();
        
        try {
            kit.read( new StringReader( s ), doc, 0 );
        }
        catch( Exception ex ) {
            throw new RuntimeException( ex );
        }
        
        try {
            s = doc.getText( 0, doc.getLength() );
        } catch( BadLocationException e ) {
            throw new RuntimeException( e );
        }
        
        return s;
    } 

	public String extractText(String file) throws Exception{
		FileInputStream stream = new FileInputStream(file);
		RTFEditorKit kit = new RTFEditorKit();
		Document doc = kit.createDefaultDocument();
		kit.read(stream, doc, 0);
		return doc.getText(0, doc.getLength());
		
	} 

	public static String aI(String rtfString) throws Exception {
		RTFEditorKit kit = new RTFEditorKit();
		Document doc = kit.createDefaultDocument();
		ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(rtfString.getBytes());
		InputStreamReader in = new InputStreamReader(tInputStringStream, "utf-8");
		kit.read(in, doc, 0);
		String result = doc.getText(0, doc.getLength());
		return result;
	} 

	public static String getTextFromRtf(String filePath) {
		String result = null;
		File file = new File(filePath);
		RTFEditorKit rtf = new RTFEditorKit();
		try {
			DefaultStyledDocument styledDoc = new DefaultStyledDocument();
			InputStream is = new FileInputStream(file);
			rtf.read(is, styledDoc, 0);
			result = new String(styledDoc.getText(0, styledDoc.getLength())
					.getBytes("ISO8859_1"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return result;
	} 

	protected String extractText() throws Exception {
		  RTFEditorKit rtf = new RTFEditorKit();
	      Document doc = rtf.createDefaultDocument();
	  
	      FileInputStream fis = new FileInputStream(this.getSysFile());
	      rtf.read(fis,doc,0);
	      String text = doc.getText(0,doc.getLength());
	      
	      fis.close();
	      
	      return text;
	} 

    @Category(Draft.class)
    public void create_RTFEditorKit()
    throws Exception {
        @SuppressWarnings("unused")
        final RTFEditorKit sut = new RTFEditorKit();
    } 

	public String getContent() throws Exception {
		RTFEditorKit rtfKit=new RTFEditorKit();
		DefaultStyledDocument dsd=new DefaultStyledDocument();
		try {
			rtfKit.read(is, dsd, 0);
			String tmp=dsd.getText(0, dsd.getLength());
			return new String(tmp.getBytes("iso-8859-1"));
		} catch (Exception e) {
                e.printStackTrace();
                throw e;
		}
		
	} 

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to RTFEditorKit.class!",
                   RTFEditorKit.class.isAssignableFrom(sut));
    } 

    public StyledEditorKit getKit() {
        if (this.kit == null) {
            this.kit = new RTFEditorKit();
        }
        return this.kit;
    } 

	public String getText(InputStream is) throws IOException {

		DefaultStyledDocument styledDoc = new DefaultStyledDocument();
		try {
			new RTFEditorKit().read(is, styledDoc, 0);
			return styledDoc.getText(0, styledDoc.getLength());
		} catch (BadLocationException e) {
			throw new IOException(e);
		}
	} 

    public void LeerRTF(File file) {
        
        try {
            RTFEditorKit rtfParser = new RTFEditorKit();
            Document document = rtfParser.createDefaultDocument();
            FileInputStream fi = new FileInputStream(file);
            rtfParser.read(fi, document, 0);
            String text = document.getText(0, document.getLength());
            System.out.println(text);
            
                        
            writer.flush();
            
            StopWords sw = new StopWords();
            Steeming stm=new Steeming();
            String stopWords="";
            stopWords=sw.remove(sw.remove(text));
            writer.flush();
            
            archivo = new File("archivos/rtf/"+ file.getName() + ".steeming.txt");            
            writer.flush();
                  

        } catch (BadLocationException | IOException ex) {
            Logger.getLogger(LeerRTF.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    public RTFFileExport(File f, Document doc) {
        RTFEditorKit kit = new RTFEditorKit();        
        try {            
            kit.write(new FileOutputStream(f), (DefaultStyledDocument)doc, 0, doc.getLength());
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    } 

    public void extract(InputStream file, FilePath target) throws IOException {
        try {
            RTFEditorKit rtfParser = new RTFEditorKit();
            Document document = rtfParser.createDefaultDocument();
            rtfParser.read(file, document, 0);
            String text = document.getText(0, document.getLength());
            target.write(text);
        } catch (BadLocationException ex) {
            throw new IOException(ex);
        }
    } 

Advertisement
Javadoc
This is the default implementation of RTF editing functionality. The RTF support was not written by

the Swing team. In the future we hope to improve the support provided. @author Timothy Prinzing (of this class, not the package!)

Read More
Advertisement