Convert RTF to PDF with Open Source library iText

Version 2.1 of iText has partial support for reading an RTF file and converting it to a PDF. This feature is still under development.

and here is the example on how to parser and convert RTF to PDF(from http://cfsearching.blogspot.com/2009/04/itext-preview-of-things-to-come-someday.html)

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.pdf.PdfWriter;

import com.lowagie.text.rtf.parser.RtfParser;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class ConvertRTFToPDF {


public static void main(String[] args) {

 String inputFile = "sample.rtf";

 String outputFile = "sample_converted.pdf";

 // create a new document

 Document document = new Document();

 try {

     // create a PDF writer to save the new document to disk

     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));

     // open the document for modifications

     document.open();

     // create a new parser to load the RTF file

     RtfParser parser = new RtfParser(null);

     // read the rtf file into a compatible document

     parser.convertRtfDocument(new FileInputStream(inputFile), document);

     // save the pdf to disk

     document.close();

     System.out.println("Finished");

 } catch (DocumentException e) {

     e.printStackTrace();

 } catch (FileNotFoundException e) {

     e.printStackTrace();

 } catch (IOException e) {

     e.printStackTrace();

 }

}

}

Leave a Reply