Java: Convert RTF to HTML, Image

2024-11-22 01:48:04 Written by  support iceblue
Rate this item
(0 votes)

Converting RTF to HTML helps improve accessibility as HTML documents can be easily displayed in web browsers, making them accessible to a global audience. While converting RTF to images can help preserve document layout as images can accurately represent the original document, including fonts, colors, and graphics. In this article, you will learn how to convert RTF to HTML or images in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Convert RTF to HTML in Java

Converting RTF to HTML ensures that the document can be easily viewed and edited in any modern web browser without requiring any additional software.

With Spire.Doc for Java, you can achieve RTF to HTML conversion through the Document.saveToFile(String fileName, FileFormat.Html) method. The following are the detailed steps.

  • Create a Document instance.
  • Load an RTF document using Document.loadFromFile() method.
  • Save the RTF document in HTML format using Document.saveToFile(String fileName, FileFormat.Html) method.
  • Java
import com.spire.doc.*;

public class RTFToHTML {
    public static void main(String[] args) {
        // Create a Document instance
        Document document = new Document();

        // Load an RTF document
        document.loadFromFile("input.rtf", FileFormat.Rtf);

        // Save as HTML format
        document.saveToFile("RtfToHtml.html", FileFormat.Html);
        document.dispose();
    }
}

Convert a Word document to an HTML file

Convert RTF to Image in Java

To convert RTF to images, you can use the Document.saveToImages() method to convert an RTF file into individual Bitmap or Metafile images. Then, the Bitmap or Metafile images can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF format files. The following are the detailed steps.

  • Create a Document object.
  • Load an RTF document using Document.loadFromFile() method.
  • Convert the document to images using Document.saveToImages() method.
  • Iterate through the converted image, and then save each as a PNG file.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class RTFtoImage {
    public static void main(String[] args) throws Exception{
        // Create a Document instance
        Document document = new Document();

        // Load an RTF document
        document.loadFromFile("input.rtf", FileFormat.Rtf);

        // Convert the RTF document to images
        BufferedImage[] images = document.saveToImages(ImageType.Bitmap);

        // Iterate through the image collection
        for (int i = 0; i < images.length; i++) {

            // Get the specific image
            BufferedImage image = images[i];

            // Save the image as png format
            File file = new File("Images\\" + String.format(("Image-%d.png"), i));
            ImageIO.write(image, "PNG", file);
        }
    }
}

Convert all pages in a Word document to multiple PNG images

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.