RTF to PDF
Using IronPDF, you can convert your RTF (Rich Text Format) documents with embedded images into PDFs.
Convert rich-text content into PDF documents by invoking PdfDocument.renderRtfAsPdf
. This method works with an RTF string or with an external file (specified as a Path
or String
). It returns a PdfDocument
that you can further customize and then save with saveAs(...)
.
Be sure your RTF string is valid — renderRtfAsPdf
will throw an IOException
if it encounters formatting errors or unrecognizable elements.
How to Convert RTF to PDF in Java
Add IronPDF to your project (e.g., via Maven):
<dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>2025.6.5</version> </dependency>
<dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>2025.6.5</version> </dependency>
XML- Use
renderRtfAsPdf
for RTF strings, orrenderRtfFileAsPdf
for files. - Save the resulting PDF with
saveAs(...)
.
// Import IronPDF classes
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class RtfToPdfConverter {
/**
* Converts an RTF string to a PDF and saves it to the specified path.
*
* @param rtfContent The RTF content as a string.
* @param outputPath The path where the PDF will be saved.
* @throws IOException if there's an error during PDF creation.
*/
public static void convertRtfStringToPdf(String rtfContent, String outputPath) throws IOException {
// Render the RTF string as a PDF
PdfDocument pdfDocument = PdfDocument.renderRtfAsPdf(rtfContent);
// Save the PDF document
pdfDocument.saveAs(Paths.get(outputPath));
}
/**
* Converts an RTF file to a PDF and saves it to the specified path.
*
* @param rtfFilePath The path to the RTF file.
* @param outputPath The path where the PDF will be saved.
* @throws IOException if there's an error during PDF creation.
*/
public static void convertRtfFileToPdf(String rtfFilePath, String outputPath) throws IOException {
// Render the RTF file as a PDF
PdfDocument pdfDocument = PdfDocument.renderRtfFileAsPdf(Paths.get(rtfFilePath));
// Save the PDF document
pdfDocument.saveAs(Paths.get(outputPath));
}
public static void main(String[] args) {
String rtfContent = "{\\rtf1\\ansi\\deff0 {\\fonttbl{\\f0 Arial;}}\\f0\\fs20 Hello, World!}";
String outputFromString = "outputFromString.pdf";
String outputFromFile = "outputFromFile.pdf";
try {
convertRtfStringToPdf(rtfContent, outputFromString);
convertRtfFileToPdf("example.rtf", outputFromFile);
System.out.println("Conversion complete");
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Import IronPDF classes
import com.ironsoftware.ironpdf.PdfDocument;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class RtfToPdfConverter {
/**
* Converts an RTF string to a PDF and saves it to the specified path.
*
* @param rtfContent The RTF content as a string.
* @param outputPath The path where the PDF will be saved.
* @throws IOException if there's an error during PDF creation.
*/
public static void convertRtfStringToPdf(String rtfContent, String outputPath) throws IOException {
// Render the RTF string as a PDF
PdfDocument pdfDocument = PdfDocument.renderRtfAsPdf(rtfContent);
// Save the PDF document
pdfDocument.saveAs(Paths.get(outputPath));
}
/**
* Converts an RTF file to a PDF and saves it to the specified path.
*
* @param rtfFilePath The path to the RTF file.
* @param outputPath The path where the PDF will be saved.
* @throws IOException if there's an error during PDF creation.
*/
public static void convertRtfFileToPdf(String rtfFilePath, String outputPath) throws IOException {
// Render the RTF file as a PDF
PdfDocument pdfDocument = PdfDocument.renderRtfFileAsPdf(Paths.get(rtfFilePath));
// Save the PDF document
pdfDocument.saveAs(Paths.get(outputPath));
}
public static void main(String[] args) {
String rtfContent = "{\\rtf1\\ansi\\deff0 {\\fonttbl{\\f0 Arial;}}\\f0\\fs20 Hello, World!}";
String outputFromString = "outputFromString.pdf";
String outputFromFile = "outputFromFile.pdf";
try {
convertRtfStringToPdf(rtfContent, outputFromString);
convertRtfFileToPdf("example.rtf", outputFromFile);
System.out.println("Conversion complete");
} catch (IOException e) {
e.printStackTrace();
}
}
}