IRun-J RTF Converter for Java
version 1.07
IRun-J is a java software component that can convert RTF documents to
XML or HTML.. IRun-J can read RTF documents conforming to the RTF1.6
specifications. You can produce an XML or an HTML file from the source
document. There are two options for HTML converison, you can choose to
include inline CSS elements to create better results, or you can
exclude them to support older browsers. IRun-J can convert embedded
objects, and metafiles to SVG pictures if this option is enabled.
Since RTF and markup language are not 100% compatible some feature of
the RTF specifications are excluded from IRun. Some features though,
can be added in the later versions. Here is the list of features that
are not included in IRun:
·
Embedded fonts, font extension files.
·
Style shortcut keys
·
Positioned objects and frames (including floating pictures and objects)
·
List override table
·
Revision marks
·
Foot-notes,end-notes
·
Compatibility options
·
User defined document properties
·
Associated character properties
·
Bookmarks
·
Indexed entries
·
Drawing objects (Shapes)
·
Table of contents entries
·
Embedded tables
·
Form fields
1. Installation
IRun-J
is a java application. In order to run
IRun-J
you need to have at least J2RE1.4 (Java2 runtime) installed on your
system. Extract the irunjxxx.zip file in to an empty folder and set
JAVA_HOME variable in the irun.bat (irun.sh in linux) to point to the
correct java home before running IRun-J.
The following folders will be found under the IRun root:
/lib: contains necessary jar files, and irun.jar itself
/src: java sources
/doc: javadoc files
2. Running
In order to run IRun-J converter from a user interface, just execute
the irun.bat (or irun.sh in linux). If the JAVA_HOME is set correctly
then converter UI should show up.
You can select a source file by typing the path, or by selecting from
the file system. You have to give the destination location and name
also.
The default conversion type is "HTML with CSS", you can choose other options (XML and plain HTML) from the "type" part.
There are some options to choose when especially converting to HTML
(with or without CSS). Here are the meanings of conversion options:
Add results
: Fields, symbols and some other RTF special tags have "result"s, which
are ordinary RTF texts. Instead of trying to evaluate the field, IRun
reads results. Otherwise results are discarded, and since most of the
time evaluation of fields is not supported, the fields are completelely
discarded. It is suggested to choose this option.
No header
: You may want to add your headers to the converted output, or insert
output to another document. At those times you may choose this option;
IRun will only add the tags between <body> </body> elements
discarding the header tags.
Convert WMF to PNG
: If this option is selected, IRun will convert WMF and EMF pictures
(and also objects with WMF and EMF images) to PNG format. (Not yet
implemented)
Convert WMF to SVG
: If selected IRun will convert WMF and EMF pictures (and also objects
with WMF and EMF images) to SVG format. SVG is a vector graphic format
that requires a SVG enabled browser to show.
Embed base64 images
: This option makes IRun embed images rather than create separate files
for images. Generated output is a single HTML or XML. The images are
encoded in base64 binary encoding style. Not all browsers support
base64 encoded images.
Tag section boundaries
: This option tells IRun to put <sect> tags at section
boundaries. <sect> is not a valid tag. You may use it just for
the sake of information.
Display header/footer content
: This option tells IRun to use and convert header/footer data. H/F is
one of many differences between a web page and a document. The default
option is to discard H/F data.
After selecting the options, and determining source and destination
files, just click the "Convert" button. This will create the
destination file (or overwrite it if already exists), if everything
works fine. You may use your browser to see the created file.
3. Using as a component
In order to convert RTF from buffer to buffer, write batch conversion
scripts or add conversion functionality to server side components you
may use the conversion API.
com.pilot.irun.RtfConverter
is the only necessary class to import and use.
RtfConverter
has different convert functions for file-to-file and buffer-to-buffer
conversion. One has to set the options prior to calling the convert
methods.
For restricting and/or allowing debug-error log you can set two static fields in
com.pilot.irun.Util
class
Here are a couple of examples:
//Example1 : convert and RTF file to HTML with CSS.
import com.pilot.irun.RtfConverter;
import com.pilot.irun.Util;
public class Test1
{
public static void main(String[] args)
{
Util.debug=false;
Util.error=true;
RtfConverter conv=new RtfConverter();
conv.setBgcolor("ffffff");
conv.setDislayHeaderFooterData(false);
conv.setEncodeBase64(true);
conv.setNoHeader(false);
conv.setTagSections(true);
conv.setTitle("Welcome to the world of IRun-J");
conv.setWmfAction(RtfConverter.CONVERT_TO_SVG);
conv.convert(RtfConverter.HTML_WITH_CSS,
"c:\\temp\\Sample.rtf", "c:\\temp\\Sample.html");
}
}
//Example2 : convert and RTF file to HTML with CSS + no base64 encoding
import com.pilot.irun.RtfConverter;
import com.pilot.irun.Util;
public class Test2
{
public static void main(String[] args)
{
Util.debug=false;
Util.error=true;
RtfConverter conv=new RtfConverter();
conv.setBgcolor("ff00ff");
conv.setDislayHeaderFooterData(false);
conv.setEncodeBase64(false);
conv.setNoHeader(false);
conv.setTagSections(true);
conv.setTitle("Welcome to the world of IRun-J");
conv.setWmfAction(RtfConverter.CONVERT_TO_SVG);
conv.convert(RtfConverter.HTML_WITH_CSS,
"c:\\temp\\Sample.rtf", "c:\\temp\\Sample.html");
}
}
//Example3 : convert and RTF file to XML
import com.pilot.irun.RtfConverter;
import com.pilot.irun.Util;
public class Test3
{
public static void main(String[] args)
{
Util.debug=true;
Util.error=true;
RtfConverter conv=new RtfConverter();
conv.setDislayHeaderFooterData(false);
conv.setEncodeBase64(true);
conv.convert(RtfConverter.XML,
"c:\\temp\\Sample.rtf", "c:\\temp\\Sample.xml");
}
}
WMF and EMF metafiles to SVG conversion is a side product of IRun-J.
This functionality can be accessed seperately through the
com.pilot.irun.wmf.SvgConverter
class.
Here is a WMF/EMF to SVG conversion example:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.pilot.irun.wmf.Metafile;
import com.pilot.irun.wmf.SvgConverter;
import com.pilot.irun.wmf.WMFParser;
public class TestWmf
{
public static void main(String[] args)
{
Metafile mf=null;
try
{
FileInputStream fis=new FileInputStream("c:\\temp\\wmftest\\graph.wmf");
FileOutputStream fout=new FileOutputStream("c:\\temp\\wmftest\\graph.svg");
WMFParser parser=new WMFParser();
mf=parser.parse(fis);
SvgConverter converter=new SvgConverter();
converter.convert(mf, fout);
fout.flush();
fout.close();
}
catch (FileNotFoundException e)
catch (IOException e)
e.printStackTrace();
}
}
One can also write a custom RTF converter by implementing the
com.pilot.irun.RtfListener
interface. You can register your listener to the
com.pilot.irun.RtfParser
, which is responsible for parsing the RTF and informing listeners.
RtfListener inteface:
public interface RtfListener
{
public void handleDocumentStart(RtfInfo info);
public void handleParagraph(RtfInfo info,Par par);
public void handleRow(RtfInfo info,Row row);
public void handleSectionStart(RtfInfo info,Sect sect);
public void handleSectionEnd(RtfInfo info,Sect sect);
public void handleDestinationStart(RtfInfo info,String dest);
public void handleDestinationEnd(RtfInfo info,String dest);
public void handleDocumentEnd(RtfInfo info,Doc doc);
}
RtfInfo, Par, Sect, Doc are RTF structures (ie. simple java objects) containing RTF properties and data.
Suppose TestReader is an RtfListener implementation:
package com.pilot.irun.test;
import com.pilot.irun.*;
import java.io.*;
public class TestReader
{
public static void main(String[] args)
{
RtfParser reader=new RtfParser();
FileInputStream fis=null;
ByteArrayOutputStream out=new ByteArrayOutputStream();
try
{
fis=new FileInputStream("c:\\temp\\Sample.rtf");
fous=new FileOutputStream("c:\\temp\\Sample.html");
reader.registerListener(new TestListener());
reader.read(fis,true);
}
catch(Exception e)
System.out.println("TestReader::main:exception:"+e);
}
}
You may contact the API docs (under the /doc/index.html) for custom conversion.
Please report bugs to
support@pilotltd.com
, you may attach the source file for which you encounter problems.
Pilot Software Ltd.
http://www.pilotltd.com