2. Converting an XSL-FO file to RTF

This first sample consists in a single step: invoke XMLmind XSL-FO Converter to convert the XSL-FO input file to RTF.

Note that converting XSL-FO to other formats is simply a matter of changing the value of the outputFormat property. The possible values for this property are: rtf, wml, docx, odt.

Excerpts of samples/java/Sample1.java:

import org.xml.sax.InputSource;
import com.xmlmind.fo.converter.OutputDestination;
import com.xmlmind.fo.converter.Converter;

...
            Converter converter = new Converter();1
            converter.setProperty("outputFormat", "rtf");2
            converter.setProperty("outputEncoding", "Cp1252");
            converter.setProperty("imageResolution", "120");

            InputSource src = new InputSource(inFile.toURI().toASCIIString());3
            OutputDestination dst = new OutputDestination(outFile.getPath());4
            converter.convert(src, dst);5
...

1

Create a new Converter object.

2

Parameterize the Converter using setProperty or setProperties.

Note that specifying property outputEncoding is really useful only in the case of the RTF format. All the other formats are XML-based and thus, the default value of outputEncoding, generally UTF-8, should work fine in all cases.

3

Specify the input source of the Converter by the means of a SAX InputSource object.

Here we use the most high-level specification: we specify an URL. In production, you'll generally specify an InputStream or a Reader. Note that when you'll specify an InputStream or a Reader, the Converter will not automatically close it at the end of the conversion. You'll have to do that yourself. The rule here is: the code which has opened an InputStream or a Reader has the responsibility to close it.

4

Specify the output destination of the Converter by the means of a OutputDestination object.

Here we use the most high-level specification: we specify an File. In production, you'll generally specify an OutputStream or a Writer. As explained before, when you'll specify an OutputStream or a Writer, the Converter will not automatically close it at the end of the conversion.

[Tip]

Do not use OutputDestination.setEncoding to specify the encoding of the output of the Converter. Using property outputEncoding is much easier to spot.

5

Perform the conversion by invoking Converter.convert.