File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How To Read RTF file in JAVA?  Using  iText? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How To Read RTF file in JAVA?  Using  iText?" Watch "How To Read RTF file in JAVA?  Using  iText?" New topic
Author

How To Read RTF file in JAVA? Using iText?

Ninjin ninjin
Greenhorn

Joined: Jul 07, 2011
Posts: 2
How To Read RTF file in JAVA? Using iText?
Help me?


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41141
    
  45
Define "read" - what are you tyring to accomplish? And no - iText is not the answer for any definition of "reading RTF".

See my post in http://www.coderanch.com/t/441312/java/ for two options.


Ping & DNS - my free Android networking tools app
Ninjin ninjin
Greenhorn

Joined: Jul 07, 2011
Posts: 2
This code is dont use itext. ..


import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;

class RTF
extends JFrame
{
public RTF()
{
setTitle( "RTF Text Application" );
setSize( 400, 240 );
setBackground( Color.GREEN );
getContentPane().setLayout( new BorderLayout() );

JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel, BorderLayout.CENTER );

// Create an RTF editor window
RTFEditorKit rtf = new RTFEditorKit();
JEditorPane editor = new JEditorPane();
editor.setEditorKit( rtf );
editor.setBackground( Color.white );

// This text could be big so add a scroll pane
JScrollPane scroller = new JScrollPane();
scroller.getViewport().add( editor );
topPanel.add( scroller, BorderLayout.CENTER );

// Load an RTF file into the editor
try {
FileInputStream fi = new FileInputStream( "AAA.rtf" );


rtf.read( fi, editor.getDocument(), 0 );



System.out.println(editor.getDocument());
System.out.println(rtf.toString());

}
catch( FileNotFoundException e )
{
System.out.println( "File not found" );
}
catch( IOException e )
{
System.out.println( "I/O error" );
}
catch( BadLocationException e )
{
}
}

public static void main( String args[] )
{
// Create an instance of the test application
RTF mainFrame = new RTF();
mainFrame.setVisible( true );
}
}

Result




Itext use code is ...
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfWriter2;


public class RTF4 {


public static void main(String[] args) throws Exception {

System.out.println("Hello World example in RTF");

// step 1: creation of a document-object
Document document = new Document();

// step 2:
// we create a writer that listens to the document
// and directs a RTF-stream to a file

RtfWriter2.getInstance(document, new FileOutputStream("HelloWorld.rtf"));

// step 3: we open the document
document.open();

// step 4: we add a paragraph to the document
Paragraph p = new Paragraph("Hello");
for (int i = 0; i < args.length; i++) {
p.add(" ");
p.add(args[i]);
}
document.add(p);

// step 5: we close the document
document.close();
}
}


Result ... Exception...


HELP me ...
I wanna print Rtf file value--String
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41141
    
  45
I don't know how else to say it - iText has no facilities for reading RTFs in the sense of extracting data from it.

RTFEditorKit does, but it's buggy, and does not support all of RTF. I wouldn't base any serious effort of dealing with RTF on it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How To Read RTF file in JAVA? Using iText?
 
Similar Threads
RTF to PDF transformation
error while using Rtfwriter by iText version 2.1.3
Java API for RTF to DOCX Conversion
Read RTF File from Java
how to read rtf file and display in console?