7 Using Writer2LaTeX from another application

7.1 Using Writer2LaTeX from a Java application

Writer2LaTeX features a simple API to convert documents from another Java application. Please see the javadoc for writer2latex.jar (the package writer2latex.api) for details.

The API offers a stream based as well as a file based interface for conversions.

Here's a simple example showing how to convert a file to LaTeX using a custom configuration (excluding exception handling) using the file based methods of the API.

import java.io.File;

import writer2latex.api.*;

 

// Create a LaTeX converter

Converter converter =

    ConverterFactory.createConverter("application/x-latex");

 

// Configure the converter

Config config = converter.getConfig();

config.read(new File("myconfig.xml"));

config.setOption("inputencoding","latin1");

 

// Convert the document

ConverterResult result =

    converter.convert(new File("mydocument.odt"),

        "mydocument.tex");

 

// Write the files

result.write(new File("mydirectory"));

Using the stream based methods the conversion may look like this (assuming the option save_images_in_subdir is set to false):

import java.io.FileInputStream;

import java.io.FileOutputStream;

 

// Convert the document

ConverterResult result =

    converter.convert(new FileInputStream("mydocument.odt"),   

    "mydocument.tex");

 

// Write the files

Iterator<OutputFile> docs = result.iterator();

while (docs.hasNext()) {

    OutputFile docOut = (OutputFile) docs.next();

    FileOutputStream fos =

        new FileOutputStream("mydirectory/"+docOut.getFileName());

    docOut.write(fos);

    fos.flush();

    fos.close();

}

Writer2LaTeX also offers an interface for batch conversion of a directory into XHTML. For at simple example, see the source of Application.java.