• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How To Read RTF file in JAVA? Using iText?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How To Read RTF file in JAVA? Using iText?
Help me?


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 https://coderanch.com/t/441312/java/ for two options.
 
Ninjin ninjin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This will take every ounce of my mental strength! All for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic