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