Skip to content

Instantly share code, notes, and snippets.

@teppi210
Created May 13, 2021 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teppi210/0011810e70b54fe6cd15fc16a46fddc9 to your computer and use it in GitHub Desktop.
Save teppi210/0011810e70b54fe6cd15fc16a46fddc9 to your computer and use it in GitHub Desktop.
RTF to PDF using ITEXT
package com.company.common.util;
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 RtfConverter {
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));
System.out.println("open the document for modifications\n");
// open the document for modifications
document.open();
// create a new parser to load the RTF file
System.out.println("create a new parser to load the RTF file\n");
RtfParser parser = new RtfParser(null);
// read the rtf file into a compatible document
System.out.println("read the rtf file into a compatible document\n");
System.setProperty("os.name", "Windows 7");
parser.convertRtfDocument(new FileInputStream(inputFile), document);
// save the pdf to disk
System.out.println(" save the pdf to disk\n");
document.close();
System.out.println("Finished");
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment