Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How To Read RTF file in JAVA? Using iText?

 
Ninjin ninjin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How To Read RTF file in JAVA? Using iText?
Help me?


 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ninjin ninjin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic