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 can convert rich text content from a string of raw data and also from an external file (referenced as a valid Path object or as a valid String path). The method returns a PdfDocument that the developer can customize further before saving the content to the filesystem (using the saveAs method).

For converting strings of raw rich-text data into PDFs, make sure that the string is formatted properly. renderRtfAsPdf will throw an IOException if the string data isn't formatted properly or if it contains unrecognizable elements (symbols, tags, etc.).

// Import required classes from the IronPDF library
import com.aspose.pdf.Document;
import com.aspose.pdf.PdfDocument;
import com.aspose.pdf.RtfLoadOptions;
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 {
        // RtfLoadOptions to handle RTF specifics
        RtfLoadOptions loadOptions = new RtfLoadOptions();

        // Create a PdfDocument from the RTF string
        PdfDocument pdfDocument = PdfDocument.renderRtfAsPdf(rtfContent, loadOptions);

        // Save the PDF document to the specified file path
        pdfDocument.saveAs(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 {
        // Construct a Path object for the RTF file
        Path rtfPath = Paths.get(rtfFilePath);

        // Create a PdfDocument from the RTF file
        PdfDocument pdfDocument = PdfDocument.renderRtfFileAsPdf(rtfPath);

        // Save the PDF document to the specified file path
        pdfDocument.saveAs(outputPath);
    }

    public static void main(String[] args) {
        // Example RTF content
        String rtfContent = "{\\rtf1\\ansi\\deff0 {\\fonttbl {\\f0 Arial;}}\\f0\\fs20 Hello, World!}";

        // Specify output paths
        String outputFromString = "outputFromString.pdf";
        String outputFromFile = "outputFromFile.pdf";

        try {
            // Convert RTF string to PDF
            convertRtfStringToPdf(rtfContent, outputFromString);

            // Convert RTF file to PDF, assuming `example.rtf` is a valid file path
            convertRtfFileToPdf("example.rtf", outputFromFile);

        } catch (IOException e) {
            // Handle the exception by printing the error message
            e.printStackTrace();
        }
    }
}
// Import required classes from the IronPDF library
import com.aspose.pdf.Document;
import com.aspose.pdf.PdfDocument;
import com.aspose.pdf.RtfLoadOptions;
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 {
        // RtfLoadOptions to handle RTF specifics
        RtfLoadOptions loadOptions = new RtfLoadOptions();

        // Create a PdfDocument from the RTF string
        PdfDocument pdfDocument = PdfDocument.renderRtfAsPdf(rtfContent, loadOptions);

        // Save the PDF document to the specified file path
        pdfDocument.saveAs(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 {
        // Construct a Path object for the RTF file
        Path rtfPath = Paths.get(rtfFilePath);

        // Create a PdfDocument from the RTF file
        PdfDocument pdfDocument = PdfDocument.renderRtfFileAsPdf(rtfPath);

        // Save the PDF document to the specified file path
        pdfDocument.saveAs(outputPath);
    }

    public static void main(String[] args) {
        // Example RTF content
        String rtfContent = "{\\rtf1\\ansi\\deff0 {\\fonttbl {\\f0 Arial;}}\\f0\\fs20 Hello, World!}";

        // Specify output paths
        String outputFromString = "outputFromString.pdf";
        String outputFromFile = "outputFromFile.pdf";

        try {
            // Convert RTF string to PDF
            convertRtfStringToPdf(rtfContent, outputFromString);

            // Convert RTF file to PDF, assuming `example.rtf` is a valid file path
            convertRtfFileToPdf("example.rtf", outputFromFile);

        } catch (IOException e) {
            // Handle the exception by printing the error message
            e.printStackTrace();
        }
    }
}
JAVA